php脚本将POST保存到数据库并发送电子邮件

时间:2013-09-08 10:59:38

标签: php mysql post

我试图编写一个php脚本,它将获取POSTed变量并将它们保存到数据库,然后发送包含这些变量的电子邮件。

我把以下单独使用过的方法放在一起,没有问题。当我在页面上收到以下警告时,我在某处犯了一个错误: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')'。

我没有来这里?

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

<?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);

}

?>

我的数据库:

enter image description here

1 个答案:

答案 0 :(得分:2)

我能看到的唯一可能导致此问题的是以下行中的额外);

('$_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]')";