我想从第一个查询中插入一条记录,我的意思是行然后用秒查询来插入。 有人可以帮助我吗
if ( isset($_GET['del']) ) {
$id = $_GET['del'];
$result = mysqli_query($con,"SELECT * FROM products WHERE id='$id'");
while($row = mysqli_fetch_array($result))
{
echo $row['username'] . " " . $row['size'];
echo "<br>";
}
$sql_move = mysqli_query($con,"INSERT INTO history (username, serial, image, arrive,size)");
$sql = mysqli_query($con,"DELETE FROM products WHERE id='$id'");
echo "its deleted";
}
?>
答案 0 :(得分:4)
首先,您尝试从循环外部访问$row
,但它并不存在。将插入/删除行放在循环中,然后继续。
其次,您没有插入值。
$sql_move = mysqli_query($con, "INSERT INTO history (username, serial, image, arrive,size) VALUES ('{$row['username']}', '{$row['serial']}', '{$row['image']}', '{$row['arrive']}', '{$row['size']}'");
答案 1 :(得分:0)
另一种方法是使用select
插入mysqli_query($con,"insert into history select username, serial, image,arrive,size from products where id = '$id'");
然后从产品中删除
mysqli_query($con,"delete from products where id = '$id'");
在事务中包装这两个查询可能是个好主意,以确保在两个查询之间没有任何内容添加到product表中。
完整代码将是
if ( isset($_GET['del']) ) {
$id = intval($_GET['del']);
$result = mysqli_query($con,"SELECT * FROM products WHERE id=$id");
while($row = mysqli_fetch_array($result))
{
echo $row['username'] . " " . $row['size'];
echo "<br>";
}
mysqli_autocommit($con, false);
mysqli_query($con,"insert into history select username, serial, image,arrive,size from products where id = $id");
mysqli_query($con,"delete from products where id = $id");
mysqli_commit($con);
}