PHP T_ENCAPSED_AND_WHITESPACE

时间:2012-10-11 01:03:35

标签: php mysql database forms

我试图看到它有什么问题它说它期待一个']'但是我没有看到我的mySQL中存在的问题就是它说它搞乱了。

('$_POST[first_name]','$_POST[last_name]','$_POST[email]','$_POST[phone_number]','$_POST[event_name]','$_POST[event_category]','$_POST[event_ description]','$_POST[event_admission]','$_POST[event_amount]',$event_start_date,$event_end_date,'$_POST[event_location]','$_POST[event_family]')";

以下是完整代码:

$sql="INSERT INTO events_main (first_name, last_name, email, phone_number, event_name, event_category, event_description, event_admission, event_amount, event_start_date, event_end_date, event_location, event_family)VALUES('$_POST[first_name]','$_POST[last_name]','$_POST[email]','$_POST[phone_number]','$_POST[event_name]','$_POST[event_category]','$_POST[event_description]','$_POST[event_admission]','$_POST[event_amount]','$event_start_date','$event_end_date','$_POST[event_location]','$_POST[event_family]')";

1 个答案:

答案 0 :(得分:2)

您收到错误的原因是event_ description;空间不应该在那里。

但是,在将数据保存到数据库之前,应该使用类似的东西来正确转义数据:

$escaped_values = join(',', array_map(function($v) {
    return "'" . mysql_real_escape_string($v) . "'";
}, array(
    $_POST['first_name'],
    $_POST['last_name'],
    $_POST['email'],
    $_POST['phone_number'],
    $_POST['event_name'],
    $_POST['event_category'],
    $_POST['event_description'],
    $_POST['event_admission'],
    $_POST['event_amount'],
    $event_start_date,
    $event_end_date,
    $_POST['event_location'],
    $_POST['event_family'],
)));

$sql = "INSERT INTO .... VALUES ($escaped_values)";

准备好的陈述变得更加容易,因为逃脱已经为你完成了:)