0

Here I have to update only category,sd,fd,assignto,reviewed by and file upload. So when the user clicks on update button it will go to the update.php and updates the necessary fields. But its not executing and its showing an a warning:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement

Can somebody tell me what's wrong?

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();
}
?>
Kiran
  • 31
  • 7
  • "Number of variables doesn't match" . `bind_param('sssssb',$category, $sd,$fd,$assignto,$reviewed,$upload);` has 7 params. But in query 6. – Mehman Bashirov Feb 17 '15 at 09:15
  • Now I passed one more param but still not working – Kiran Feb 17 '15 at 09:22
  • Can you please add the HTML form structure also? – Cyberpks Feb 17 '15 at 09:23
  • Its better to add the HTML form, that is rendered at the browser. I mean instead of adding the whole php and html code, try adding the rendered html by copying from the browser's source, because the current code is quite confusing. You can use `Inspect Element` of Chrome or `Firebug` of firefox for that. This might help: http://stackoverflow.com/questions/12576247/copying-code-from-inspect-element-in-google-chrome – Cyberpks Feb 17 '15 at 09:40

2 Answers2

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

here you got 1 types string and 6 params;

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

and here you got 7 ? Thats the problem, it should be :

 $stmt->bind_param('sssssbd',$category, $sd,$fd,$assignto,$reviewed,$upload, $srn);
szapio
  • 998
  • 1
  • 9
  • 15
  • @Kiran. i know, and you make a param placeholder for $srn and you didn't bind variable to this placeholder, so you got 7 placeholders and 6 params binded, and that's the reason why it's not working – szapio Feb 17 '15 at 09:13
  • How to do that tell me because I'm new to the php. – Kiran Feb 17 '15 at 09:14
  • @Kiran, i wrote it above, you should pass one more param to $stmt->bind_param() – szapio Feb 17 '15 at 09:16
  • Can you update your question, and place there var_dump($_POST) ? And, did you pass correct type for $srn -> the first string in $stmt->bind_param – szapio Feb 17 '15 at 09:23
0

There are few problems in the code. Try solving them and see if the problem is solved.

  1. select is not a self ending tag. Please correct that.
  2. You are not initializing variable $uploads in update.php, It should be initialized with $_POST['filename'] This seems to be the actual problem
  3. The select elements are readonly so the values won't be included in the posted form. Try adding hidden fields next to them and use these hidden fields to get the data instead in update.php
Cyberpks
  • 1,401
  • 6
  • 21
  • 51