duanqing3026
2016-02-10 20:15
浏览 93
已采纳

MySQL INSERT与PHP中的外键问题

I am using PDO to insert values into my table like this:

$query = "INSERT INTO Maps(meetingId, street, city, code, centerLat, centerLon, zoom, markerVisible, markerLat, markerLon) VALUES (:meetingId, :street, :city, :code, :centerLat, :centerLon, :zoom, :markerVisible, :markerLat, :markerLon)";

$paramArr = array(
    ":meetingId" => intval($mapInfo['meetingId']),
    ":street" => $mapInfo['street'],
    ":city" => $mapInfo['city'],
    ":code" => $mapInfo['code'],
    ":zoom" => $mapInfo['zoom'],
    ":centerLat" => $mapInfo['center']['lat'],
    ":centerLon" => $mapInfo['center']['lon'],
    ":markerVisible" => $mapInfo['marker']['visible'],
    ":markerLat" => $mapInfo['marker']['lat'],
    ":markerLon" => $mapInfo['marker']['lon']
);

$db = $this->databaseManager ->getDB();
$query = $db->prepare($query);

foreach ($paramsArray as $key => $value) {
    $query->bindParam($key, $value, PDO::PARAM_INT);
}

When I execute this query I get:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint

meetingId is foreign key but I add a key that I am 100% sure exists in te relevant table. This key is of type int.

On the other hand if I remove the first variable and type in proper id in its place (that again I am sure exists) I get

SQLSTATE[HY000]: General error

Am I missing something here?

图片转代码服务由CSDN问答提供 功能建议

我正在使用PDO将值插入到我的表中,如下所示:

   $ query =“INSERT INTO Maps(meetingId,street,city,code,centerLat,centerLon,zoom,markerVisible,markerLat,markerLon)VALUES(:meetingId,:street,:city,:code,:centerLat,:centerLon  ,:zoom,:markerVisible,:markerLat,:markerLon)“; 
 
 $ paramArr = array(
”:meetingId“=> intval($ mapInfo ['meetingId']),
”:street“  => $ mapInfo ['street'],
“:city”=> $ mapInfo ['city'],
“:code”=> $ mapInfo ['code'],
“:zoom  “=> $ mapInfo ['zoom'],
”:centerLat“=> $ mapInfo ['center'] ['lat'],
”:centerLon“=> $ mapInfo ['center']  ['lon'],
“:markerVisible”=> $ mapInfo ['marker'] ['visible'],
“:markerLat”=> $ mapInfo ['marker'] ['lat'],  
“:markerLon”=> $ mapInfo ['marker'] ['lon'] 
); 
 
 $ db = $ this-> databaseManager  - > getDB(); 
 $ query =  $ db-> prepare($ query); 
 
foreach($ paramsArray as $ key => $ value){
 $ qu  ery-> bindParam($ key,$ value,PDO :: PARAM_INT); 
} 
   
 
 

当我执行此查询时,我得到:

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束

meetingId是外键但我在相关表中添加了100%确定存在的密钥。 这个键是int类型。

另一方面,如果我删除第一个变量并在其位置键入正确的id(我再次确定存在)我得到

SQLSTATE [HY000]:常规错误

我在这里遗漏了什么吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题