通过提交按钮和发布操作更新SQL

时间:2018-07-30 12:07:49

标签: php sql

我希望能够按一下网站上的按钮,然后更新表格的状态字段,

但是当我放置$id = $_POST['id']时没有找到ID吗?

如果我将$id = 2放在哪里,它会像您在GIF中看到的那样更新,


<?php
include_once 'db.php';

$id = $_POST['id'];

$sql = "UPDATE form SET status='aid' WHERE id = '$id'";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?> 

= Orders.paid.inc.php


  while($row = mysqli_fetch_array($sqldata))
                 {
                  echo "</thead>";
                  echo "<tbody>";
                  echo '<form action="../assets/includes/orders.paid.inc.php" method="post">';
                  echo "<tr><td>";
                  echo $row['id'];
                  echo "</td><td>";
                  echo $row['name'];
                  echo "</td><td>";
                  echo $row['email'];
                  echo "</td><td>";
                  echo $row['diet'];
                  echo "</td><td>";
                  echo $row['pers'];
                  echo "</td><td>";
                  echo $row['quan'];
                  echo "</td><td>";
                  echo $row['address'];
                  echo "</td><td>";
                  echo $row['status'];
                  echo "</td><td>";
                  echo '<button type="submit" name="submit">Test</button>';
                  echo "</td><td>";
                  echo"<button>Canceld</button>";
                  echo "</td><td>";
                  echo"<button>Sent</button>";
                  echo "</form></td><td>";
                }

=部分来自Orders.php

1 个答案:

答案 0 :(得分:2)

您需要在表单中添加ID:

<form action="../assets/includes/orders.paid.inc.php" method="post">
    ....
    <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
    ...
</form>

此外,请检查您的SQL。目前容易受到SQL注入攻击。请遵循此处的最佳做法:How can I prevent SQL injection in PHP?

<?php
include_once 'db.php';

$id = $_POST['id'];

$stmt = $conn->prepare('UPDATE form SET status='aid' WHERE id = ?');
$stmt->bind_param('i', $id);
if ($stmt->execute()) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>