我正在使用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.";
}
}
答案 0 :(得分: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']);
答案 1 :(得分: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']);