-1

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

$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...)
4

1 回答 1

0

是的。

我建议你做一个 print $q; 查看代码是否生成正确的 sql。

于 2012-11-01T23:38:01.733 回答