简体   繁体   English

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

[英]Insert statement not working. not transferring into other table

Pretty new to PHP and MySQL. PHP和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. 我已经在php脚本中创建了一条insert语句,用于将某些表中的一行数据从一个表传输到下一个表。 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. 将不起作用,因为结果仅在成功时包含true。

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? 但是,您还应该检查一下以了解如何避免注入: 用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 PS:您没有使用$ Reference,但是仍然覆盖了它

try this one 试试这个

1) you mention all var name as $Reference its changed 1)您将所有var名称都称为$ Reference更改了

2) query not correct plz study how wrote query.. 2)查询不正确的plz学习怎么写查询..

3) REFER: http://www.w3schools.com/php/php_mysql_intro.asp 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>";


 ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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