gpt4 book ai didi

php - 将表单字段插入数据库

转载 作者:行者123 更新时间:2023-11-29 07:46:50 25 4
gpt4 key购买 nike

我正在使用自定义表单帖子,数据将按照以下代码发送到电子邮件。但是我对数据库的插入语句似乎不起作用。而且,我的数据库中名为“leads”的表仍然是空的。我几乎遵循了以下示例: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);
}

?>

最佳答案

我假设姓名、电话、电子邮件和日期都是字符串类型,如果是字符串则加上引号

$sql = "INSERT INTO leads (name, telephone, email, date)
VALUES ('$name', '$telephone', '$email', '$datesent')";

关于php - 将表单字段插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27518738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com