【问题标题】:PHP-PDO -- Can't send form data to database. The form sends it with no error but can't see on the database table via phpmyadminPHP-PDO -- 无法将表单数据发送到数据库。表单发送它没有错误,但无法通过 phpmyadmin 在数据库表上看到
【发布时间】:2021-08-15 01:38:12
【问题描述】:

我一直在通过https://www.youtube.com/watch?v=2eebptXfEvw 教程学习 PHP。时间戳在02:33:53 - Start working on Products CRUD (bad version) 和03:13:45 - Form Validation 之间。

我正在和导师做同样的事情。使用 phpmyadmin 创建了一个数据库,然后创建了一个表单并与pdo 建立了连接,我正在尝试将数据从我的表单发送到数据库,但是当我通过 phpmyadmin 查看数据库表时看不到数据。

另外,我正在使用 Manjaro Linux 并安装了 Xampp 服务器,如果这与它有任何关系的话。

这是我的create.php 代码。我也有一个index.php 文件,基本上是一样的。

<?php

$pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// image=&title=&description=&price=

$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$date = date('Y-m-d H:i:s');

$pdo->prepare("INSERT INTO products (title, image, description, price, create_date
    VALUE (:title, :image, :description, :price, :date)");

?>

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <link rel="stylesheet" href="app.css">

    <title>Products CRUD</title>
</head>

<body>
    <h1>Create new product</h1>

    <form enctype="multipart/form-data" action="create.php" method="POST">
        <div class="mb-3">
            <label>Product Image</label>
            <br>
            <input type="file" name="image">
        </div>
        <div class="mb-3">
            <label>Product Title</label>
            <input type="text" name="title" class="form-control">
        </div>
        <div class="mb-3">
            <label>Product Description</label>
            <textarea class="form-control" name="description"></textarea>

        </div>
        <div class="mb-3">
            <label>Product Price</label>
            <input type="number" step=".01" name="price" class="form-control">
        </div>

        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</body>

</html>

【问题讨论】:

  • VALUE 应该是VALUES
  • 你应该从那个语法错误中得到一个异常。
  • 你从不执行语句,也许这就是你没有得到异常的原因。
  • 这些步骤在视频中的 3:06:00 到 3:09:00 之间。
  • 我已将VALUE 更改为VALUES,但仍然无法向数据库发送数据。

标签: php mysql pdo xampp


【解决方案1】:

您在准备后缺少执行语句。

<?php

$pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// image=&title=&description=&price=

$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$date = date('Y-m-d H:i:s');

$pdo->prepare("INSERT INTO products (title, image, description, price, create_date
    VALUE (:title, :image, :description, :price, :date)");
//this line is added that you are missing
$pdo->execute(); 


?>

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <link rel="stylesheet" href="app.css">

    <title>Products CRUD</title>
</head>

<body>
    <h1>Create new product</h1>

    <form enctype="multipart/form-data" action="create.php" method="POST">
        <div class="mb-3">
            <label>Product Image</label>
            <br>
            <input type="file" name="image">
        </div>
        <div class="mb-3">
            <label>Product Title</label>
            <input type="text" name="title" class="form-control">
        </div>
        <div class="mb-3">
            <label>Product Description</label>
            <textarea class="form-control" name="description"></textarea>

        </div>
        <div class="mb-3">
            <label>Product Price</label>
            <input type="number" step=".01" name="price" class="form-control">
        </div>

        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
</body>

</html>

【讨论】:

  • 尝试 var_dump($pdo->execute())
【解决方案2】:

我找到了解决办法。

<?php

$pdo = new PDO('mysql:host=localhost;port=3306;dbname=products_crud', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// image=&title=&description=&price=

$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$date = date('Y-m-d H:i:s');

// $pdo->exec("INSERT INTO products (title, image, description, price, create_date)
//     VALUE (:title, :image, :description, :price, :date)");

// Above statement gives this error:

// Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':title, :image, :description, :price, :date)' at line 2 in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php:13 Stack trace: #0 /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php(13): PDO->exec('INSERT INTO pro...') #1 {main} thrown in /opt/lampp/htdocs/php-crash-course-2020/14_product_crud/create.php on line 13

// So I changed into the line below and it works.I suppose it is about SQL syntax and my MariaDB server version.

$pdo->exec("INSERT INTO products (title, image, description, price, create_date)
    VALUE ('$title', '', '$description', $price, '$date')");


?>

【讨论】:

  • 我使用谷歌浏览器但不使用火狐。不知道为什么。
猜你喜欢
  • 2014-01-20
  • 2016-06-21
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多