php - 项目未导入到 CRUD 数据库应用程序,并且无法检索值

标签 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 
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/

相关文章:

mysql - 数据库表关系问题

mysql - 返回时间大于 22 :00 and smaller than 06:00 for group of different timestamps 的 MySql 查询

javascript - 显示单选按钮在 PHP/HTML 中无法正常工作

php - Laravel 4.2 动态数据库连接

mysql - ON CASCADE DELETE 实现

javascript - 如何将使用 jquery 添加的元素数组保存到 MySQL DB 中?

php - 根据从父表获得的两个案例连接两个表

php - 存储动态数组和字符串

database - 如何从主题中删除演示内容

php - 如何在magento 1.9中显示货币符号 "AED"而不是 "$"