Php 项目未导入CRUD数据库应用程序,无法检索值
我有一个简单的应用程序,HTML很简单:Php 项目未导入CRUD数据库应用程序,无法检索值,php,mysql,database,Php,Mysql,Database,我有一个简单的应用程序,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
<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管理员中很明显,所以我不会发布sqlinit文件
当我尝试添加itemfood时,会收到以下错误消息:
将食物名称、卡路里、蛋白质、碳水化合物、,
脂肪值:食物名称、卡路里、蛋白质、碳水化合物、脂肪
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"; ?>
为什么这是在找食物?而不是test.foodnames,正确的语法是什么?如前所述,只有一个测试数据库Food db不存在,表foodnames在那里
此外,当我试图从数据库中读取数据时,会出现以下错误
注意:未定义的索引:C:\xampp\htdocs\public\read.php中的userInput
在第19行中,从FoodName中选择*,其中FoodName类似:用户输入
SQLSTATE[HY093]:参数编号无效:未定义参数
注意:未定义变量:结果为C:\xampp\htdocs\public\read.php
第37行未找到结果,请注意:未定义索引:中的位置
第74行的C:\xampp\htdocs\public\read.php
我尝试的是基于用户输入检索数据库条目,即使他只输入一个字符,下面是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"; ?>
很抱歉,代码非常冗长。问题在于配置文件。您需要将数据库更改为test数据库,即$db='test'//从food更改数据库“…如果FoodName类似于:FoodName”而不是用户输入,则需要使用参数名,而不是php变量。在insert的vase中,您可能连接到了服务器上的错误数据库。请查看您的配置,我打赌这是food,而不是在那里测试。@axel.michel-Hm,我刚刚注意到,如果我必须使用foodName,那么我在表单中没有引用。因此,这里的FoodName类似于:user\u input$userInput=$\u POST['userInput'].%';这种食物需要改变。我该怎么做?@ptts输入字段的name attribute是$\u POST数组中的键。因此,如果您的输入字段名为food,您可以这样做:$userInput=$\u POST['food']LOL,这很尴尬:-这是我的第一个数据库项目,它是一个噩梦,包括所有服务器配置的怪癖,但现在它工作了:-嗯,读取部分还没有工作,但谢谢