在 php 中使用 pdo 更新无效

Updation not working using pdo in php

我正在尝试更新记录,但更新查询对某些记录不起作用 reason.It 正在删除和插入正常但不知何故更新没有 work.I 检查了各种问题但不能' t find answer.I 已检查查询中插入的数据,其正常 too.This 是我的代码。

  <?php   
    require 'database.php';
        $ido = 0;
         if ( !empty($_GET['id'])) {
              $ido = $_REQUEST['id'];
              echo $ido;
         }
    if ( !empty($_POST)) {
        // keep track validation errors
        $nameError = null;
        $descError = null;
        $priceError = null;
        // keep track post values
        $name = $_POST['name'];
        $desc = $_POST['desc'];
        $price = $_POST['price'];
        // validate input
        $valid = true;
        if (empty($name)) {
            $nameError = 'Please enter Name';
            $valid = false;
        }

        if (empty($desc)) {
            $descError = 'Please enter Valid descriptin';
            $valid = false;
        }
        if (empty($price) || filter_var($price, FILTER_VALIDATE_INT) == false) {
            $priceError = 'Please enter a valid price';
            $valid = false;
        }  

        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE Items SET I_name = ? , I_desc = ? ,I_price = ? WHERE I_id = ?"; <---This is the update query part
            $q = $pdo->prepare($sql);
            $q->execute(array($name,$desc,$price,$ido));    <---these are the values inserted
            Database::disconnect();
            header("Location: index.php");
        }
      }
        else {
        echo $ido;
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM Items where I_id = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($ido));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $name = $data['I_name'];
        $desc = $data['I_desc'];
        $price = $data['I_price'];
        Database::disconnect();
    }

   ?>
   <!DOCTYPE html>
     <html lang="en">
     <head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
    </head>

     <body>
     <div class="container">

                <div class="span10 offset1">
                    <div class="row">
                        <h3>Update Items</h3>
                    </div>

                    <form class="form-horizontal" action="update_items.php" method="post">
                      <div class="control-group <?php echo !empty($nameError)?'error':'';?>">
                        <label class="control-label">Name</label>
                        <div class="controls">
                            <input name="name" type="text"  placeholder="Item Name" value="<?php echo !empty($name)?$name:'';?>">
                            <?php if (!empty($nameError)): ?>
                                <span class="help-inline"><?php echo $nameError;?></span>
                            <?php endif; ?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($descError)?'error':'';?>">
                        <label class="control-label">Description</label>
                        <div class="controls">
                            <input name="desc" type="text" placeholder="Item Description" value="<?php echo !empty($desc)?$desc:'';?>">
                            <?php if (!empty($descError)): ?>
                                <span class="help-inline"><?php echo $descError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($priceError)?'error':'';?>">
                        <label class="control-label">Price</label>
                        <div class="controls">
                            <input name="price" type="text" placeholder="Item Price" value="<? php echo !empty($price)?$price:'';?>">
                            <?php if (!empty($priceError)): ?>
                                <span class="help-inline"><?php echo $priceError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="form-actions">
                          <button type="submit" class="btn btn-success">Create</button>
                          <a class="btn" href="index.php">Back</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>

这是您的表格:

<form class="form-horizontal" action="update_items.php" method="post">
                                                      ^ nothing here

如您所见,您正在发帖,url 之后没有查询变量。

然后你检查ID:

$ido = 0;
if (!empty($_GET['id'])) {
    $ido = $_REQUEST['id'];
    echo $ido;
}

$ido 将保持 0 因为没有 $_GET['id'].

您可以修改您的表单以添加 ID 或在表单中添加带有 ID 的隐藏变量并检查 $_POST['id']

我会选择第二个选项:

<form class="form-horizontal" action="update_items.php" method="post">
  <input type="hidden" name="id" value="<?php echo $ido; ?>">

并在 php 中:

if (!empty($_POST)) {
   $ido = $_POST['id'];