2014-02-24 80 views
0

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


?> 
+0

您是否使用過'mysql_error()'來檢查是什麼問題? –

+0

你能給我們提供這個錯誤嗎? –

+0

你能告訴我什麼,我需要添加到腳本中,以獲取錯誤,請 – user3301611

回答

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)){ 

不會因爲工作的結果將只包含成功返回真。

也許還有更多的錯誤我不確定。但你也應該檢查這個以瞭解如何避免注入: What's the best method for sanitizing user input with PHP?

0

如果你想將數據從一個錶轉移到另一個表,你應該在某處選擇這個表。你的代碼中沒有任何地方,你只是指定了列,你的腳本應該如何知道它們來自哪裏?

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

PS:不要使用$參考,不過,你overwritting它

0

試試這個

1)你提到的所有變量名稱爲$引用其改變

2)查詢不正確plz研究如何寫查詢..

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


?> 
+0

不要忽視,但你也應該參考[* w3fools.com *](http:// www.w3fools.com/) –

+0

@Ravinder感謝好友的有用信息... – Mani

+0

嗨,大家好我昨天試過這個,它似乎沒有工作,我已經在原始帖子中包含代碼的更新.. ..我已經開始閱讀w3fools.com和w3schools現在也給我一個更好的主意。 – user3301611