php - PHP MYSQL 和更新表中的数据
问题描述
大家好!我试图弄清楚如何让这段代码做我想做的事,但它让我望而却步。
这个脚本应该做的是查看寄售物品的数据库,挑选出已售出='n'的物品并按物品编号列出它们。它显示列表中的所有项目,每个项目行的末尾都有一个“出售”链接。输入买家编号和金额后,单击 Sell 链接,UPDATE 更新商品并将已售商品从“n”更改为“y”,一旦发生这种情况,商品就会从列表中消失。它的工作原理完全符合我的预期,除了一个让我无法解决的小问题。每次加载页面时,它不会更改金额和买家编号,但它只会将第一个商品的已售商品从“n”更改为“y”之后它按预期工作,将购买者编号和金额添加到所有后续项目中。直到您浏览离开它,然后在您下次返回页面时它不会正确执行第一项。我已经尝试了各种方法来让它工作,这是我最接近我想要的东西。任何帮助,将不胜感激!
<?php
/**
* Sell an Item
*/
require "../config.php";
require "../common.php";
$success = null;
if (isset($_POST["submit"])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$itemnumber = $_POST["submit"];
$buyernumber = $_POST["buyernumber"];
$amount = $_POST["amount"];
$sql = "UPDATE consignitem SET sold='y', amount='$amount', buyernumber='$buyernumber' WHERE sold='n' AND itemnumber = $itemnumber";
$statement = $connection->prepare($sql);
$statement->bindValue(':itemnumber', $itemnumber);
$statement->bindValue(':buyernumber', $buyernuumber);
$statement->bindValue(':amount', $amount);
$statement->bindValue(':sold', $sold);
$statement->execute();
$success = "Item successfully updated";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM consignitem WHERE sold='n'";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "templates/header.php"; ?>
<h2>Mark Items as Sold</h2>
<?php if ($success) echo $success; ?>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<table>
<thead>
<tr>
<th>Sale Number</th>
<th>Item Number</th>
<th>Lot Number</th>
<th>Category</th>
<th>Item Description</th>
<th>Reserve</th>
<th>Seller Number</th>
<th>Amount</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["category"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["reserve"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["amount"]); ?><br><input type="text" name="amount" id="amount" size="8"></td>
<td><?php echo escape($row["buyernumber"]); ?><br><input type="text" name="buyernumber" id="buyernumber" size="12"></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td><button type="submit" name="submit" value="<?php echo escape($row["itemnumber"]); ?>">Sell</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<br>
<?php require "templates/footer.php"; ?>
解决方案
您在表单的所有 thr 行中重复相同的输入名称。提交表单时,$_POST
变量将仅包含表单最后一行的值,而不是用户单击提交按钮的行。
您可以为输入提供由 索引的数组样式名称itemnumber
。$_POST['buyernumber']
然后$_POST['amount']
将是数组,您可以使用它$_POST['submit']
来获取适当的数组索引。
您还需要在 SQL 中使用与 中的占位符匹配的占位符bindValue()
,而不是直接在查询中替换变量。
<?php
if (isset($_POST["submit"])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$itemnumber = $_POST["submit"];
$buyernumber = $_POST["buyernumber"][$itemnumber];
$amount = $_POST["amount"][$itemnumber];
$sql = "UPDATE consignitem SET sold='y', amount= :amount, buyernumber= :buyernumber WHERE sold='n' AND itemnumber = :itemnumber";
$statement = $connection->prepare($sql);
$statement->bindValue(':itemnumber', $itemnumber);
$statement->bindValue(':buyernumber', $buyernuumber);
$statement->bindValue(':amount', $amount);
$statement->execute();
$success = "Item successfully updated";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM consignitem WHERE sold='n'";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "templates/header.php"; ?>
<h2>Mark Items as Sold</h2>
<?php if ($success) echo $success; ?>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<table>
<thead>
<tr>
<th>Sale Number</th>
<th>Item Number</th>
<th>Lot Number</th>
<th>Category</th>
<th>Item Description</th>
<th>Reserve</th>
<th>Seller Number</th>
<th>Amount</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["category"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["reserve"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["amount"]); ?><br><input type="text" name="amount[<?php echo escape($row["itemnumber"]); ?>]" size="8"></td>
<td><?php echo escape($row["buyernumber"]); ?><br><input type="text" name="buyernumber[<?php echo escape($row["itemnumber"]); ?>]" size="12"></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td><button type="submit" name="submit" value="<?php echo escape($row["itemnumber"]); ?>">Sell</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<br>
<?php require "templates/footer.php"; ?>
推荐阅读
- python - Pygame 错误:pygame 没有属性 scrren
- postgresql - 可选加入 Diesel
- amazon-web-services - AWS CDK 使用什么来获取所需的接口(Python)
- php - CURLOPT_RETURNTRANSFER 在 localhost 上工作但不在 php 中的实时服务器上工作
- python - 我想在用户为所有职位投票后显示“感谢投票”消息,然后它将显示该消息
- postgresql - 第一次安装新的 postgres 时无法在 mac Os 11.3.1 中创建用户
- javascript - 带后缀的键类型
- java - 我尝试使用带有 jdk 版本 8 的 netbeans 12 的 java 语言,但它总是在输出中出现错误
- amazon-web-services - 无法通过 SSH 隧道 Sigv4 访问 Amazon Neptune
- scenebuilder - 如何在 Eclipse 中集成 javaFX 场景构建器工具包