0

I have attached a snippet of my php code. I am getting this error and I have tried to debug it by finding the source of why my query returning false.


<html>
<body>
<form action="index.php" method="post" >
<table style="width: 100%;">
<tr>
<td width="10%">Advisor name </td>
<td width="10%"> <input type="text" name="advisor" id="advisor" ></td>
<td width="10%">&nbsp;</td>
<td width="10%">&nbsp;</td>
<td>&nbsp;</td>
 </tr>
<tr>
<td>Student name</td>
<td> <input type="text" name="sname" id="sname" ></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Student ID</td>
 <td> <input type="text" name="studentid" id="studentid" ></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
 <td>&nbsp;</td>
</tr>
<tr>
 <td>Class Code </td>
 <td> <input type="text" name="classcode" id="classcode" ></td>
 <td>&nbsp;</td>
 <td>Search Advisor</td>
  <td> <input type="text" name="searchAdv" id="searchAdv" ></td>
 </tr>
 <tr>
  <td>&nbsp;</td>
      <td> <input type="submit" name="button1" value="Save" style="width: 93px" /></td>
      <td>&nbsp;</td>
 <td>&nbsp;</td>
     <td>
   <input type="submit" name="button2" value="Search" /></td>
     </tr>
      <tr>
       <td>&nbsp;</td>
        <td>&nbsp;</td>
    <td>&nbsp;</td>
       <td>&nbsp;</td>
        <td>&nbsp;</td>
        </tr>
        </table>
       </form>
          <hr>
        <h2> Result </h2>

         <?php

      if(array_key_exists('button1', $_POST))
      {
     DataSave(); /* IF CLICKED OF SAVED BUTTON */

      }
      else if(array_key_exists('button2', $_POST))
      {
      DataRetreive(); /* IF CLICKED OF SEARCH BUTTON */
            }

      function DataSave() /* FUNCTION START */
      {
       $dbhost = 'localhost:3306';
       $dbuser = 'root';
       $dbpass = '';
       $dbname = 'mysql';
       $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

       if(! $conn ){
       die('Could not connect: ' . mysqli_error());
       }

       if(isset($_POST["button1"])) /* IF CLICKED OF SAVED BUTTON */
       {
           $Advisor = $_POST['advisor'];
           $StudentName = $_POST['sname'];
           $Studentid = $_POST['studentid'];
           $ClassCode = $_POST['classcode'];

           /* USED PREAPRE STATMENT TO AVOID SQL INJECTION */
           $stmt = $conn->prepare("INSERT INTO datas(Advisor,Sname,Studentid,ClassCode) VALUES 
         (?,?,?,?) ");
           $stmt->bind_param("ssss", $Advisor, $StudentName, $Studentid,$ClassCode );


           $stmt->execute();
           $stmt->close();


       }
        $conn->close();

      } /* FUNCTION END */

      function DataRetreive() /* FUNCTION START */
            {

        $dbhost = 'localhost:3306';
       $dbuser = 'root';
        $dbpass = '';
       $dbname = 'mysql';
            $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

             if(isset($_POST["button2"])) /* IF CLICKED OF SEARCH BUTTON */
      {
       $searchAdv = $_POST['searchAdv']; /* RETRIEVE FROM POST DATA*/

       if(! $conn )
       {
           die('Could not connect: ' . mysqli_error());
       }

       /* USED PREAPRE STATMENT TO AVOID SQL INJECTION */

       $stmt = $conn->prepare("SELECT * FROM datas where Advisor like ? ");
       $stmt->bind_param("s", $searchAdv);

       $stmt->execute();
       $stmt->bind_result($Advisor , $StudentName,$Studentid,$ClassCode);

       /* fetch values */
       while ($stmt->fetch()) {
                   echo " Advisor : " . $Advisor. " , Student Name : " . $StudentName. " , StudentId 
          : " . $Studentid. ",
                   Class Code : " . $ClassCode. " <br>";

       }

       $stmt->close();
       }
          mysqli_close($conn);

       } /* FUNCTION END */

         ?>
        </body>
        </html>
Dharman
  • 26,923
  • 21
  • 73
  • 125
dyldev
  • 1
  • Also, please do not use irrelevant tags. If there is a PHP error thrown, the problem is in o way related to HTML or jQuery – Nico Haase Nov 03 '20 at 09:25

1 Answers1

-2
$stmt = $conn->prepare("INSERT INTO datas(Advisor,Sname,Studentid,ClassCode) VALUES 
         (?,?,?,?) ");
           $stmt->bind_param("ssss", $Advisor, $StudentName, $Studentid,$ClassCode );

i'm not sure but i think You have got 4 columns in 'INSERT' but You try to bind 5 values. Number of columns have to be same as values

  • 1
    According to @Jay Blanchard [answer](https://stackoverflow.com/a/31842749/6117399), it is likely because there is an issue with the query , so he needs to check whether the prepare is true or false – Burhan Kashour Nov 03 '20 at 09:31
  • @BurhanKashour They don't need to check. If they enable error reporting then they will see all errors – Dharman Nov 03 '20 at 11:02
  • or just use `try { } catch (\ErrorException $th) { }` statement – Pinks Not Dead Nov 03 '20 at 12:12