gpt4 book ai didi

php - 如何插入新行并更新旧行值?

转载 作者:行者123 更新时间:2023-11-29 21:14:20 25 4
gpt4 key购买 nike

我想用我的 PHP 表单输入值向插入一个新行

$_POST['bill_amount']$_POST['paid_amount']$_POST['due_amount']

并将倒数第二行的 due_amount 列更新为值“0”。

然后,我想选择并回显最后一行(新插入的行)的值。

我的 PHP 代码:

<?php session_start(); ?>
<?php error_reporting(E_ALL ^ E_DEPRECATED); ?>

<body>

<?php
if(isset($_SESSION['username'])) {
?>

<?php
include 'dbconnection.php';
$ba = $_POST['bill_amount'];
$pa = $_POST['paid_amount'];
$da = $_POST['due_amount']
$sql = "INSERT into table(bill_amount,paid_amount,due_amount)
values ('$ba','$pa','$da')";
"UPDATE table SET due_amount='0' ORDER BY bill_id DESC LIMIT 1,1";

if (!mysql_query($sql)) {
die('Error:' . mysql_error());
}

echo "Here's the newly inserted record:";
$result = mysql_query("SELECT * FROM table ORDER BY bill_id DESC LIMIT 1");

echo "<table>
<tr>
<th>Bill ID</th>
<th>Bill Amount</th>
<th>Paid Amount</th>
<th>Due Amount</th>
</tr>";

while($row = mysql_fetch_array($result)) {

echo "<tr>";
echo "<td>" . $row['bill_id'] . "</td>";
echo "<td>" . $row['bill_amount'] . "</td>";
echo "<td>" . $row['paid_amount'] . "</td>";
echo "<td>" . $row['due_amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

<HTML>

<?php
}
else {
echo "session expired";
}
?>

</body>
</html>

<?PHP
mysql_close($con);
?>

我的 PHP 代码的问题是它成功地将新行插入到中,但没有用该值更新倒数第二行的due_amount列“0”

表格的第一个 View :
(请注意“bill_id”是自动递增主键):

       +------------+-------------+---------------+---------------+  
| bill_id | bill_amount | paid_amount | due_amount |
+------------+-------------+---------------+---------------+
| 1 | 200 | 100 | 100 |
+------------+-------------+---------------+---------------+

执行 PHP 代码后的 View :

       +------------+-------------+---------------+---------------+  
| bill_id | bill_amount | paid_amount | due_amount |
+------------+-------------+---------------+---------------+
| 1 | 200 | 100 | 100 |
+------------+-------------+---------------+---------------+
| 2 | 300 | 100 | 200 |
+------------+-------------+---------------+---------------+

执行 PHP 代码后我希望看到的 View :

       +------------+-------------+---------------+---------------+  
| bill_id | bill_amount | paid_amount | due_amount |
+------------+-------------+---------------+---------------+
| 1 | 200 | 100 | 0 |
+------------+-------------+---------------+---------------+
| 2 | 300 | 100 | 200 |
+------------+-------------+---------------+---------------+

最佳答案

您只需像这样分隔查询即可:

 <?php 
// ....
$sql = "INSERT into table(bill_amount,paid_amount,due_amount)
values ('$ba','$pa','$da')";

$sql_update = "UPDATE table SET due_amount='0' ORDER BY bill_id DESC LIMIT 1,1";

if(!mysql_query($sql))
{
die('Error:' . mysql_error());
}

if(!mysql_query($sql_update))
{
die('Error:' . mysql_error());
}
/....
?>

除此之外,您还容易受到sql注入(inject)的攻击。<​​br/>请阅读准备好的语句以及如何避免 sql 注入(inject)!

关于php - 如何插入新行并更新旧行值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36096534/

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