简体   繁体   English

用户信息未保存到我的数据库中

[英]user info is not being saved into my database

okay so i'm having a problem with my database. 好的,所以我的数据库有问题。 I'm trying to create a registration page, and everything works with no problem. 我正在尝试创建一个注册页面,并且一切正常。 I'm using xampp phpmyadmin, and everytime I hit the registration button after entering some info, nothing comes up in my database. 我使用的是xampp phpmyadmin,每次输入一些信息后每次点击注册按钮时,数据库中都不会显示任何内容。

<?php
session_start();

$db = mysqli_connect("localhost", "root", "", "saver",);


if (isset($_POST[ 'register_btn'])) {
session_start();
$username = mysql_real_escape_string($db,$POST['username']);
$email = mysql_real_escape_string($db,$POST['email']);
$password = mysql_real_escape_string($db,$POST['password']);
$password2 = mysql_real_escape_string($db,$POST['password2']);

if ($password == $password2) {
//create user
$password = md5($password); //hashes password before entering into database
$sql = "INSERT INTO users(username, email, password) VALUES('$username',     $email', $password,)"; 
mysqli_query($db, $sql);
$_SESSION['message'] = "You are now logged in";
$_SESSION['username'] = $username;
header("location: home.php"); // Redirect to home page
}else{
$_SESSION['message'] = "The two passwords do not match";
    }
}
?>

<html> 
<head>
<title> Submission form </title>
</head>

<body>
<form method="post" action="saver.php">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" class="textInput">
</tr>

<tr>
<td>Email:</td>
<td><input type="email" name="email" class="textInput">
</tr>

<tr>
<td>Password:</td>
<td><input type="password" name="password" class="textInput">
</tr>

<tr>
<td>Password again:</td>
<td><input type="password" name="password2" class="textInput">
</tr>

<tr>
<td></td>
<td><input type="submit" name="register_btn" value="Register">
</tr>
</table>
</form>
</body>
</html>

You can discover and solve all of the issues you are having by TURNING ON AND READING PHP ERROR LOGGING. 您可以通过打开并阅读PHP错误记录来发现并解决所有问题。 Read How to do that . 阅读如何做

Your problem is in these lines: 您的问题在于以下几行:

$db = mysqli_connect("localhost", "root", "", "saver",);

and

$username = mysql_real_escape_string($db,$POST['username']);

firstly, your $db has a left over comma at the end, remove it. 首先,您的$db末尾有一个逗号,请将其删除。 This is causing a probably fatal PHP error you should be picking up with your error reporting . 这可能会导致致命的PHP错误,您应该在错误报告中进行介绍

Secondly, the $db object is a MySQLi identifier, NOT a MySQL identifier, but you've passed it to a MySQL_ function. 其次, $db对象是一个MySQLi标识符,而不是MySQL标识符,但是您已将其传递给MySQL_函数。 Therefore the script will either cease on an error or the database will insert empty values. 因此,脚本将因错误而停止,或者数据库将插入空值。 This is why you're not getting anything saved. 这就是为什么您没有保存任何内容的原因。

Also (as noted by Luke) the MySQL_ functions have an inverted argument order, so you probably simply forgot to add the i to the function name! 同样(如Luke所述), MySQL_函数具有相反的参数顺序,因此您可能只是忘记了将i添加到函数名称中!


You have also not properly encased your values in MySQL -- each value will need to be wrapped in single quotes such as you have done with '$username' you need to do the same thing with $email and $arseword . 您还没有正确地将值封装在MySQL中-每个值都需要用单引号引起来,例如使用'$username'完成后,需要使用$email$arseword做相同的事情。

And you also need to remove the trailing commas from your MySQL VALUES collection too... 而且您还需要从MySQL VALUES集合中删除结尾的逗号...

A few extra notes: 一些额外的注意事项:

  • You're using both mysqli_ and mysql_ functions in you code. 您在代码中同时使用了mysqli_mysql_函数。 You should only be using MySQLi_ functions. 您应该使用MySQLi_函数。
  • You should seriously consider (as in; DO THIS!!! ) using Prepared Statements rather than procedural SQL. 您应该认真考虑(例如;这样做!! ),而不是过程式SQL,而是使用预处理语句
  • MD5 is absolutely not suitable for hashing passwords. MD5绝对不适合哈希密码。 Stop it. 停下来。 Use password_hash . 使用password_hash
  • header(Location:); relocation directives should be immediately followed by a script terminator such as exit; 搬迁指令应紧跟一个脚本终止如exit; or die(); die();
  • session_start() should only be called once at the top of the script. session_start()只能在脚本顶部被调用一次
  • Seriously, Use prepared statments. 认真使用准备好的陈述。 There are hundreds of good examples of them around. 周围有数百个很好的例子。
  • And you will save yourself (and many folks on SO) hundreds of hours of stress by using and reading and reacting to PHP Error Logging ! 通过使用和阅读PHP错误日志并对其做出反应,您将节省数百小时的压力(以及SO上的许多人)!

EDIT: 编辑:
As clarified by Luke : 正如路加福音所阐明的:

The actual problem is mysql_real_escape_string accepts only one parameter (ie mysql_real_escape_string($POST['username']);). 实际的问题是mysql_real_escape_string仅接受一个参数(即mysql_real_escape_string($ POST ['username']);)。 It can be used regardless of MySQLi being in use or not, but it should of course be avoided for using prepared statements instead. 无论是否使用MySQLi,都可以使用它,但是,如果使用准备好的语句,则应避免使用它。

hey guys I found the problem. 嘿,我发现了问题。 it was in the insert part. 它在插入部分。 I had "users" in my editor, and I had "user_info" in my database. 我的编辑器中有“用户”,数据库中有“ user_info”。 So I just changed "users", to "user_info". 因此,我只是将“用户”更改为“ user_info”。 Thank you so much for your help guys. 非常感谢您的帮助。 How can I give you credit? 我怎么给你信用?

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

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