PHP脚本将POST保存到数据库并发送电子邮件 [英] php script to save POST to database and send email

查看:238
本文介绍了PHP脚本将POST保存到数据库并发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个php脚本,将接收POSTed变量并将其保存到数据库中,然后发送包含这些变量的电子邮件.

Im trying to write a php scrip that will take POSTed variables and save them to a database then send an email with those variables.

我已经使用下面我分别使用过的方法整理了以下内容,没有出现问题.在页面上收到以下警告时,我在这里某个地方犯了一个错误:Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ws')' at line 16

Ive put together the below using methods I have used separately before without issue. I've made a mistake somewhere here thoug as I get the following warning on the page: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ws')' at line 16

我不理解此警告,因为在第16行或附近没有"ws')'.

I dont understand this warning as there is no ''ws')' at or near line 16.

我什么不来?

我拿出了所有保存到数据库的代码,并且电子邮件工作正常,但是我仍然看不到数据库代码有什么问题.

I took out all of the code for saving to the database and the email worked fine but I still cant see whats wrongwith the database code.

<?php 

$email = $_POST['email'] ;
$number = $_POST['number'] ;
$ext = $_POST['ext'] ;
$startDay = $_POST['startDay'] ;
$endDay = $_POST['endDay'] ;
$startTime = $_POST['startTime'] ;
$endTime = $_POST['endTime'] ;
$startDay2 = $_POST['startDay2'] ;
$endDay2 = $_POST['endDay2'] ;
$startTime2 = $_POST['startTime2'] ;
$endTime2 = $_POST['endTime2'] ;
$sine = $_POST['sine'] ;
$webmaster_email = "support@website.com";
$hostname = "Sines.db.42.mywebresource.com";
$username = "Sines";
$dbname = "Sines";
$password = "Password!";
$usertable = "contact";



$con = mysql_connect($hostname, $username, $password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbname, $con);

$sql="INSERT INTO contact (
`email` ,
`number` ,
`ext` ,
`startDay` ,
`endDay` ,
`startTime` ,
`endTime` ,
`startDay2` ,
`endDay2` ,
`startTime2` ,
`endTime2` ,
`sine`
)
VALUES
('$_POST[email]','$_POST[number]','$_POST[ext]','$_POST[startDay]','$_POST[endDay]','$_POST[startTime]','$_POST[endTime]','$_POST[startDay2]','$_POST[endDay2]','$_POST[startTime2]','$_POST[endTime2]'),'$_POST[sine]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "User", ' ' , mysql_insert_id() , ' ' ,"added";
$_SESSION["PRid"] = mysql_insert_id();
mysql_close($con);

/*Below is for sending the email.*/
function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}
if ( isInjected($email) ) {
}
else {
       $contents = <<<EOM
Email: $email
Number: $number
Ext: $ext
StartDay: $startDay
EndDay: $endDay
StartTime: $startTime
EndTime: $endTime
StartDay2: $startDay2
EndDay2: $endDay2
StartTime2: $startTime2
EndTime2: $endTime2
Sine: $sine
EOM;

mail( "$webmaster_email", "Contact", $contents);

}

?>

我的数据库:

推荐答案

我唯一能看到的可能是这是下面一行中的多余)

The only thing I can see that could cause this is the extra ) in the following line;

('$_POST[email]','$_POST[number]','$_POST[ext]','$_POST[startDay]','$_POST[endDay]','$_POST[startTime]','$_POST[endTime]','$_POST[startDay2]','$_POST[endDay2]','$_POST[startTime2]','$_POST[endTime2]'),'$_POST[sine]')";

更改为此;

('$_POST[email]','$_POST[number]','$_POST[ext]','$_POST[startDay]','$_POST[endDay]','$_POST[startTime]','$_POST[endTime]','$_POST[startDay2]','$_POST[endDay2]','$_POST[startTime2]','$_POST[endTime2]','$_POST[sine]')";

这篇关于PHP脚本将POST保存到数据库并发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆