2

我正在使用 PHP 和 PDO 将 API 的结果插入 mysql DB (innodb)。这是我的代码:

 $insertbrewery = $db->prepare('insert into breweries values(DEFAULT, :api_id, :name, :city, :state, DEFAULT)');

foreach ($data as $result) {

$insertbrewery->bindParam('api_id', $id);
$insertbrewery->bindParam('name', $result[brewery][name]);
$insertbrewery->bindParam('city', $result[brewery][locality]);
$insertbrewery->bindParam('state', $result[brewery][region]);

$insertbrewery->execute();

if ($insertbrewery) {
  echo $insertbrewery->rowcount()." breweries successfully submitted.<br />";

} else {
  echo "An error occurred with submitting the post to the database. Sorry.";
}

}
4

2 回答 2

3

您的参数名称是:name,:city:state(包括前导冒号)。另外,请务必在数组中引用您的键。

改变这个:

$insertbrewery->bindParam('name', $result[brewery][name]);
$insertbrewery->bindParam('city', $result[brewery][locality]);
$insertbrewery->bindParam('state', $result[brewery][region]);

对此:

$insertbrewery->bindParam(':name', $result['brewery']['name']);
$insertbrewery->bindParam(':city', $result['brewery']['locality']);
$insertbrewery->bindParam(':state', $result['brewery']['region']);
于 2013-09-23T18:13:28.407 回答
1

错误的数组语法:

$insertbrewery->bindParam('name', $result[brewery][name]);
$insertbrewery->bindParam('city', $result[brewery][locality]);
$insertbrewery->bindParam('state', $result[brewery][region]);

应该:

$insertbrewery->bindParam('name', $result['brewery']['name']);
$insertbrewery->bindParam('city', $result['brewery']['locality']);
$insertbrewery->bindParam('state', $result['brewery']['region']);
于 2013-09-23T18:12:45.393 回答