我有一个简单的应用程序,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 管理中很明显,所以我不会发布 sql 初始化文件。
当我尝试添加项目(食物)时,收到以下错误消息:
INSERT INTO foodnames (foodName, calories, proteins, carbohydrates, fats) values (:foodName, :calories, :proteins, :carbohydrates, :fats) SQLSTATE[42S02]: Base table or view not found: 1146 Table 'food.foodnames' doesn't exist
这是我的 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 数据库不存在)并且表 foodnames 存在。
此外,当我尝试从数据库中读取时,出现以下错误
Notice: Undefined index: userInput in C:\xampp\htdocs\public\read.php on line 19 SELECT * FROM foodnames WHERE FoodName LIKE :user_input SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Notice: Undefined variable: result in C:\xampp\htdocs\public\read.php on line 37 No results found for Notice: Undefined index: location in C:\xampp\htdocs\public\read.php on line 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"; ?>
抱歉,代码非常冗长。
最佳答案
问题出在配置文件上。您需要将数据库更改为测试数据库,即 $db = 'test';//从食物更改数据库
关于php - 项目未导入到 CRUD 数据库应用程序,并且无法检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46083076/