使用相同ID的多个MySqli插入

时间:2017-07-04 10:29:14

标签: php mysql mysqli

下面是我的php代码,$ db是与db的连接

<?php

if (! hasEmpty([$_POST['loc-city']])) {
    $city = htmlspecialchars_decode($_POST['loc-city']);
    $addr = htmlspecialchars_decode($_POST['loc-address']);
    $prov = htmlspecialchars_decode($_POST['loc-province']);
    $pos = htmlspecialchars_decode($_POST['loc-poscode']);

    $houseID = mysqli_insert_id($db);

    $sql = "INSERT INTO house (houseID, username, houseStatus) VALUES ('".$houseID."', '".$_SESSION['login_user']."','0')";
    $sql2 = "INSERT INTO house_loc (houseID, city, address, province, postalCode) VALUES ('".$houseID."',?,?,?,?)";

    $query = $db->prepare($sql);
    $query2 = $db->prepare($sql2);
    $query2->bind_param('ssss',$city,$addr,$prov,$pos);

    if (!$query->execute()) {
      echo '{"status":"error", "errorMsg" : "Error!"}';
      exit;
    }
    if (!$query2->execute()) {
      echo '{"status":"error", "errorMsg" : "Error!"}';
      exit;
    }

    echo '{"status":"success"}';
?>

我遇到将两个语句都插入数据库的问题,我尝试将$ houseID直接更改为int并且它可以工作,所以我认为问题发生在

$houseID = mysqli_insert_id($db);

因为不能两次使用?

0 个答案:

没有答案