从一个表复制和删除数据

时间:2014-01-17 15:23:04

标签: php mysql forms

我使用了这段代码

<?php
$con=mysqli_connect("localhost","willy","12345","mop");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO nominated select * from student where regno = '$_POST[regno]'";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
header("location:form5_1.php");

mysqli_close($con);
?>

并将内容从一个表复制到另一个表,如何移动(从当前表中删除并移动到另一个表)?

2 个答案:

答案 0 :(得分:0)

1)复制记录

$sql1="INSERT INTO nominated select * from student where regno = ".intval($_POST["regno"]);

2)删除原来的

$sql2="DELETE from student where regno = ".intval($_POST["regno"]);

执行两个查询。请记住在执行此操作之前清理POST变量。

修改

你问过如何执行它们,这是你如何使用你自己的代码

来做到这一点
$sql1="INSERT INTO nominated select * from student where regno = ".intval($_POST["regno"]);
if (!mysqli_query($con,$sql1))
{
 // your error checking here
}
else
{
    $sql2="DELETE from student where regno = ".intval($_POST["regno"]);
    if (!mysqli_query($con,$sql2)
    {
      // your error checking here
    }
}

答案 1 :(得分:0)

MOVE SQL语句不存在。您必须将所需记录插入新表中并将其从旧表中删除。