php—将数据行从一个表归档到另一个表

ffvjumwh  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(359)

希望通过将记录从一个表移动到另一个表来存档患者。这是我尝试使用的代码:

<?php

$patient_id = $_GET['patient_id'];
include("db.php");

$sql="Select * from patient where patient_id=".$patient_id;
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);

//Call the function to archive the table
//Function definition is given below
archive_record(patient,$row);

//Once you archive, delete the record from original table

$sql = "Delete from patient where patient_id=".$patient_id;
mysqli_query($conn,$sql);

function archive_record($patient_archive,$row)
{
	$sql = "insert into patient_archive values(";
	$i=0;
	while($i<(count($row)-1))
	{
		$sql.="'".$row[$i]."',";
	}
	$i=$i+1;

	$sql.="'".$row[$i]."'";
	$sql.=")";

}
?>

我在运行代码enter image description here后得到这个结果
如何改进此代码以获得所需的结果?
调试后出错请在此处输入图像描述

f3temu5u

f3temu5u1#

可以使用此函数复制行:

function archive_record($patient_id)
{
    $sql = "INSERT INTO patient_archive (field1, field1, ...) SELECT t.field1, t.field1, .... FROM patient t WHERE t.patient_id = '$patient_id'";
    mysqli_query($conn,$sql);

    //Ten you can write your delete query here  
}

相关问题