mysql_insert_id
当我想将新数据从 html 表单插入到多个表中时,我对使用该函数有疑问?我使用的表格是这样的:
<form method="POST" action="insert.php">
<table>
<tr>
<td>Room</td>
<td>
<select name="room">
<?php
openDB();
$sql = "SELECT room_id, name FROM room";
$rs = executeDB($sql);
while(list($room_id, $name) = mysql_fetch_array($rs)) {
echo "<option value='$room_id'>$name</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Product</td>
<td>
<select name="product">
<?php
openDB();
$sql = "SELECT product_id, name FROM product";
$rs = executeDB($sql);
while(list($product_id, $name) = mysql_fetch_array($rs)) {
echo "<option value='$product_id'>$name</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Quantity</td>
<td>
<?php
openDB();
$sql = "SELECT quantity FROM items_order";
$rs = executeDB($sql);
while(list($quantity ) = mysql_fetch_array($rs)) {
echo " <input type='number' name='quantity []' min='1' max='4'/></td>";
}
?>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
然后输入这些变量:
if(isset($_POST['user'])) {
$user= $_POST['user'];
$room_id = $_POST['room_id'];
$product_id = $_POST['product_id'];
$quantity= $_POST['quantity'];
$status = 0; // if it's payed or not, if it is status= 1 else status = 0
这是代码:
$sql = "INSERT INTO order (status, room_id, user_id) VALUES ('$status','$room_id','$user_id')";
$rs = mysql_query($sql);
$newid = mysql_insert_id();
$sql2 ="INSERT INTO items_order VALUES ('$newid ','$product_id','$quantity')";
$rs = mysql_query($sql2);
它不起作用。
我的桌子是;order , items_order, product, order 有 order_id 主键作为自增,items_order 有两个主键 order_id 和 product_id 因为它们是外键,room 有 room_id,product 有 product_id,order 表中的外键是user_id, room_id, 为。我是否必须使用该功能才能输入新数据?最重要的是我做错了什么,这段代码不起作用?请帮忙。