PHP / MySQL代码不会在表中插入用户ID和密码

时间:2013-12-03 23:32:44

标签: php mysql login sessionid login-script

我正在进行一项任务,似乎无法获取我的代码以将用户ID和密码插入到我的“用户”表中。输入名称是“userid”和“password” - 我还使用了散列密码示例。为简洁起见,我省略了if($ result == false)后面的代码。如果您没有看到任何错误,那么我将在SQL表/主机设置中将其归结为错误。

Thanks so much.  

<?php

    $userid = $_POST['userid'];
    $password = $_POST['password'];

    require('constants.php');
    include_once('menu.php'); 

    //starts connection
    $dbc = mysqli_connect(HOST,USERID,PASSWORD,DB);

    if(isset($_POST['register'])) {
        $query = "INSERT INTO `users` (`userid`, `password`) VALUES (?,?)";
        // PREPARED STATEMENTS
        $stmt = mysqli_prepare($dbc,$query);

        include('PasswordHash.php');
        $pwdHasher = new PasswordHash(8, false);
        $hash = $pwdHasher->HashPassword($password);

        mysqli_stmt_bind_param($stmt,'ss',$userid,$hash);

        // execute query
        $result = mysqli_stmt_execute($stmt);

    else {
        //user successfully signed in
        //session the session value to the user id
        $_SESSION['uid']=$userid;
        //display the page
        include('displayChat.php');
    }
    }
?>

1 个答案:

答案 0 :(得分:0)

我建议从mysqli_ *转到PDO。

我真的不使用mysqli_ *但这应该有用。如果没有,请发布您正在使用的所有代码以使其更容易。 您还错过了else {

旁边的括号
require 'constants.php';
include_once 'menu.php'; 

if( isset($_POST['register']) )
{
    $dbc = mysqli_connect(HOST,USERID,PASSWORD,DB);

    $stmt = mysqli_prepare( $dbc, "INSERT INTO users (userid, password) VALUES ( :userid, :password )" );
    $stmt = mysqli_execute(':userid' =>$_POST['userid'], ':password' => $_POST['password']);

    include('PasswordHash.php');
    $pwdHasher = new PasswordHash(8, false);
    $hash = $pwdHasher->HashPassword($_POST['password']);

    mysqli_stmt_bind_param($stmt,'ss',$_POST['userid'],$hash);

    $result = mysqli_stmt_execute($stmt);

} else {
    $_SESSION['uid']= $userid;

    include_once 'displayChat.php';
}