-1

我想从第一个查询中插入一条记录,我的意思是行然后用秒查询插入。有人能帮助我吗

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";
}

?>
4

2 回答 2

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']}'");
于 2013-07-08T15:02:09.913 回答
0

另一种方法是使用一个插入到一个选择

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'");

将这两个查询包装在一个事务中可能是一个好主意,以确保在两个查询之间不会将任何内容添加到产品表中。

完整的代码是

  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);
}
于 2013-07-08T15:10:20.223 回答