将注册信息插入Mysql数据库的问题

时间:2016-01-31 07:21:42

标签: php mysql mysqli

我在注册信息方面遇到了一些麻烦 它应该转到一个名为“mon”的数据库和一个名为“user”的表。这个表包含信息“'id','name'和'pass'”。'id'基于一个auto增量系统,因此不需要在寄存器页面中定义。

没有报告错误,只是重新加载页面。但是,当使用phpMyAdmin访问表时,它显示没有创建新行。我需要帮助,因为我对PHP很新,不要我知道它的大部分问题。

代码:
Pastebin because stackoverflow is acting weird with my code even though I spaced it.

register.php:

<?php
    require_once 'connect.php';
    if(!empty($_POST)) {
        if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];

            if(!empty($username) && !empty($password)) {
                $insert = $db->prepare("INSERT INTO 'user' ('name', 'pass') VALUES (?, ?)");
                $insert->bind_param('ss', $username, $password);
            }
        }
    }
    ?>
    <html>
        <head>
            <?php
            require_once 'style.php';
            ?>
            <title>si | Registering an Account</title>
        </head>
        <body>
            <?php
            require_once 'header.php';
            ?>
            <div id="page">
                <h2>Register an Account on Si</h2>
                <form action="" method="post">
                    <input type="text" name="username" placeholder="Username" autocomplete="off">
                    <input type="password" name="password" placeholder="Password" autocomplete="off">
                    <button>Register</button>
                </form>
            </div>
        </body>
    </html>

connect.php:

<?php
    $db = new mysqli('127.0.0.1', 'root', '');
    if($db->connect_errno) {
        echo "<p id='gotem'>Something has gone wrong.Tell J to fix it.</p>";
    }
    ?>

对此有任何帮助,创建一个标记登录的会话,并且在会话开启的情况下重定向到索引页面非常感谢。

4 个答案:

答案 0 :(得分:3)

结合给出的其他答案,您在表和列周围使用常规引号,这是无效的标识符限定符。

("INSERT INTO 'user' ('name', 'pass')
              ^    ^  ^    ^  ^    ^

应删除哪个

("INSERT INTO user (name, pass)

或使用类似报价的刻度,但实际上不是报价;那些是两种不同的动物。

("INSERT INTO `user` (`name`, `pass`)

MySQL的标识符限定符的引用:

<强>密码

我还注意到您可能以纯文本格式存储密码。不建议这样做。

使用以下其中一项:

其他链接:

关于列长的重要说明:

如果您决定使用password_hash()或隐藏,请务必注意,如果您当前的密码列长度低于60,则需要将其更改为(或更高)。手册建议长度为255。

您需要更改列的长度并重新开始使用新哈希才能使其生效。否则,MySQL将无声地失败。

答案 1 :(得分:1)

首先,您忘记在mysqli实例中添加数据库名称和密码

<?php
    $db = new mysqli('127.0.0.1', 'root', 'your_password_for_the_user_root','yout_database_name');
    if($db->connect_errno) {
        echo "<p id='gotem'>Something has gone wrong.Tell J to fix it.</p>";
    }
?>

然后在你的register.php文件中你添加了一个不会被设置的帖子索引($_POST['desc']),这就是你的if语句中的代码永远不会被执行的原因,因为你的表单不包含输入名称desc.I认为它更好地删除它,请不要忘记执行您准备好的声明。代码可能是这样的。

更新'更改'

  if(!empty($_POST)) {
            if(isset($_POST['username'], $_POST['password'])) {
                $username = $_POST['username'];
                $password = $_POST['password'];

                if(!empty($username) && !empty($password)) {
                    $insert = $db->prepare("INSERT INTO `user` (`name`, `pass`) VALUES (?, ?)");
                    $insert->bind_param('ss', $username, $password);
                    if (!$insert->execute()) {
                         echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
                     }                
                }
            }
        }

添加按钮的属性类型可能也会有所帮助

                <form action="" method="post">
                    <input type="text" name="username" placeholder="Username" autocomplete="off">
                    <input type="password" name="password" placeholder="Password" autocomplete="off">
                    <button type="submit">Register</button>
                </form>

答案 2 :(得分:0)

在您的档案中,第6行

if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {

你不发帖$_POST['desc']检查一下。

注意:更好的方法是,还有其他所有if,看看发生了什么

编辑:

使用$insert->execute();

$insert->bind_param('ss', $username, $password);

答案 3 :(得分:0)

行号6

我认为你已经在isset语句中添加了额外的参数'desc',这在任何地方都没有使用。

更改此行

if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {

关注

 if(isset($_POST['username'], $_POST['password'])) {

添加以下代码。

您已将参数绑定到sql查询但尚未执行。

在$ insert-&gt; bind_param('ss',$ username,$ password)之后添加以下代码;

$insert->execute();

创建会话

要创建会话,您需要将会话值存储在会话变量中。您可以通过在会话变量中存储新注册的用户ID来完成此操作。在$ insert-&gt; execute();

之后添加以下代码
$userid =  mysqli_insert_id($db); // get newly registred userid
$_SESSION['userid'] = $userid     // store the registred userid in session.

重定向到索引页

使用php中的标题函数重定向页面

添加以下代码。

header("Location: index.php");

在索引页面

访问会话和会话变量。在第一行添加以下代码。

session_start();
$userid = $_SESSION['userid'];

if(isset($userid) && !empty($userid)) {
echo "User is logged In using user id:".$userid;
} else {
echo "User is not logged In"
}