在询问之前,我已经阅读了有关stackoverflow的几个问题。我对php和mysql知之甚少,需要很多帮助。
所以,我需要的是为每个表行创建一个按钮,这样当用户点击“复制”按钮时,该行的数据将被复制到数据库中。我怎么能这样做?
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>
<a href="copytrade.php?id=$_GET['id]"> View </a>
</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();
?>
顺便说一句,每当我点击demotable.php上的“复制按钮”时,链接就是:http://localhost/1103242B/demo/copytrade.php?copy=copy
。我可以知道我错过了哪部分代码或者我做错了吗?谢谢。
答案 0 :(得分:1)
您必须将所有表格设计放在表单标记中。然后,对于每一行,您必须使用类似http://localhost/1103242B/demo/copytrade.php?id=id
的URL添加复制链接。这里id是数据库中的记录id。您正在生成上表,然后在下面的表单中,您没有copytrade.php
页面的任何ID引用。
这样你也可以这样做,但是在每行中你可以放一些复选框,然后当用户点击复选框时在表单内的隐藏字段中设置id,然后你就可以将该id发布到{{1} }。
两种方式都可以。
尝试编辑您的页面,如下所示。
copytrade.php