我正在使用自定义表单帖子,并根据以下代码将数据发送到电子邮件。但是我对DB的insert语句似乎不起作用。而且,我的数据库中名为“lead”的表仍然是空的。 我几乎遵循了以下示例:http://www.w3schools.com/php/php_mysql_insert.asp 任何指针都会受到赞赏,以使这个sql插件工作,
<?php
error_reporting(E_ALL);
if($_POST){
$to = 'myemailaddres@example.com';
$subject = 'Website Enquiry';
$now = new DateTime();
$datesent=$now->format('Y-m-d H:i:s');
$name = $_POST['name'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$fname = $_POST['fname'];
$username = "usertest";
$password = "pwdtest";
$dbname = "dbtest";
$servername = "localhost";
//spam checks
if ( isset($fname) && !isset($fname) ) {
echo 'spam';
exit();
} elseif ( preg_match("/http/i","$comments") ){
echo '<h1>Spam detected</h1><p>Sorry, no urls allowed in comment box.</p>';
exit();
}
$data = clean($_POST);
if(validate($data) == 0){
$email = $data['email'];
$headers = "From: ".$data['name']." <".$email.">\r\n".
"Bcc: <".$bcc.">\r\n".
"X-Mailer: PHP/" . phpversion();
if($data['form'] == 'quote') {
$subject = 'Quick quote - '.$subject;
}else if($data['form'] == 'enquiry') {
$subject = 'Enquiry Form - '.$subject;
}else{
$subject = 'Enquiry Form - '.$subject;
}
foreach ($data as $k => $v) {
if($k != "Submit" && $k != "form" && $k != "fname" && $k != "quiz" && $k != "captcha_code"){
$k = str_replace ( "_" , " " , $k );
$message .= $k.": ".$v."\n\r";
}
}
if(mail($to, $subject, $message, $headers)){
/****** TEST SEND TO DB ***********/
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO leads (name, telephone, email, date)
VALUES ($name, $telephone, $email, $datesent)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
/********* END TEST SEND TO DB *****/
header("Location: /thank-you");
} else {
//echo 'Mail Error';
header("Location: /error");
}
}else{
//echo 'Validate Error';
header("Location: /error");
}
}else{
//echo 'Post Error';
header("Location: /error");
}
function clean($post){
$remove = array('vin');
$data = array();
foreach($post as $key => $value){
if(in_array($value,$remove)){
$data[$key] = '';
}else{
$data[$key] = $value;
}
}
return array_filter($data);
}
function validate($data){
$error = array();
if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $data['email'])) {
// $error[] = '1';
}
if ((strlen($data['name']) < 1) || (strlen($data['name']) > 32)) {
//$error[] = '1';
}
if ((strlen($data['telephone']) < 10)) {
$error[] = '1';
echo 'Please enter 10 digit numbers for telephone number';
}
if(isset($data['form'])){
if($data['form'] == 'quote'){
if ((strlen($data['address']) < 1)) {
// $error[] = '1';
}
}
}
return count($error);
}
?>
答案 0 :(得分:2)
我假设姓名,电话,电子邮件和日期都是字符串类型,如果是字符串
则加上引号$sql = "INSERT INTO leads (name, telephone, email, date)
VALUES ('$name', '$telephone', '$email', '$datesent')";
答案 1 :(得分:0)
用户在连接php变量和字符串时纠正字符串连接的方法。
$sql = "INSERT INTO leads (name, telephone, email, date)
VALUES ('".$name."', '".$telephone."', '".$email."', '".$datesent."')";