繁体   English   中英

PHP MYSQL 和更新表中的数据

[英]PHP MYSQL and updating data in a table

大家好。 我试图弄清楚如何让这段代码做我想做的事,但它让我望而却步。

这个脚本应该做的是查看寄售物品的数据库,挑选出已售出='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']将是 arrays,您可以使用$_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"; ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM