无法使用php mysql更新和插入数据

时间:2016-09-05 10:24:43

标签: php mysql mysqli-multi-query

使用php mysql更新和插入数据时遇到问题。这个系统的过程是当有人点击nama材料组合框形式时,它会显示来自" barang"表。然后用户将填写" jumlah"和" satuan"输入表格。如果用户想要添加更多&n 34材料,jumlah和satuan",他们可以点击" tambah barang"按钮。

主要问题是" satuan"无法更新到" barang"表格和显示错误如下: 警告:mysqli_num_rows()要求参数1为mysqli_result,布尔值在第23行的D:\ xampp \ htdocs \ sime \ perpindahan_add_proses.php中给出

警告:mysqli_num_rows()期望参数1为mysqli_result,第23行的D:\ xampp \ htdocs \ sime \ perpindahan_add_proses.php中给出布尔值 更新记录时出错:命令不同步;你现在不能运行这个命令

但如果我只插入1个数据/" nama材料",它会显示: 更新记录时出错:命令不同步;你现在不能运行这个命令

这是我的代码:

$no_berita  = mysqli_real_escape_string($conn, $_POST['no_berita']);
$hari       = mysqli_real_escape_string($conn, $_POST['hari']); 
$tgl        = mysqli_real_escape_string($conn, $_POST['tanggal']);
$pihak1     = mysqli_real_escape_string($conn, $_POST['pihak1']);
$pihak2     = mysqli_real_escape_string($conn, $_POST['pihak2']);
$lokasi     = mysqli_real_escape_string($conn, $_POST['lokasi']);

$detail = $_POST['detail'];
for($i=0; $i<sizeof($detail['id_brg']); $i++)
{         
    $sql = "SELECT * FROM barang WHERE id_brg = ".$detail['id_brg'][$i];
    $result = mysqli_query($conn, $sql);

    if(mysqli_num_rows($result) >0)
    {   
        $detail_id_brg = $detail['id_brg'][$i];
        $sql = "UPDATE barang SET  lokasi    = '$lokasi' WHERE id_brg    = '".$detail['id_brg'][$i]."';
          INSERT INTO perpindahan_detail (no_berita,id_brg_pindah,jumlah_detail,satuan_detail) 
          VALUES ('".$no_berita."', '".$detail['id_brg'][$i]."', ".$detail['jumlah'][$i].",  
                  '".$detail['satuan'][$i]."')";
        mysqli_multi_query($conn, $sql);         
    }
}

$sql = "INSERT INTO perpindahan (no_berita,hari,tgl,pihak1,pihak2,lokasi) 
                       VALUES ('$no_berita','$hari','$tgl','$pihak1','$pihak2','$lokasi')";
if(mysqli_query($conn, $sql)) 
{
    echo "<script>alert('Insert data berhasil! Klik ok untuk melanjutkan');location.replace('perpindahan_list.php')</script>";
} 
else 
{
    echo "Error updating record: " . mysqli_error($conn);
}

任何帮助将不胜感激

0 个答案:

没有答案