【发布时间】:2018-02-15 09:48:15
【问题描述】:
我有一个简单的应用程序,HTML 很简单:
<ul>
<li><a href="create.php"><strong>Create</strong></a> - add a food by
name</li>
<li><a href="read.php"><strong>Read</strong></a> - find a food and the
nutrition values</li>
<li><a href="update.php"><strong>Create</strong></a> - update a foods
values</li>
<li><a href="delete.php"><strong>Read</strong></a> - delete an entry</li>
</ul>
我有一个名为“test”的数据库并成功创建了“foodnames”表,这在mySQL admin中很明显,所以我不会发布sql init文件。
当我尝试添加项目(食物)时,我收到以下错误消息:
插入食物名称(食物名称、卡路里、蛋白质、碳水化合物、 脂肪)值(:食物名称,:卡路里,:蛋白质,:碳水化合物,:脂肪) SQLSTATE [42S02]:未找到基表或视图:1146 表 'food.foodnames' 不存在
这是我的 create.php:
if (isset($_POST['submit']))
{
require "../config.php";
require "../common.php";
try
{
$connection = new PDO($dsn, $username, $password, $options);
$new_food = array(
"foodName" => $_POST['foodName'],
"calories" => $_POST['calories'],
"proteins" => $_POST['proteins'],
"carbohydrates" => $_POST['carbohydrates'],
"fats" => $_POST['fats']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"foodnames",
implode(", ", array_keys($new_food)),
":" . implode(", :", array_keys($new_food))
);
$statement = $connection->prepare($sql);
$statement->execute($new_food);
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']) && $statement)
{ ?>
<blockquote><?php echo $_POST['foodName']; ?> successfully added.
</blockquote>
<?php
} ?>
<h2>Add a food</h2>
<form method="post">
<label for="foodName">Foods Name</label>
<input type="text" name="foodName" id="foodName">
<label for="calories">Calories per 100 grams</label>
<input type="text" name="calories" id="calories">
<label for="proteins">Proteins in %</label>
<input type="text" name="proteins" id="proteins">
<label for="carbohydrates">Carbohydrates in %</label>
<input type="text" name="carbohydrates" id="carbohydrates">
<label for="fats">Fats in %</label>
<input type="text" name="fats" id="fats">
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Back to home</a>
<?php require "templates/footer.php"; ?>
为什么要查找 food.foodnames?而不是 test.foodnames 什么是正确的语法?如前所述,只有一个测试数据库(food db 不存在),并且表 foodnames 在那里。
此外,当我尝试从数据库中读取时,我收到以下错误
注意:未定义索引:C:\xampp\htdocs\public\read.php 中的 userInput 在第 19 行 SELECT * FROM foodnames WHERE FoodName LIKE :user_input SQLSTATE[HY093]:参数号无效:参数未定义
注意:未定义的变量:导致 C:\xampp\htdocs\public\read.php 在第 37 行没有找到通知的结果:未定义的索引:位置在 C:\xampp\htdocs\public\read.php 在第 74 行
我正在尝试根据用户输入检索数据库条目,即使他只输入一个字符,这里是 read.php 代码:
<?php
/**
* Function to query information based on
* a parameter: in this case, food name.
*
*/
if (isset($_POST['submit']))
{
try
{
require "../config.php";
require "../common.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM foodnames
WHERE FoodName LIKE :user_input";
$userInput = $_POST['userInput'] . '%';
$statement = $connection->prepare($sql);
$statement->bindParam(':FoodName', $userInput, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']))
{
if ($result && $statement->rowCount() > 0)
{ ?>
<h2>Results</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Food Name</th>
<th>Calories/100g</th>
<th>Proteins</th>
<th>Carbohydrates</th>
<th>Fats</th>
<th>Time to burn by running</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row)
{ ?>
<tr>
<td><?php echo escape($row["id"]); ?></td>
<td><?php echo escape($row["foodNAme"]); ?></td>
<td><?php echo escape($row["calories"]); ?></td>
<td><?php echo escape($row["proteins"]); ?></td>
<td><?php echo escape($row["carbohydrates"]); ?></td>
<td><?php echo escape($row["fats"]); ?></td>
<td><?php echo escape($row["ttb"]); ?> </td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<blockquote>No results found for <?php echo escape($_POST['location']);
?>.</blockquote>
<?php
}
}?>
<h2>Find food by name</h2>
<form method="post">
<label for="food">Food</label>
<input type="text" id="food" name="food">
<input type="submit" name="submit" value="View Results">
</form>
<a href="index.php">Back to home</a>
<?php require "templates/footer.php"; ?>
抱歉,代码非常冗长。
【问题讨论】:
-
'...WHERE FoodName LIKE : FoodName' 而不是 user_input,您需要使用参数名称,而不是 php 变量。
-
在插入的花瓶中,您可能连接到服务器上的错误数据库。
-
看看你的配置,我敢打赌这是食物而不是在那里测试。
-
@axel.michel 嗯,我只是注意到,如果我必须使用 foodName,那么我在表格中没有参考。所以这个: WHERE FoodName LIKE :user_input"; $userInput = $_POST['userInput'] . '%'; 这个 需要更改。我该如何处理?
-
@ptts 输入字段的名称属性是 $_POST 数组中的键。因此,如果您的输入字段名称为 food,您将这样做: $userInput = $_POST['food']