我在注册信息方面遇到了一些麻烦。
它应该转到一个名为"mon"的数据库和一个名为"user"的表。此表包含信息"id"、"名称"和"pass"。"id"基于自动增量系统,因此不需要在寄存器页面中定义。
没有报告任何错误,只是重新加载页面。但是,当使用 phpMyAdmin 访问表时,它显示未创建新行。我需要这方面的帮助,因为我对PHP相当陌生,对它的问题知之甚少。
法典:
Pastebin 因为堆栈溢出对我的代码表现得很奇怪,即使我间隔了它。
注册.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>
连接.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>";
}
?>
非常感谢对此的任何帮助,创建标记登录的会话以及重定向到带有会话的索引页面。
结合给出的其他答案,您在表和列周围使用常规引号,这是无效的标识符限定符。
("INSERT INTO 'user' ('name', 'pass')
^ ^ ^ ^ ^ ^
应该删除哪个
("INSERT INTO user (name, pass)
或者使用类似于引号的刻度,但实际上不是引号;它们完全是两种不同的动物。
("INSERT INTO `user` (`name`, `pass`)
关于 MySQL 标识符限定符的参考:
- http://dev.mysql.com/doc/en/identifier-qualifiers.html
密码
我还注意到您可能以纯文本形式存储密码。不建议这样做。
使用以下方法之一:
- CRYPT_BLOWFISH
-
crypt()
-
bcrypt()
-
scrypt()
- 在开墙上
- PBKDF2
- PBKDF2 on PHP.net
- PHP 5.5 的
password_hash()
函数。 - 兼容包(如果 PHP <5.5(https://github.com/ircmaxell/password_compat/
其他链接:
- PBKDF2 for PHP
关于列长的重要旁注:
如果您决定使用password_hash()
或加密,请务必注意,如果您当前密码列的长度低于 60,则需要将其更改为该长度(或更高(。手册建议长度为 255。
您需要更改列的长度并使用新的哈希重新开始才能使其生效。否则,MySQL 将以静默方式失败。
您忘记在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>";
}
?>
然后在你的寄存器.php文件中,你添加了一个不会被设置($_POST['desc']
(的post索引,这就是为什么你的if语句中的代码永远不会被执行,因为你的表单不包含输入名称desc。我认为最好删除它,请不要忘记执行您准备好的声明。代码可能是这样的。
将 ' 更新为 '
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>
文件中,第 6 行
if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {
您不发布$_POST['desc']
检查此内容。
注意:更好的方法是,对于所有如果,看看发生了什么
编辑:
使用$insert->execute();
后
$insert->bind_param('ss', $username, $password);
第 6 行
我认为您已经在 isset 语句中添加了额外的参数"desc",该语句在任何地方都没有使用。
更改此行
if(isset($_POST['username'], $_POST['password'], $_POST['desc'])) {
前往关注
if(isset($_POST['username'], $_POST['password'])) {
添加以下代码。
您已将参数绑定到 sql 查询,但尚未执行。
在 $insert->bind_param('ss', $username, $password( 之后添加以下代码;
$insert->execute();
创建会话
要创建会话,您需要将会话值存储在会话变量中。您可以通过在会话变量中存储新注册的用户 ID 来执行此操作。在 $insert->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"
}