2012-09-08 108 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表中。这样你可能会有冲突的麻烦,不是吗?