简体   繁体   English

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

[英]PHP/MySQL code won't insert userid and password into table

I'm working on an assignment and can't seem to get my code to insert the userid and password into my "users" table. 我正在分配作业,似乎无法获取将用户ID和密码插入“用户”表中的代码。 The input name is "userid" and "password" - I also used a hashing password example. 输入名称为“ userid”和“ password”-我还使用了哈希密码示例。 For the sake of brevity, I left out the code that follows if($result == false). 为了简洁起见,我省略了if($ result == false)后面的代码。 If you don't see any errors, then I will chalk it up to errors in my SQL table/host setup. 如果您没有看到任何错误,则将其归因于我的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');
    }
    }
?>

I'd suggest moving from mysqli_* to PDO. 我建议从mysqli_ *迁移到PDO。

I don't really use mysqli_* but this should work. 我不是真的使用mysqli_ *,但是应该可以。 If it doesn't, post all the code you're using to make it easier. 如果没有,请发布您正在使用的所有代码以使其变得更容易。 Also you were missing a bracket beside your else { 另外,您在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';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM