I have tried specifying all the columns of the table. However, nothing is being captured in the table when I hit the “copy” button.
Here is my code.
demotable.php (here is where all the data will be displayed after a form submission)
<?php
require('connect.php');
$query = "SELECT * FROM trade_history1 "; //You don't need a ; like you do in SQL
$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>" ."IDKWHAISTHIS"."</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['type'] ."</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 (Here is where I try to retrieve the entered data and duplicate it into the same table)
<?php
require ('connect.php');
$data = "INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss)
SELECT size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
FROM trade_history1
WHERE id = ''";
$db = mysql_query($data);
while($row = mysql_fetch_array($db)){ //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['type'] ."</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
}
mysql_close();
?>
Much help is needed as I am really new to php and mysql. Thanks.
When someone clicks on the copy button on the demotable.php, they are sent to the copytrade.php page, but the url would look like this: copytrade.php?copy=copy because that’s the data being sent. In order to send the data to retrieve, you must create a form entry for each field you want. Then on the copytrade.php page, use the $_GET variable to access the data.
Also, in order to insert data into the database with a query, it goes like this:
And I have tried the INSERT INTO … VALUES statement. However, there is an error. Do you mean that if I have the INSERT INTO… VALUES statement, I can remove my SELECT … FROM statement?
No, he means you will need to create a form of some description that sends the values you have from the first query - actually you just need 1; the ID number of the record you are copying.
Although I do have to question WHY you are inserting another identical row instead of updating the existing row.
Could you explain the logic behind it and what is happening?
Would you mind giving me an example? I have very little knowledge on php and mysql. The purpose of inserting another identical row is because I want to allow my users to “copy trades”. So, say User A executed a trade and User B wants to follow, User B will just have to simply click on “copy”.
That script is vulnerable to SQL Injection, any data submitted by the user in any way must always be considered to be dangerours until it has been sanitized. Also the mysql_* extension is deprecated as of version 5.5.x of PHP you should be migrating over to using either the mysqli_* extension or to PDO (in either case you should make use of prepared statements).
$stmt = $mysqli->prepare("INSERT INTO trade_history1 (size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss)
SELECT size, date, type, id, currency_pair, entry, stoploss, takeprofit, dateclose,close,profitloss
FROM trade_history1
WHERE id = '?'");
$stmt->bind_param("i", $id); // 1 ? so we use i. we use i because id is INT
$successfullyCopied = $stmt->execute();
$stmt->close();