gpt4 book ai didi

php - 如何为每一行插入按钮,以便该特定行的数据可以复制到数据库中

转载 作者:行者123 更新时间:2023-11-29 13:41:01 24 4
gpt4 key购买 nike

在提出这个问题之前,我已经阅读了 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 。我可以知道我错过了哪部分代码或者我做错了吗?谢谢。

最佳答案

您必须将所有表格设计放入表单标签中。然后对于每一行,您必须添加一个带有 url 的复制链接,例如 http://localhost/1103242B/demo/copytrade.php?id=id 。这里的 id 是数据库中的记录 id。您正在生成上面的表格,然后在下面的表格中,您没有任何对 copytrade.php 的 id 引用页。

这样你也可以做到这一点,但在每一行中你可以放置一些复选框,然后当用户单击复选框时在表单内的隐藏字段中设置 id,然后你可以将该 id 发布到 copytrade.php

两种方式都会起作用。

尝试如下编辑您的页面。

<html>
<form method = "GET" action = "copytrade.php">

<?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>" ."Copy"."</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><td>a href='copytrade.php?id=" .$row['id'].'">copy</a></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
?>

<input type = "submit" name = "copy" value = "copy"/></form>
</html>

关于php - 如何为每一行插入按钮,以便该特定行的数据可以复制到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18087413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com