我无法使用mysqli_query将我的用户信息发送到数据库

时间:2016-10-22 13:14:25

标签: php php-7

我的代码:

<?PHP

session_start();
$user = 'root';
$password = 'root';
$db = 'Authentication';
$host = 'localhost';
$port = 3306;

$link = mysqli_init();
$success = mysqli_real_connect(
   $link, 
   $host, 
   $user, 
   $password, 
   $db,
   $port
);
if(isset($_POST['sbtn'])){
    session_start();
    $f_name = mysqli_real_escape_string($_POST['f_name']);
    $l_name = mysqli_real_escape_string($_POST['l_name']);
    $email = mysqli_real_escape_string($_POST['email']);
    $pass = mysqli_real_escape_string($_POST['pass']);
    $c_pass = mysqli_real_escape_string($_POST['c_pass']);

    if($pass == $c_pass){
        //create user
        $Password = md5($pass);
        mysqli_query($success, $db);

        $query = "INSERT INTO users( email, password, f_name, l_name) VALUES ($email, $pass, $f_name, $l_name)";
        mysqli_query($success, $query);
    }else{
        //tell user they are not equal
        echo("The two passwords did not match");
    }
}

mysqli_close($success);
?>

似乎所有其余的代码都工作,因为我有错误检查代码,我是新的编码在php中因此我很难理解我是如何克服这个问题的!

非常感谢帮助!

1 个答案:

答案 0 :(得分:-1)

尝试改变:

$query = "INSERT INTO users( email, password, f_name, l_name) VALUES ($email, $pass, $f_name, $l_name)";

为:

$query  = "INSERT INTO users( email, `password`, f_name, l_name) VALUES ('$email', '$pass', '$f_name', '$l_name')";

<强>说明: When to use single quotes, double quotes, and backticks in MySQL

然后:

mysqli_query($success, $db);
mysqli_query($success, $query);
mysqli_close($success);

为:

mysqli_query($link, $db);
mysqli_query($link, $query);
mysqli_close($link);

最后:

mysqli_query($link, $query);

为:

$result = mysqli_query($link, $query);
    if (!$result) {
        echo '<pre>';
        var_dump(mysqli_error_list($link));
        echo '</pre>';
    }

(用于调试目的)

<强>解释

对我的(可怜的)英语

抱歉

所有这些替换完成($ success - &gt; $ link)因为这些函数需要 mysqli_connect() mysqli_init()返回的链接标识符,但是你传递了 $ success (bool值) http://php.net/manual/en/mysqli.query.php(程序风格要求)

变化:

$f_name = mysqli_real_escape_string($_POST['f_name']);
$l_name = mysqli_real_escape_string($_POST['l_name']);
$email = mysqli_real_escape_string($_POST['email']);
$pass = mysqli_real_escape_string($_POST['pass']);
$c_pass = mysqli_real_escape_string($_POST['c_pass']);

$f_name = mysqli_real_escape_string($link,$_POST['f_name']);
$l_name = mysqli_real_escape_string($link,$_POST['l_name']);
$email = mysqli_real_escape_string($link,$_POST['email']);
$pass = mysqli_real_escape_string($link,$_POST['pass']);
$c_pass = mysqli_real_escape_string($link,$_POST['c_pass']);

<强>说明: mysqli_real_escape_string 需要 mysqli_connect()或mysqli_init()返回的链接标识符。 http://php.net/manual/en/mysqli.real-escape-string.php(程序风格要求)