为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 简单的 INSERT 数据库

PHP 简单的 INSERT 数据库

PHP
人到中年有点甜 2022-11-04 17:20:00
我无法弄清楚我的 PHP 有什么问题,但 INSERT 似乎总是失败。当我提交表单时,什么也没有发生,也没有任何错误消息发送到我的数据库<form action="" method="POST">  <div class="form-group mt-4">    <label for="exampleInputEmail1">Title</label>    <input type="text" class="form-control" name="title" aria-describedby="emailHelp" required>  </div>  <div class="form-group">    <label for="exampleInputPassword1">Products to buy</label>    <textarea class="form-control" name="products" rows="3" required></textarea>  </div>  <div class="form-group">    <label for="exampleInputPassword1">Comments</label>    <input type="text" class="form-control" name="comments" aria-describedby="emailHelp" required>  </div>  <button type="submit" class="btn btn-info">Submit</button></form><?php $title = null;if (isset($_POST['title'])) {    $title = htmlspecialchars($_POST['title']);}$products = null;if (isset($_POST['products'])) {  $products = htmlspecialchars($_POST['products']);}$comments = null;if (isset($_POST['comments'])) {  $comments = htmlspecialchars($_POST['comments']);}$pdo = new PDO('mysql:host=localhost;dbname=liste_course;charset=utf8', 'root', '', [  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);$sql = "INSERT INTO content(id, title_content, products_content, comments_content) VALUES('','$title','$products','$comments')";?>
查看完整描述

1 回答

?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

您可以尝试以这种方式使用您的查询


$pdo = new PDO('mysql:host=localhost;dbname=liste_course;charset=utf8', 'root', '', [

  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,

  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC

]);

//$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$query = $pdo->prepare("INSERT INTO content(id, title_content, products_content, comments_content) VALUES('','$title','$products','$comments')");

$query->execute();

绑定参数也更安全


$query = $pdo->prepare("INSERT INTO content(id, title_content, products_content, comments_content) VALUES(:id,:title,:products,:comments)");

$query->bindParam(":id", $id);

$query->bindParam(":title", $title);

$query->bindParam(":products", $products);

$query->bindParam(":comments", $comments);

$query->execute();


查看完整回答
反对 回复 2022-11-04
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信