简体   繁体   中英

Copy a record with a unique reference in mysql from one table to another table

as the title suggests, I want to Move a record with a unique reference in mysql from one table to another table.

I have tried many different methods myself and what other members have suggested.

I will show you my two pages what I want to get to work. one is a table of records from the first table. which has a link on each row to copy the row's data from that table to another table. I just cant get the code to work on the second page to copy the data across to the other table.

ps I will be going through all the code once finished to try and look for sql injection.

Please someone try and help me fix this as its driving me crazy.

Thanks for your time in advance.

Table.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>

Logic/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;

 ?> 

I have fixed the issue myself I was having. I wanst 'Get'ing the reference so they didn't know what record needed to be moved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM