pdo :: execute($ array)没有在表中没有错误

时间:2012-11-01 23:35:28

标签: php pdo execute

我目前正在使用此代码,并且没有错误,它只是没有将它添加到表中。

$fields["firstName"] = $_POST['firstName']; 
$fields["middleName"] = $_POST['middleName'];
$fields["lastName"] = $_POST['lastName']; 
$fields["suffix"] = $_POST['suffix'];
$fields["age"] = $_POST['age'];
.....etc


$t = array();
foreach($fields as $k=>$v)
{
$count++;
$n .= $k;
$u .= $k;
if($count != count($fields))
{
    $n .= ", ";
    $u .= ", :";
}
$s = ":".$k;
$t[$s]= $v;
}

 $q="INSERT INTO search_requests SET (".$n.") VALUE(:".$u.")";

 $prep=$conn->prepare($q);
 $prep->execute($t);

你可以使用这样的数组,还是必须在执行函数中写出每个变量

 $prep->execute(array(':firstName=>$_POST['firstName'], etc..));

sql代码

INSERT INTO search_requests SET (user_email, datestamp, historicalMatches, 
firstName, middleName, lastName, suffix, etc...) VALUE(:user_email, 
:datestamp, :historicalMatches, :firstName, :middleName, :lastName, :suffix,etc...)

他们都匹配。

Array ( [:user_email] => Email@domain.com [:datestamp] => 02 Nov 2012 10.24.39 AM   
[:historicalMatches] => [:firstName] => First [:middleName] => m [:lastName] => lastname 
[:suffix] => etc...)

1 个答案:

答案 0 :(得分:0)

是。

我建议你打印$ q;查看代码是否生成正确的sql。