PHP Mysql查询,没有错误,不会插入行

时间:2010-02-04 01:57:49

标签: php mysql

表单正确提交,并向我发送电子邮件。没有报告错误,它创建的SQL工作正常,我在phpMyAdmin测试它。 mysql_error()什么都没引出,它只是不添加一行。谁能看到发生了什么?

<?PHP 

$to = "me@gmail.com";
$subject = "New Lead"; 
$date = date ("l, F jS, Y"); 
$time = date ("h:i A");
$mysql = mysql_connect("db.perfora.net:3306","db","password");

if(!$mysql)
{
    die("Could Not Connect: ".mysql_error());
}

mysql_select_db("db",$mysql);



if ($_SERVER['REQUEST_METHOD'] == "POST") { 
    $name = $_POST['firstname']." ".$_POST['lastname'];
    $email = $_POST['email'];
    $phone = "(".$_POST['areacode'].") ".$_POST['firstthree']."-".$_POST['lastfour'];
    $area = $_POST['area'];
    $lookdate = $_POST['lmm']."/".$_POST['ldd']."/".$_POST['lyyyy'];
    $lookdatedb = date("{$_POST['lmm']}.{$_POST['ldd']}.{$_POST['lyyyy']}");
    $movedate = $_POST['mmm']."/".$_POST['mdd']."/".$_POST['myyyy'];
    $movedatedb = date("{$_POST['mmm']}.{$_POST['mdd']}.{$_POST['myyyy']}");
    $loft = $_POST['loft'] ? "loft" : "";
    $highrise = $_POST['highrise'] ? "highrise" : "";
    $traditional = $_POST['traditional'] ? "traditional" : "";
    $price = $_POST['price'];
    $comments = $_POST['comments'];

$sql = "INSERT INTO Leads 
            (Name, Email, Phone, Area, LookDate, MoveDate, Loft, HighRise, Traditional, Price, Comments) 
            VALUES 
            ('$name', '$email', '$phone', '$area', '$lookdatedb', '$movedatedb', '{$_POST['loft']}', '{$_POST['highrise']}', '{$_POST['traditional']}', '$price', '$comments')";

if (mysql_query($sql,$con))
{
  echo "Row added.";
}
else
{
  echo "Error adding row: " . mysql_error();
  echo("\n\n".$sql);
}


$msg = "
    New Lead Submitted On $date at $time.\n\n

    Name: $name\n
    Email: $email\n
    Phone: $phone\n
    Area: $area\n
    Look Date: $lookdate\n
    Move Date: $movedate\n
    Type: $loft $highrise $traditional \n
    Price: $price\n
    Comments: $comments\n


";  

}
mysql_close($mysql);
mail($to, $subject, $msg, "From:$email"); 
if ($forward == 1) { 
    header ("Location:$location"); 
} 
else { 
    echo "Thank you for submitting our form. We will get back to you as soon as possible."; 
} 

?>

回应:

  

感谢您提交表单。我们会尽快回复您。

生成的SQL:

INSERT INTO Leads (Name, Email, Phone, Area, LookDate, MoveDate, Loft, HighRise, Traditional, Price, Comments) VALUES ('work work', 'work@work.com', '(214) 131-4131', 'dallas', '02.18.2010', '02.25.2010', '', '1', '1', '$333333333333333333', '33fdsdfsdfsd')

数据库结构:

http://imgur.com/iQHRk.jpg

3 个答案:

答案 0 :(得分:2)

让我们看看,您的数据库连接句柄显然是由 $ mysql 引用的,但您已经这样做了:

if (mysql_query($sql,$con))

您的数据库处理程序错误。

答案 1 :(得分:0)

mysql_query($sql,$con);

应该返回一些为什么不看一下

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

最佳做法是尽可能检查错误。

另外需要注意的是,您没有转义任何用户输入,因此您的代码容易受SQL injections攻击。请使用mysql_real_escape_string

答案 2 :(得分:0)

将post变量放在另一个变量中然后传递给insert查询我认为它会起作用 像这样

$ sql =“INSERT INTO Leads             (姓名,电子邮件,电话,区域,LookDate,MoveDate,Loft,HighRise,繁体,价格,评论)             VALUES             ('$ name','$ email','$ phone','$ area','$ lookdatedb','$ movingatedb','$ loft','$ highrise','$ traditional','$ price' ,'$ comments')“;

的mysql_query($ SQL);