尝试插入后的空白页

时间:2011-01-08 15:17:55

标签: mysql

每当我尝试将数据插入我的数据库“用户”时,我总会得到一个空白页面。它没有给我任何错误,它不包括'mainmenu.php',或者返回任何反馈。有人可以帮我吗?这是代码:

<?php

include("mainmenu.php");

$con = mysql_connect("localhost", "root", "*********");
if (!$con) {
   die('Connection failure.' . mysql_error());
   }

//Variable def
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$usrname = $_POST['usrname'];
$password = $_POST['password'];

mysql_select_db("users",$con) or die(mysql_error());
mysql_query("INSERT INTO data (usrname, fname, lname, password, email) VALUES ($usrname, $fname, $lname, $password, $email)") or die(mysql_error());
mysql_close($con)

echo("Thank you for registering!")
?>

看起来对我来说。

4 个答案:

答案 0 :(得分:3)

没有错误?在脚本顶部添加:

<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );

答案 1 :(得分:3)

首先:字符串需要分隔:

mysql_query("INSERT INTO data (usrname, fname, lname, password, email) VALUES ('$usrname', '$fname', '$lname', '$password', '$email')";

第二:从未将未经检查的用户数据传递到数据库查询中。首先对每个值使用mysql_real_escape_string()

答案 2 :(得分:2)

你没有收到任何语法错误吗?

第一

mysql_close($con)
echo("Thank you for registering!")

更改为

mysql_close($con);
echo("Thank you for registering!");

第二,请引用您的$ _POST并正确转义它 阅读本文 - Escaping single quote in PHP when inserting into MySQL

答案 3 :(得分:0)

mysql_query("INSERT INTO data (usrname, fname, lname, password, email) VALUES ($usrname, $fname, $lname, $password, $email)") or die(mysql_error());

改为使用

$insert_sql = sprintf("INSERT INTO users (usrname, fname, lname, password, email) " .
          "VALUES ('%s' ,'%s', '%s', '%s', %d); ",
           mysqli_real_escape_string($conn, $usrname),
           mysqli_real_escape_string($conn, $fname),
           mysqli_real_escape_string($conn, $lname),
           mysqli_real_escape_string($conn, md5($password)),
           mysqli_real_escape_string($conn, $email),
           mysqli_insert_id($conn)); 

然后查询上面的字符串

mysqli_query($conn, $insert_sql);

然后是条件

if($insert_sql){
$usrname = $_SESSION['user_id'];
//url.ext e.g could be "home.php" or "you.html"
//header is used for redirecting a page
header("Location: url.ext");
}else{
$msg = "error inserting";
header("Location: " . $_SERVER['HTTP_REFERER'] . "?Message= ". $msg );
}