Hi, I have read through several questions on stackoverflow before asking this. I have very little knowledge on php and mysql so much help is needed.

So, what I needed is to create a button for every table row so that when user hits the "copy" button, the data for that row will be copied into the database. How can I do this?

demotable.php

<?php 
    require('connect.php');
    $query = "SELECT * FROM trade_history1 ";
    $result = mysql_query($query);

    echo "<table border = '1px'>"; // start a table tag in the HTML
    echo "<tr><td>" . "ID" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Currency Pair" . "</td><td>" . "Entry" . "</td><td>" . "Stoploss" . "</td><td>". "Take Profit" . "</td><td>" . "Date Close" . "</td><td>" ."Close" . "</td><td>" ."Profit/Loss"."</td><td>" ."Copy"."</td></tr>" ;  //$row['index'] the index here is a field name

    while($row = mysql_fetch_array($result)){   //Creates a loop to loop through results
    echo "<tr><td>" . $row['id'] . "</td><td>" . $row['date'] . "</td><td>" . $row['type'] . "</td><td>" . $row['size'] ."</td><td>" . $row['currency_pair'] ."</td><td>" . $row['entry'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $row['dateclose'] ."</td><td>" . $row['close'] ."</td><td>" . $row['profitloss'] . "</td></tr>";  //$row['index'] the index here is a field name
    }

    echo "</table>"; //Close the table in HTML

    mysql_close(); //Make sure to close out the database connection
    ?>

    <html>
    <form method = "GET" action = "copytrade.php"><input type = "submit" name = "copy" value = "copy"/></form>
    </html>

copytrade.php

<?php

        require ('connect.php');

        $mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database"); 

        $stmt = $mysqli->prepare("INSERT INTO trade_history1 (size, date, type, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss) 
                                      SELECT size, date, type, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
                                      FROM trade_history1
                                      WHERE id = ?"); 


        $stmt->bind_param("i", $id); // 


        $successfullyCopied = $stmt->execute(); 


        $stmt->close();

        $mysqli->close();

?>

By the way, whenever I click the "copy button" on demotable.php, the link will be this: "http://localhost/1103242B/demo/copytrade.php?copy=copy". May I know which part of the code did I missed out or did I do wrongly? Thanks.

I am a little confused.

Why would you display info from the database, to then copy it back to the database???

Am i missing something here? :)

Also you have a mix of MySQLi and MySQL.

Oh, because I want the User B to be able to duplicate a trade executed from User A if he wants to.

One solution:
The form with the button should be within the while loop so each row has additional column containing the form with one button. The button's name should be the ID ($row['id']) so you get the ID in the $_GET array upon clicking (please note that it is more appropriate to use action="post" when changing data on the server).

Another solution:
Each row has additional column with a checkbox so the user can select as many rows as they wish. At the end of the table there is only one submit button to submit all the checked rows at once. In this case you have only one form and the checkbox valuees are the IDs ($row['id']). See this article as an example. Again, note that it is more appropriate to use action="post" when changing data on the server.

Which solution to choose is a matter of how you would like the data to be used.

Also: Your html tags are in wrong places.

commented: very nice explaination +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.