PHP-PDO -- 无法将表单数据发送到数据库。表单发送它没有错误,但无法通过 phpmyadmin 在数据库表上看到



我一直在通过https://www.youtube.com/watch?v=2eebptXfEvw辅导的时间戳介于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>

您在准备之后缺少execute语句。

<?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>

我找到了解决方案。

<?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')");

?>

最新更新