douyu5775 2016-05-23 12:02
浏览 35
已采纳

如何从php表单成功查询MySql服务器? [重复]

This question already has an answer here:

This is my existing code:

<?php
include('config.php');
session_start();
if(isset($_POST['submit'])) {
  if(!$_POST['username']) {
    $error['username'] = "<p>Please supply your name.</p>
";
  }
  if(!$_POST['password']) {
    $error['password'] = "<p>Please supply your password.</p>
";
  }

  if(!is_array($error)) {       
        $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

        if($db === false) {
            die('Could not connect: ' . mysqli_error());
        }

        $user = mysqli_real_escape_string($db, $_POST['username']);
        $pass = mysqli_real_escape_string($db, password_hash($_POST['password'], PASSWORD_BCRYPT));

        $query = "INSERT INTO users (USERNAME, PASSWORD) VALUES ('$user', '$pass')";
        mysqli_query($query, $db);

        mysqli_close($db);
  }
}
?>

This just doesn't seem to work. The connection is fine because I am using the same process in another file and it works perfectly. I just wondered why the query isn't doing anything?

EDIT:

Here's the form and changed to mysqli in above:

<html>

<head>
    <title>Add Users</title>
</head>

<body>

    <form method="post" action="formProcess.php">
        Username: <input type="text" name="username"><br> Password: <input type="password" name="password"><br>
        <input type="submit">
    </form>
</body>

</html>
</div>
  • 写回答

2条回答 默认 最新

  • doucang6739 2016-05-23 12:10
    关注

    You need to use either mysql_* or mysqli_* functions throughout your whole script. Use the latter as mysql_* functions has been deprecated.

    Edited code:

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

    The parameters required for mysqli_query is connection followed by query.

    More information at http://php.net/manual/en/mysqli.query.php.


    Also, you need to specify the name attribute in your submit button:

    <input name="submit" type="submit">
    

    Thanks to @Fred-ii- for the hint!


    Just a tip: You shouldn't store users' passwords as plain-text, you should hash/ crypt it.

    In PHP 5.5, there's a Password Hash function: http://php.net/manual/en/ref.password.php.

    Also, you shouldn't escape the password before hashing as it would change the hash since slashes will be added.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮