插入语句不起作用。没有转移到其他表

时间:2014-02-24 10:14:51

标签: php mysql insert

PHP和MySQL的新手。

我在我的php脚本中创建了一个insert语句,用于将某行数据从一个表传输到下一个表中。唯一的问题是,它似乎没有起作用?

有人可以看到问题所在吗?

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

 ?> 

更新

// 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 个答案:

答案 0 :(得分:0)

首先你应该修改作业:

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

应该是这样的:

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

然后在以下位置更新查询:

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

以及其他值。

另外

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

不起作用,因为结果只会在成功时包含true。

也许有更多的错误我不确定。但你也应该检查这一点,以了解如何避免注射: What's the best method for sanitizing user input with PHP?

答案 1 :(得分:0)

如果要将数据从一个表传输到另一个表,则应在某处选择此表。您的代码中没有任何地方,您只是指定了列,您的脚本应该如何知道它们来自哪里?

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

P.S。:你不使用$ Reference,但你仍在覆盖它

答案 2 :(得分:0)

尝试这个

1)你提到所有var名称为$ Reference,已更改

2)查询不正确plz研究如何写查询..

3)参考: 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>";


 ?>