-1

我是使用 PDO 的新手,我不知道它有什么问题。它说我有这个错误解析错误:语法错误,意外的 T_ENCAPSED_AND_WHITESPACE,在第 3 行期待 T_STRING 或 T_VARIABLE 或 T_NUM_STRING 这是我的代码。

<?php
include 'dbconnect.php';
$sql = "INSERT INTO event (name,location,admission,map,additional,featured,description)    VALUES (:name,:location,:admission,:map,:additional,:featured,:description)";
$q = $conn->prepare($sql);
$q->execute(array(':name'=>$_POST['name'],
      ':location'=>$_POST['location'],
      ':admission'=>$_POST['addmission'],
      ':map'=>$_POST['map'],
      ':additional'=>$_POST['additional'],
      ':featured'=>$_POST['featured'],
      ':description'=>$_POST['description']));
?>
4

2 回答 2

1

从所有这些$_POST变量中删除双引号。没有必要引用它们,如果你也引用它们的数组键,确实会造成伤害。

$name= $_POST['name'];
$location =  $_POST['location'];
$admission =    $_POST['admission'];
$map =   $_POST['map'];
$additional  =  $_POST['additional'];
$featured  =  $_POST['feature'];
$description =   $_POST['description'];

在双引号字符串中,如果引用数组键,则不得引用键,或将整个内容括在{}. 但这里两者都不是必需的。

echo "In a double quoted string, you can use $array[keys] unquoted, or surround quoted {$array['keys']} in {}";
// But this is a syntax error! (like you are getting)
echo "Don't quote the $array['keys'] if inside a double quote string, not wrapped in {}";

事实上,根本没有必要将它们分配给变量。只需在调用$_POST中直接使用输入即可。execute()

$q->execute(array(':name'=>$_POST['name'],
          ':location'=>$_POST['location'],
          ':admission'=>$_POST['addmission'],
          ':map'=>$_POST['map'],
          ':additional'=>$_POST['additional'],
          ':featured'=>$_POST['featured'],
          ':description'=>$_POST['description']));
于 2012-12-11T03:04:14.453 回答
0

该错误可能是指在 $_POST 变量周围使用双引号。

错误是第 3 行的语法,似乎与此问题的标题无关。

$name = $_POST['name'];

至于 PDO,您必须在包含的 dbconnect.php 文件中包含此类代码,因为在问题中看不到与 PDO 相关的代码。

于 2012-12-11T03:13:34.423 回答