0

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>";


 ?> 

3 Answers3

0

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?

Community
  • 1
  • 1
nowhere
  • 1,558
  • 1
  • 12
  • 31
0

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

P.S.: You do not use $Reference, but still, you are overwritting it

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
0

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>";


 ?> 
Mani
  • 888
  • 6
  • 19
  • Not to disregard, but you too should refer to [*w3fools.com*](http://www.w3fools.com/) – Ravinder Reddy Feb 26 '14 at 04:36
  • Hi guys I tried this yesterday and it doesn't seem to be working, I have included a update of the code in the original post.... I have started reading w3fools.com and w3schools also now to give me a better idea. – user3301611 Feb 26 '14 at 14:20