插入不适用于SQLite PHP

时间:2017-12-14 10:51:56

标签: php sqlite pdo contact

我试图用SQLite数据库创建一个简单的联系表单。

当我提交表单时,此消息显示:

Array ( [title] => Mr [fname] => John [lname] => Smith [email] => john@smith.com [phone] => 0123456789 [message] => test ) E-Mail Sent successfully, we will get back to you soon.Array ( [0] => HY000 [1] => 1 [2] => no such column: test )

它没有发送电子邮件或将数据插入db文件。

PHP:

<?php
$db = new PDO("sqlite:contact.db");

if((isset($_POST['title']) && !empty($_POST['title']))
&& (isset($_POST['fname']) && !empty($_POST['fname']))
&& (isset($_POST['lname']) && !empty($_POST['lname']))
&& (isset($_POST['email']) && !empty($_POST['email']))
&& (isset($_POST['phone']) && !empty($_POST['phone']))){
//if((isset($_POST['name']) && (!empty($_POST['name']))) && (isset($_POST['email']) && !empty($_POST['email'])) && (isset($_POST['subject']) && !empty($_POST['subject']))){
  print_r($_POST);
  $title = $_POST['title'];
   $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $message = $_POST['message'];
  $subject = "Alpine A110";

  $to = "example@example.co.uk";
  $headers = "From : " . $email;

  if( mail($to, $subject, $message, $headers)){
    echo "E-Mail Sent successfully, we will get back to you soon.";

    $db->exec("INSERT INTO contact (title, fname, lname, email, phone, details) VALUES ('$title', '$fname', '$lname', '$email', '$phone', $message);") or die(print_r($db->errorInfo(), true));
  }
}

?>

2 个答案:

答案 0 :(得分:1)

您当前的问题是,您忘记了查询

$message周围的引号
    $db->exec("INSERT INTO contact (title, fname, lname, email, phone, details) VALUES ('$title', '$fname', '$lname', '$email', '$phone', '$message');") or die(print_r($db->errorInfo(), true));

此外,我强烈建议您使用准备好的陈述!

答案 1 :(得分:-1)

替换你的陈述,

$ db-&gt; exec(“INSERT INTO contact(title,fname,lname,email,phone,details)VALUES('$ title','$ fname','$ lname','$ email',' $ phone',$ message);“)或死(print_r($ db-&gt; errorInfo(),true));

$ db-&gt; exec(“INSERT INTO contact(title,fname,lname,email,phone,details)VALUES('$ title','$ fname','$ lname','$ email',' $ phone','$ message');“)或死(print_r($ db-&gt; errorInfo(),true));