-2

I am new on php and I have create a project that bring a data from the database as the user selection , and when the execution of the project it occurred an error at an method , and I didn't now how to solve it . hear is the php code : -

     <?php

      $db = new PDO('sqlite:dinner.db');
      $meals = array('breakfast','lunch','dinner');

      if(in_array( $_POST['meal'], $meals)){

     $stmt = $db->prepare('SELECT dish,price FROM meals WHERE meal LIKE ?');
     $stmt->execute(array($_POST['meal']));
    $rows = $stmt->fetchAll();

      if(count($rows) == 0){
         print "No dishes available now";
      } else {

          print "<table><tr><th>dish</th><th>price</th></tr>";
          foreach($rows as $row) {
           print "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
      }
          print "</table>";
      }
} else {
   print "Unknown meal";
  }

  ?>

and that is the html page code :

          <html>
         <head>
         </head>
         <body>

      <form method="post" action="test.php">
      <lable>Select the meal to see all the available dishes:
      <input type="text" name="meal">


      </lable><input type="submit" value="Ok">
     </form>

   </body>
  </html>

and that is the output at the browser: - enter image description here

  • 2
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – CBroe Jun 12 '17 at 08:51
  • 1
    This error message has been discussed countless times already, so you can easily _research_ it. Besides that, it already tells you what is wrong in plain English. – CBroe Jun 12 '17 at 08:52

2 Answers2

0

According to PHP Docs execute should NOT have any parameter (void) and it only executes the current query.

You should pass your parameters to fill the query by bindValue or bindParam. Look at his example to see the correct way of this manner.

Maybe something like this fixes it, $meal has to be only one string and not an array though:

$stmt = $db->prepare('SELECT dish, price FROM meals WHERE meal LIKE ?');
$stmt->bindValue(1, $meal, SQLITE3_TEXT);
PRAISER
  • 793
  • 7
  • 15
0

to pass your params to the query you need to use bindparams not in the execute here how :

<?php

  $db = new PDO('sqlite:dinner.db');
  $meals = array('breakfast','lunch','dinner');

  if(in_array( $_POST['meal'], $meals)){

 $stmt = $db->prepare('SELECT dish,price FROM meals WHERE meal LIKE ?');
 $stmt->bindParam(1, $_POST['meal']);
 $stmt->execute();
$rows = $stmt->fetchAll();

  if(count($rows) == 0){
     print "No dishes available now";
  } else {

      print "<table><tr><th>dish</th><th>price</th></tr>";
      foreach($rows as $row) {
       print "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
  }
      print "</table>";
  }
} else { print "Unknown meal";}  ?>