-1

What I want is that when I select one meal that he show me the meal and the right price. Now he show me the meals but when i select one of these meals there are comes a error.

Error: Call to a member function fetch_assoc() on a non-object in

When I in mwvp.php this code removed. Then you see al prices that stored in database.

You can see under this the removed code en the orginal.

Change Meal.php

<?php
include ("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<title>Kantine</title>
<link rel="stylesheet" type="text/css" href="test.css">
<meta charset="utf-8">
<script src="js/jquery.min.js"></script>
   <script type="text/javascript">
        function getMeals(str) {
            $('#meal_data').text("");
            if (str == "select")
                return;
            $('#meal_data').load('mwvp.php?q='+str);
        }   
  </script>
</head>
<body>
<div id="container">
<div id="logo"></div>
<div id="header">
<h1>Change Meal</h1></div>
<div id="menu">
<a href="Addmeal.php"><img src="Addmeal.png"/></a>
</div>
<div id="content">
<?php
$result = mysqli_query($con,"SELECT meal, price FROM Meals ");
  echo '<select name="Meals"onchange="getMeals(this.value)"><option value="select">Choose Meal</option>';
while ($row = $result->fetch_assoc())  {
  echo "<option value=\""."\">" . $row['meal']."</option>\n";
  echo '<br />'; 
  }
  echo '</select>';
  echo '<ul id="meal_data">';
  echo '</ul>';

  mysqli_close($con);
?></div>
<div id="footer"></div></div>
</body>
</html>

Original mwvp.php

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<?php
$q = $_GET["q"];
include("connection.php");

$result = mysqli_query($con, "SELECT meal, price FROM meals WHERE meal = " .$q);

while  ($row = $result->fetch_assoc())  {
    echo '<input name="price" type="text" value="'.$row['price'].'" />';
   }
?>
</html>

Removed code in mwvp.php

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<?php
include("connection.php");

$result = mysqli_query($con, "SELECT meal, price FROM meals") 
while  ($row = $result->fetch_assoc())  {
    echo '<input name="price" type="text" value="'.$row['price'].'" />';
   }
?>
</html>

1 Answers1

0

mysqli_query returns FALSE in case of error. There seems to be an error in your SQL request. Try to change

$result = mysqli_query($con, "SELECT meal, price FROM meals WHERE meal = " .$q);

To

$result = mysqli_query($con, "SELECT meal, price FROM meals WHERE meal = '" . mysqli_real_escape_string($con, $q) . "'");
barbashov
  • 542
  • 3
  • 9
  • Try to change echo "\n"; to echo "\n"; – barbashov Apr 04 '13 at 13:42
  • It works half if i have 1 meal name with no space he show me the price but if i have a meal with a space he show me nothing – MMTjoelkerT Apr 04 '13 at 13:51
  • For better experience, you should slightly change your app design. Do you have an id column in your meals table? – barbashov Apr 04 '13 at 14:51
  • Now you should address your meals by id. i.e. change `echo "\n"; to echo "\n";` and `$result = mysqli_query($con, "SELECT meal, price FROM meals WHERE meal = '" . mysqli_real_escape_string($con, $q) . "'");` to `$result = mysqli_query($con, "SELECT id, meal, price FROM meals WHERE meal = '" . intval($q) . "'");` and `$result = mysqli_query($con,"SELECT meal, price FROM Meals ");` to `$result = mysqli_query($con,"SELECT id, meal, price FROM Meals ");` – barbashov Apr 05 '13 at 14:58