mysqli_stmt :: bind_param():类型定义字符串中的元素数量与绑定变量的数量不匹配,无法更新

时间:2016-06-09 03:34:45

标签: php html

我提交或更新此数据时遇到问题。数据无法更新。

此代码:

if(isset($_GET['newsid'])){
    if(isset($_POST['submit'])){
        $sql = $connect->prepare("UPDATE news  set title =?, short_desc =?, isi =?, author =?, status =?,  date_period_start =?,  date_period_end =?, category =? WHERE newsid =?");
        $sql->bind_param('aaaaa', 
          $title,$short_desc,$isi,$author,$status,
         $date_period_start, $date_period_end, $category);
        $title = $_POST['title'];
        $short_desc = $_POST['short_desc'];
        $isi = $_POST['isi'];
        $author  = $_POST['author'];
        $status = $_POST['status'];
        $date_period_start = $_POST['date_period_start'];
        $date_period_end = $_POST['date_period_end'];
        $category = $_POST['category'];

        if($sql->execute()){
          echo "<script>location.href='index.php'</script>";
        }else{
          echo "<script>alert('".$sql->error."')</script>";
        };
}

$res =$connect->query("SELECT newsid, title, short_desc, isi, author, status, date_period_start, date_period_end, category from news
  where newsid=".$_GET['newsid']);
$row = $res->fetch_assoc();

?>
            <div class="container">
              <div class="row">
                <h2>UPDATE</h2>
              </div>
              <div class="row">


                <form role="form" method="post" action="">
                  <input type="hidden" value="<?php echo $row['newsid'] ?>" name="newid"/>
                  <div class="form-group">
                    <label>Category : </label>
                    <input type="text" name="category" id="category" value="<?php echo $row['category']  ?>"> <br><br>
                  </div>
                  <p>
                   <label> Title : </label>
                   <input name="title" id="title" size="40" maxlength="255" value="<?php echo $row['title']  ?>"/>
                   <p/>
                   <p>
                    <label> Author : </label>
                    <input name="author" id="author" size="40" maxlength="255" value="<?php echo $row['author']  ?>"/>
                    <p/>
                    <br/>
                    <p><label>Short Description : </label> 
                      <textarea  name="short_desc" id="short_desc" value="<?php echo $row['short_desc']  ?>">
                      </textarea>
                    </p>
                    <br>
                    <p>
                      <label>Description: </label>
                      <textarea name="isi" id="isi" rows="7" cols="90" value="<?php echo $row['isi'];  ?>"></textarea>
                    </p>
                    <br/>
                    <p>
                     <label> Status: </label>
                     <input type="text" name="status" id="status" value="<?php echo $row['status']  ?>"/> <br><br>
                     <p/>
                     <p>
                      <label> Display Periode </label>

                      <input type="text" id="date_period_start" name="date_period_start" value="<?php echo $row['date_period_start']  ?>"/>
                      <input type="text" id="date_period_end" name="date_period_end" value="<?php echo $row['date_period_end']  ?>"/>
                      <input type="checkbox" name="cek" value="yes"/><br><br>
                    </p> 

                    <button type="submit" name="submit" class="btn btn-default">Submit</button>
                  </form>
          <?php
        }
          ?>

我得到的输出:

mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables .

并且还提醒您:no data supplied for parameters in prepared statement.

实际上,我无法更新数据。

请帮我解决这个问题。我更新时无法发现任何错误

感谢。

2 个答案:

答案 0 :(得分:1)

查看SQL Update语句

UPDATE news  set title =?, short_desc =?, isi =?, author =?, status =?, category =? WHERE newsid =?

我可以算7个参数。当我查看您的bind_param电话

$sql->bind_param('aaaaa', 
     $title,$short_desc,$isi,$author,$status,
     $date_period_start, $date_period_end, $category);

我计算了5个类型字符('aaaaa')和8个参数($title, $short_desc, $isi, $author, $status, $date_period_start, $date_period_end, $category)。这些所有需要匹配。阅读this可能有所帮助。

答案 1 :(得分:0)

绑定参数中的aaaa无效,需要s为string或i为整数或d为double