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

时间:2018-10-21 08:38:52

标签: php mysql archive

想通过将记录从一张桌子移到另一张桌子来对患者进行存档。这是我要使用的代码:

<?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

如何改进此代码以获得所需的结果?

调试后错误 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用此功能复制该行:

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  
}