2012-09-08 70 views
0

我在上個小時試圖解決這個問題。我有一個很長的形式,我試圖將信息上傳到MySQL數據庫。進入我製作的桌子。這是我使用的MySQL查詢:查詢Mysql的麻煩:上傳

mysql_query("INSERT INTO `users_temp`(`first_name`, `surname`, `birthday`, `nationality`, `email`, `mobile`, `landline`, `address`, `town`, `post_code`, `country`, `password`, `code_conf`) VALUES ([$f_name],[$s_name],[$bday],[$nationality],[$email],[$mobile],[$landline],[$address],[$town],[$post_code],[$country],[$pass],[$conf_code])"); 

如果有人看到任何問題,爲什麼它不起作用請讓我知道。謝謝...

$f_name   = $_POST["f_name"]; 
$s_name   = $_POST["s_name"]; 
$pass   = $_POST["pass"]; 
$birthday  = $_POST["bday"]; 
$nationality = $_POST["nationality"]; 
$email   = $_POST["email"]; 
$mobile   = $_POST["mobile"]; 
$landline  = $_POST["landline"]; 
$address  = $_POST["address"]; 
$town   = $_POST["town"]; 
$post_code  = $_POST["post_code"]; 
$country  = $_POST["country"]; 

$conf_code  = substr(md5(uniqid(rand(), true)), 1, 70); 

include("connect_into_mysql.php"); 
mysql_select_db("jzperson_edu", $conn); 

$res_01 = mysql_query("SELECT * FROM users WHERE email='$email'"); 
$count_01 = mysql_num_rows($res_01); 

if($count_01==0) 
{ 
mysql_query("INSERT INTO users_temp ('first_name','surname','birthday','nationality','email','mobile','landline','address','town','post_code','country','password','code_conf') VALUES ('$f_name','$s_name','$bday','$nationality','$email','$mobile','$landline','$address','$town','$post_code','$country','$pass','$conf_code')"); 
header("Location: home.php"); 
}else{echo "This email is already registered. If you lost your password you can reset it here.";} 
+0

你有什麼錯誤嗎?你是否清理了你放入數據庫的變量? ps,你可以使用'{$ varname}'或''。$ varname。',而不是'[$ varname]' –

+0

我會把整個代碼置於問題中。我正在編輯它。 –

+0

什麼是確切的錯誤信息?並請刪除方括號[]並再次嘗試。你現在有什麼錯誤信息? –

回答

1

1 /您必須注意SQL注入:請read this article。封裝所有的$ _ POST數據和mysql_real_escape_string,例如:

$f_name   = mysql_real_escape_string($_POST["f_name"]); 

2/MySQL將很快過時,看到紅色的盒子here。在所有的頭文件之後,放一個die()(否則,繼續執行代碼直到結束,如果客戶端的brwoser重定向被禁用,他可能會看到一些未經授權的內容)。

4 /不要在一行中寫很長的請求,這可能會避免麻煩並使工作更輕鬆。爲了調試它,在末尾添加一個「or die(mysql_error())」,這將顯示一條消息,幫助您獲得解決方案。

mysql_query(" 
INSERT INTO users_temp (
    'first_name','surname','birthday', 
    'nationality','email','mobile', 
    'landline','address','town', 
    'post_code','country','password', 
    'code_conf' 
) VALUES (
    '$f_name','$s_name','$bday', 
    '$nationality','$email','$mobile', 
    '$landline','$address','$town', 
    '$post_code','$country','$pass', 
    '$conf_code' 
) 
") or die(mysql_error()); 

順便說一下,您正在檢查用戶表並插入到user_tmp表中。這樣你可能會有衝突的麻煩,不是嗎?