-1

我想要的是,当我选择一顿饭时,他会告诉我这顿饭和合适的价格。现在他向我展示了饭菜,但是当我选择其中一种饭菜时,出现了错误。

错误:在非对象上调用成员函数 fetch_assoc()

当我在 mwvp.php 中删除此代码时。然后您会看到存储在数据库中的所有价格。

您可以在此下方看到已删除的原始代码。

换餐.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>

原始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>

删除了 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>
4

1 回答 1

0

mysqli_query 在出错的情况下返回 FALSE。您的 SQL 请求中似乎有错误。尝试改变

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

$result = mysqli_query($con, "SELECT meal, price FROM meals WHERE meal = '" . mysqli_real_escape_string($con, $q) . "'");
于 2013-04-04T13:28:57.390 回答