【问题标题】:archiving data row from a table to another将数据行从一个表归档到另一个表
【发布时间】:2018-10-21 08:38:52
【问题描述】:

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

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

【问题讨论】:

  • 首先纠正这个:archive_record(patient,$row); - '耐心'应该是什么? $patient_archive 未在函数 archive_record() 中使用
  • second: $i=$i+1; 在 while 之外,所以 while 将永远运行,这是您的第二个错误。
  • 第三:你永远不会执行在archive_records中创建的$sql
  • 第四:您的代码高度不安全,对 sql 注入开放。切换到准备好的语句或至少清理用户输入 ($_GET['patient_id'])。
  • 请将错误作为文本添加到问题中,而不是图像

标签: php mysql archive


【解决方案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  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    相关资源
    最近更新 更多