PHP插入准备好的声明

时间:2018-06-05 06:08:57

标签: php mysqli

我通过不同的帖子查看准备好的陈述。我收到以下错误

  

错误:无法准备查询:INSERT INTO contact(,,)VALUES(?,   ?)。您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近',,,,)VALUES(?,?)'在第1行

我似乎无法弄清楚为什么我会收到此错误。我在网上找到的一切都没有用。我希望有人能指出我正确的方向。

--allow-file-access-from-files

谢谢,

1 个答案:

答案 0 :(得分:1)

找到了这个问题的答案。

<?php
 $servername = "mysql";
 $username = "root";
 $password = "passwrd";
 $dbname = "dbname";

 try {
 $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
    $password);
 // set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO tablename (name, email, commtype, 
comment, confirm) 
VALUES (:name, :email, :commtype, :comment, :confirm)");
  $stmt->bindParam(':name', $name);
  $stmt->bindParam(':email', $email);
  $stmt->bindParam(':commtype', $commtype);
  $stmt->bindParam(':comment', $comment);
  $stmt->bindParam(':confirm', $confirm);

// insert a row
   $name = $_POST['name'];
   $email = $_POST['email'];
   $commtype = $_POST['commtype'];
   $comment = $_POST['comment'];
   $confirm = $_POST['confirm'];
   $stmt->execute();


   echo "New records created successfully";
   }
   catch(PDOException $e)
   {
   echo "Error: " . $e->getMessage();
    }
   $conn = null;
    ?>
相关问题