由于未定义的索引,UPDATE不起作用,但定义了变量

时间:2016-04-08 15:23:26

标签: php html mysql sql sql-update

我有两页 - 1.产品| 2.编辑产品。

我在产品页面上有一个列出产品的表格。然后我在每个产品旁边都有一个编辑按钮。当我单击编辑按钮时,它会将用户带到编辑产品页面。然后我有额外的输入字段,用户可以编辑信息。

我的问题是我无法让UPDATE query工作。它说我有一个未定义的索引用于我带来信息的变量,但我不明白,因为我在输入字段中使用这些变量。

这是我的产品页面以及我如何进入编辑页面:

$stmt = $dbc->query("SELECT `id`,`first`,`last`,`product` FROM users");
        $stmt->setFetchMode(PDO::FETCH_ASSOC);

        while($row = $stmt->fetch()) {
        ?>
        <form method="POST" action="edit-product">
            <tr>
                <td><?php echo $row['id'];?>"</td>
                <td><?php echo $row['first'];?></td>
                <td><?php echo $row['last'];?></td>
                <td><?php echo $row['product'];?></td>
                <input name="id" type="hidden" value="<?php echo $row['id'];?>" readonly>
                <input name="first" type="hidden" value="<?php echo $row['first'];?>">
                <input name="last" type="hidden" value="<?php echo $row['last'];?>">
                <input name="product" type="hidden" value="<?php echo $row['product'];?>">
                <td><div class="delete-class" name="delete" id="<?php echo $row['id']; ?>">Delete</div></td>
                 <td><input name="edit" type="submit" value="Edit"></td> 
<!-- <td><a href="edit-product">Edit</a></td> -->
            </tr>
        </form>
        <?php } ?>
        </tbody>

然后在编辑页面上它基本上设置如下:

ini_set('display_errors', 1);
error_reporting(E_ALL);

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

$newId = filter_var($id, FILTER_SANITIZE_STRING);


try {
    $host = 'localhost';
    $name = '';
    $user = '';
    $password = '';

    $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
    echo $e->getMessage();

}

if(isset($_POST['update'])) {
    $stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");

    $stmt->bindParam(1, $_POST['first']);
    $stmt->bindParam(2, $_POST['last']);
    $stmt->bindParam(3, $_POST['product']);
    $stmt->bindParam(4, $_POST['amount']);
    $stmt->bindParam(5, $_POST['available']);
    $stmt->bindParam(6, $newId);

    $stmt->execute();
}

    $stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //print_r($stmt);
    while($row = $stmt->fetch()) {

?>

<form method="POST">
<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">

<button name="update" type="submit">Update Product Information</button>
</form>

当我点击“更新”按钮启动UPDATE query时,我得到了本页顶部变量的无效索引错误。这些:

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

如果我正在使用这些变量正确发送信息,为什么我在尝试执行此查询时会收到错误消息:

我的代码或展示位置问题是否还有其他问题?

更新的编辑页面

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$id = $_POST['id'];
$first = $_POST['first'];
$last = $_POST['last'];
$product = $_POST['product'];

$newId = filter_var($id, FILTER_SANITIZE_STRING);


try {
    $host = 'localhost';
    $name = '';
    $user = '';
    $password = '';

    $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
    echo $e->getMessage();

}

    $stmt = $dbc->query("SELECT * FROM users WHERE id='$newId' ");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //print_r($stmt);
    while($row = $stmt->fetch()) {

?>
<form name="update" method="POST">
<input type="text" value="<?php echo $newId; ?>">
<input type="text" value="<?php echo $first; ?>">
<input type="text" value="<?php echo $last; ?>">
<input type="text" value="<?php echo $product; ?>">
<input type="text" value="<?php echo $row['amount']; ?>">
<input type="text" value="<?php echo $row['available']; ?>">

<button type="submit">Update Product Information</button>
</form>
<?php 
}


if(isset($_POST['update'])) {
    $stmt = $dbc->prepare("UPDATE users SET first = ?, last = ?, product = ?, amount = ?, available = ? WHERE id = ?");

    $stmt->bindParam(1, $_POST['first']);
    $stmt->bindParam(2, $_POST['last']);
    $stmt->bindParam(3, $_POST['product']);
    $stmt->bindParam(4, $row['amount']);
    $stmt->bindParam(5, $row['available']);
    $stmt->bindParam(6, $newId);

    $stmt->execute();
}
?>

0 个答案:

没有答案