使用mysqli函数进行PHP更新查询

时间:2015-02-17 09:05:49

标签: php mysqli

在此,我必须仅更新categorysdfdassigntoreviewed和文件上传。因此,当用户点击更新按钮时,它将转到update.php并更新必要的字段。但它没有执行,它显示了一个警告:

  

警告:mysqli_stmt :: bind_param():变量数不匹配   准备好的陈述中的参数数量

有人可以告诉我出了什么问题吗?

updateview.php

<form action="update.php" method="post" enctype="multipart/form-data" novalidate>
<?php
include_once('dbconn.php');
$srn = $_GET['srn'];
  if($stmt = $mysqli->prepare("SELECT srn, client, type, fy, category, sd, fd, assignto, edoc, reviewed, upload FROM main WHERE srn=?")){
    $stmt->bind_param("s",$_GET["srn"]);
    $stmt->execute();
    $stmt->bind_result($srn,$client,$type,$fy,$category,$sd,$fd,$assignto,$edoc,$reviewed,$upload);
    $stmt->fetch();
    $stmt->close();
  }
  ?>
  <label> <span>SRN</span>
  <input name="srn" type="text" id="srn" size="15" readonly="readonly" maxlength="40" value="<?php echo $srn; ?>"/>
  </label>
  <label> <span>Client</span>    
  <select class="required" name="client" disabled="disabled"/>
  <?php
    if($stmt = $mysqli->prepare("SELECT cname FROM client")){
      $stmt->execute();
      $stmt->bind_result($cname);
      while($stmt->fetch()){
        ?>
<option value="<?php echo $cname; ?>" <?php if($cname==$client){ echo "selected"; } ?>> <?php echo $cname; ?> </option>
        <?php
      } /* END OF WHILE LOOP */
      $stmt->close();
    } /* END OF PREPARED STATEMENT OF CLIENT */
  ?>
  </select>
    <input type="hidden" name="client" value = "<?php echo $row['client']; ?>" />
  </label>
  <label> <span>Type</span>    
  <select class="required" name="type" disabled="disabled"/>
  <?php
    if($stmt = $mysqli->prepare("SELECT type FROM entity")){
      $stmt->execute();
      $stmt->bind_result($type);
      while($stmt->fetch()){
        ?>
          <option value="<?php echo $type; ?>" <?php if($type==$type){ echo "selected"; } ?>> <?php echo $type; ?> </option>
        <?php
      } /* END OF WHILE LOOP */
      $stmt->close();
    } /* END OF PREPARED STATEMENT */
  ?>
  </select>
    <input type="hidden" name="type" value = "<?php echo $row['type']; ?>" />
  </label>
  <label> <span>Financial Year</span>    
  <select class="required" name="fy" disabled="disabled"/>
  <?php
    if($stmt = $mysqli->prepare("SELECT year FROM fy")){
      $stmt->execute();
      $stmt->bind_result($year);
      while($stmt->fetch()){
        ?>
      <option value="<?php echo $year; ?>" <?php if($year==$fy){ echo "selected"; } ?>> <?php echo $year; ?> </option>
        <?php
      } /* END OF WHILE LOOP */
      $stmt->close();
    } /* END OF PREPARED STATEMENT OF */
  ?>
  </select>
    <input type="hidden" name="fy" value = "<?php echo $row['fy']; ?>" />
  </label>
  <label> <span>Category</span>      
  <select class="required" name="category"/>
  <?php
    if($stmt = $mysqli->prepare("SELECT name FROM category")){
      $stmt->execute();
      $stmt->bind_result($name);
      while($stmt->fetch()){
        ?>
          <option value="<?php echo $name; ?>" <?php if($name==$category){ echo "selected"; } ?>> <?php echo $name; ?> </option>
        <?php
      } /* END OF WHILE LOOP */
      $stmt->close();
    } /* END OF PREPARED STATEMENT OF CATEGORY */
  ?>
  </select>
  </label>
<label> <span>Short Description</span>
    <textarea required="required" name='sd'><?php echo $sd; ?></textarea>
</label>
<label> <span>Full Description</span>
    <textarea required="required" name='fd'><?php echo $fd; ?></textarea>
</label>
  <label> <span>Assign To</span>    
  <select class="required" name="assignto"/>
  <?php
    if($stmt = $mysqli->prepare("SELECT name FROM assignto")){
      $stmt->execute();
      $stmt->bind_result($name);
      while($stmt->fetch()){
        ?>
          <option value="<?php echo $name; ?>" <?php if($name==$name){ echo "selected"; } ?>> <?php echo $name; ?> </option>
<?php
}
      $stmt->close();
}
?>
  </select>
  </label>
  <label> <span>EDOC</span>
  <input name="edoc" type="text" id="edoc" size="15" readonly="readonly" maxlength="40" value="<?php echo $edoc; ?>"/>
  </label>
  <label> <span>Reviewed By</span>    
  <select class="required" name="reviewed"/>
  <?php
    if($stmt = $mysqli->prepare("SELECT type FROM entity")){
      $stmt->execute();
      $stmt->bind_result($type);
      while($stmt->fetch()){
?>
<option value="<?php echo $type; ?>" <?php if($type==$reviewed){ echo "selected"; } ?>> <?php echo $type; ?> </option>
<?php
}
      $stmt->close();
}
?>
  </select>
  </label>
        <label>
            <span>Input Attachment</span>
        <input type="file" name ="filename" required>
        </label>
<button id='cancel' type='cancel'>Cancel</button>
<button id='send' type='submit'>Update</button>
</form>

update.php

<?php
    include('dbconn.php');
    $srn      = $_POST['srn'];
    $client   = $_POST['client'];
    $type     = $_POST['type'];
$fy       = $_POST['fy'];
    $category = $_POST['category'];
    $sd       = $_POST['sd'];
    $fd       = $_POST['fd'];
    $assignto = $_POST['assignto'];
    $edoc     = $_POST['edoc'];
    $reviewed = $_POST['reviewed'];

    $stmt = $mysqli->prepare("UPDATE main SET category=?,sd=?,fd=?,assignto=?,reviewed=?,upload=? WHERE srn=?");
    $stmt->bind_param('sssssb',$srn,$category, $sd,$fd,$assignto,$reviewed,$upload);
    $stmt->execute();
?>

dbconn.php

<?php
$host = "localhost";
$user = "root";
$pwd  = "root";
$db   = "eservice";
$mysqli = new mysqli($host,$user,$pwd,$db);
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno()) {
   echo "Failed to connect to mysql : " . mysqli_connect_error();
    exit();
}
?>

2 个答案:

答案 0 :(得分:0)

$stmt->bind_param('sssssb',$category, $sd,$fd,$assignto,$reviewed,$upload);

这里有1种类型的字符串和6种参数;

 $stmt = $mysqli->prepare("UPDATE main SET category=?,sd=?,fd=?,assignto=?,reviewed=?,upload=? WHERE srn=?");

在这里你有7个?这个问题应该是:

 $stmt->bind_param('sssssbd',$category, $sd,$fd,$assignto,$reviewed,$upload, $srn);

答案 1 :(得分:0)

代码中几乎没有问题。尝试解决它们并查看问题是否已解决。

  1. select不是自结尾标记。请更正。
  2. 您没有在$uploads中初始化变量update.php,应该使用$_POST['filename']初始化这似乎是实际问题
  3. 选择元素为readonly,因此这些值不会包含在已发布的表单中。尝试在它们旁边添加隐藏字段,并使用这些隐藏字段在update.php
  4. 中获取数据