equipment_id int(11)
room_id int(11)
equipment int(11)
qty int(11)
price int(11)
这是我的桌子
{{1}}
我要检查房间ID的设备是否存在回显“此设备已存在,请添加其他设备。”如果设备不存在,请正常插入。在插入之前还有其他方法可以检查是否存在记录,请帮助我。我上面的代码无法正常工作。
答案 0 :(得分:0)
您已经将equipment
设为$_POST['cr_equipment']
,然后直接将其放入查询中。整个代码应如下所示:
$sql_check = "SELECT * FROM tblroom_equipment WHERE room_id = " . $_POST['room_id'] . " AND equipment = '" . $_POST['cr_equipment'] . "'";
$query_check = $connect->query($sql_check);
$row = mysqli_fetch_array($query_check);
if (empty($row)) {
$sql = "INSERT INTO tblroom_equipment (room_num, equipment, qty, price) VALUES ('$a', '$b', '$c', '$d')";
$query = $connect->query($sql);
} else {
echo "The equipment already exists.";
}