php - 将mysql中具有唯一引用的记录从一个表复制到另一个表

标签 php mysql sql

正如标题所示,我想将 mysql 中具有唯一引用的记录从一个表移动到另一个表。

我自己尝试了许多不同的方法以及其他成员建议的方法。

我将向您展示我想要的两页内容。一个是第一个表中的记录表。每行都有一个链接,用于将该行的数据从该表复制到另一个表。我只是无法让代码在第二页上运行以将数据复制到另一个表。

附:完成后我将检查所有代码以尝试查找 SQL 注入(inject)。

请有人尝试帮助我解决这个问题,因为它让我发疯。

感谢您提前抽出时间。

表.php

<?php

require_once('auth.php');

 $host=""; // Host name 
 $username=""; // Mysql username 
 $password=""; // Mysql password 
 $db_name=""; // Database name 
 $tbl_name="Instruction"; // Table name 

// Connect to server and select database.
 mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM Instruction";

$result=mysql_query($sql);
 ?>
<style type="text/css">
body {
background-color: #FFF;
}
</style>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table width="1400" border="0" cellspacing="1" cellpadding="0">
  <tr>
    <td width="1400"><table width="1270" border="1" cellspacing="0" cellpadding="3">
      <tr>
        <td colspan="400"><p align="center"><strong>List of Instructions</strong></p></td>
      </tr>
      <tr>
        <td width="70" align="center"><strong>Reference</strong></td>
        <td width="120" align="center"><strong>Forename</strong></td>
        <td width="120" align="center"><strong>Surname</strong></td>
        <td width="100" align="center"><strong>DOB</strong></td>
        <td width="120" align="center"><strong>Mobile Number</strong></td>
        <td width="120" align="center"><strong>Home Number</strong></td>
        <td width="120" align="center"><strong>Address</strong></td>
        <td width="100" align="center"><strong>Postcode</strong></td>
        <td width="100" align="center"><strong>Email</strong></td>
        <td width="100" align="center"><strong>Accident Date</strong></td>
        <td width="120" align="center"><strong>Accident details</strong></td>
        <td width="120" align="center"><strong>Refer for Triage</strong></td>
        <td width="120" align="center"><strong>Refer for IA</strong></td>
      </tr>
      <?php
 while($rows=mysql_fetch_array($result)){
 ?>
      <tr>
        <td><? echo $rows['Reference']; ?></td>
        <td><? echo $rows['Forename']; ?></td>
        <td><? echo $rows['surname']; ?></td>
        <td><? echo $rows['DOB']; ?></td>
        <td><? echo $rows['Mobile']; ?></td>
        <td><? echo $rows['Home']; ?></td>
        <td><? echo $rows['Address ']; ?></td>
        <td><? echo $rows['Postcode1']; ?></td>
        <td><? echo $rows['Email']; ?></td>
        <td><? echo $rows['Accident']; ?></td>
        <td><? echo $rows['Details']; ?></td>
        <td align="center"><a href="refertotriage.php?Reference=<? echo $rows['Reference']; ?>">Refer for Triage</a></td>
        <td align="center"><a href="update.php?Reference=<? echo $rows['Reference']; ?>">Refer for IA</a></td>
      </tr>
      <?php
 }
 ?>
    </table></td>
  </tr>
</table>
<p>
  <?php
 mysql_close();
 ?>
</p>
<p>&nbsp;</p>

逻辑/code.php

<?php

    require_once('auth.php');

$host=""; // Host name 
$username=""; // Mysql username
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name="Instruction"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");


//$Reference=$_REQUEST['Reference'];
//$Forename=$_REQUEST['Forename'];
//$surname=$_REQUEST['surname'];
//$DOB=$_REQUEST['DOB'];
//$Mobile=$_REQUEST['Mobile'];
//$Home=$_REQUEST['Home'];
//$Address=$_REQUEST['Address'];
//$Postcode=$_REQUEST['Postcode1'];
//$Email=$_REQUEST['Email'];
//$Accident=$_REQUEST['Accident'];
//$Details=$_REQUEST['Details'];


//semi colon removed  


// $sql="INSERT INTO 
//     Triage 
//   (
//   Reference, 
//       Forename,
//       surname,
//       `D.O.B`,
//       `Mobile Number`,
//       `Home Number`,
//       Address,
//       Postcode1,
//       Email,
//       Accident,
//       Details
//      )
//     VALUES
//  (
//    '".$Reference."',
//    '".$Forename."',
//    '".$surname."',
//    '".$DOB."',
//    '".$Mobile."',
//    '".$Home."',
//    '".$Address."',
//    '".$Postcode1."',
//    '".$Email."',
//    '".$Accident."',
//    '".$Details."'
// )";

// $result=mysql_query($sql);

//$sql="INSERT INTO Triage (Reference, Forename) 
//SELECT Reference, Forename from `Instruction` WHERE Reference='$Reference'"

$Reference=mysql_real_escape_string($_GET['Reference']);

$sql="INSERT INTO Triage (Reference, Forename)  
SELECT Reference, Forename FROM `Instruction` 
WHERE Reference='$Reference' LIMIT 1";

// echo "Successful";
// echo "<BR>";
// echo "<a href='list_records.php'>View result</a>";
// mysql_error()

echo $sql;

 ?> 

最佳答案

我自己已经解决了我遇到的问题。我并不是“获取引用资料,这样他们就不知道需要移动哪些记录。”

关于php - 将mysql中具有唯一引用的记录从一个表复制到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22143212/

相关文章:

mysql - 将纬度、经度的 MYSQL 5.7 点表示形式插入表中

php - 显示 session 数组中的所有项目。仅显示顶部项目

php - 我无法从数据库中检索值

sql - 在sql中对连续时间间隔进行分组

php - 重新生成 "random"MySQL 查询

php - 在数据库中查询后,使用 PHP 在 Laravel 中正确获取数据

php - 将多维数组的路径表示为字符串(例如表单输入名称数组)

mysql - 缓存与数据库查询

php - 将 PHP 代码添加到 Drupal 7 页面

php - Mysql大数据库迁移