简体   繁体   中英

Insert statement not working. not transferring into other table

Pretty new to PHP and MySQL.

I have created an insert statement in my php script, to transfer a row of data from one table to the next for certain fields. Only thing is, it doesn't seem to be working?

Can anybody see where the issue is?

<?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="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);

 // 
while($rows=mysql_fetch_array($result)){
echo '<a href="update.php?Reference='.$rows['Reference'].' ">update test</a>';
 }
// 
// end of while loop 

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

 ?> 

Update

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


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


 ?> 

first you should fix the assignments:

 $Reference=$_REQUEST['Reference'];
 $Reference=$_REQUEST['Forename'];
...

should be something like:

 $Reference=$_REQUEST['Reference'];
 $Forename=$_REQUEST['Forename'];
$surname=$_REQUEST['surname'];

Then update the query in:

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

and so on with the rest of the values.

Also

while($rows=mysql_fetch_array($result)){

won't work since result will only contain true on success.

Maybe there are more mistakes I'm not sure. But you should also check this to learn how to avoid injection: What's the best method for sanitizing user input with PHP?

If you want to transfer data from one table to another, you should select this table somewhere. You have not anywhere in your code, you just specified columns, how is your script supposed to know where do they come from?

INSERT INTO table1 (col1, col2, col3) SELECT correspondingColumn1, correspondingColumn2, correspondingColumn3 FROM table2

PS: You do not use $Reference, but still, you are overwritting it

try this one

1) you mention all var name as $Reference its changed

2) query not correct plz study how wrote query..

3) REFER: http://www.w3schools.com/php/php_mysql_intro.asp

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



 // 
while($rows=mysql_fetch_array($result)){
echo '<a href="update.php?Reference='.$rows['Reference'].' ">update test</a>';
 }
// 


// end of while loop 

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


 ?> 

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