gpt4 book ai didi

php - 脚本未插入数据 [MySQL/PHP]

转载 作者:行者123 更新时间:2023-11-29 12:12:58 24 4
gpt4 key购买 nike

我有一个小问题。

我制作了一个 PHP 脚本,应该将数据插入到我的数据库中,但遗憾的是......它不起作用,但它以前曾起作用过。我现在正在主机上运行我的脚本,这可能是问题所在..但我认为不是。

代码:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<?php
$usernameErr = $emailErr = $passwordErr = $password_valErr = "";
$username = $email = $password = $password_val = "";

if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(empty($_POST['username'])) {
$usernameErr = "Name is required";
} else {
$username = validate_input($_POST['username']);
if(strlen($username) <= 3) {
$usernameErr = "Username must be 4 characters or longer.";
}
if(strlen($username) > 26) {
$usernameErr = "Username can't be longer as 26 characters.";
}
if(!preg_match("/^[a-zA-Z ]*$/", $username)) {
$usernameErr = "Only letters and white space allowed.";
}
}

if(empty($_POST['email'])) {
$emailErr = "Email is required";
} else {
$email = validate_input($_POST['email']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format.";
}
}

if(empty($_POST['password'])) {
$passwordErr = "Password is required";
} else {
$password = validate_input($_POST['password']);
if(strlen($password) <= 5) {
$passwordErr = "Password must be 6 characters or longer.";
}
if(strlen($password) > 26) {
$passwordErr = "Password can't be longer as 26 characters.";
}
if(!preg_match("#[0-9]+#", $password)) {
$passwordErr = "Password must contain atleast 1 number.";
}
}

if(empty($_POST['password_val'])) {
$password_valErr = "Password_val is required";
} else {
$password_val = validate_input($_POST['password_val']);
if($password_val != $password) {
$password_valErr = "Password_val must be equal to password.";
}
}

if($usernameErr == '' && $emailErr == '' && $passwordErr == '' && $password_valErr == '') {

$check_user = mysqli_query($conn, "SELECT * FROM users WHERE username='".trim($username)."'");
$check_mail = mysqli_query($conn, "SELECT * FROM users WHERE email='".trim($email)."'");

if(mysqli_num_rows($check_user) > 0) {
echo 'This username allready exists';
} elseif(mysqli_num_rows($check_mail) > 0) {
echo 'This email address is already registered.';
} else {
$username = mysql_real_escape_string(trim($username));
$email = mysql_real_escape_string(trim($email));
$password = mysql_real_escape_string(trim($password));
$rand_salt = randString();

/*$final_pass = password_hash($password_val, PASSWORD_DEFAULT)."\n";*/
$final_pass = sha1($password.PASSWORD_SALT.$rand_salt);
$privileges = 0;

$sql = "INSERT INTO users (username,password,salt,email)
VALUES ('".$username."','".$final_pass."','".$rand_salt."','".$email."')";

if($conn->query($sql) === TRUE) {
echo "User registered.";

} else {
echo 'Error: ' . $sql . '<br>' . $conn->error;
}
}
}
}
?>

<table border="1">
<tr>
<td><label>Username</label><?=' <b>' . $usernameErr . '</b>';?></td>
<td><input type="text" name="username" value="<?=$username;?>" placeholder="Enter your desired username..." /></td>
</tr>
<tr>
<td><label>E-mail</label><?=' <b>' . $emailErr . '</b>';?></td>
<td><input type="text" name="email" value="<?=$email;?>" placeholder="Enter your email address..." /></td>
</tr>
<tr>
<td><label>Password<?=' <b>' . $passwordErr . '</b>';?></label></td>
<td><input type="password" name="password" placeholder="Enter your desired password..." /></td>
</tr>
<tr>
<td><label>Repeat Password<?=' <b>' . $password_valErr . '</b>';?></label></td>
<td><input type="password" name="password_val" placeholder="Repeat your chosen password.." /></td>
</tr>
<tr>
<td><input type="submit" name="register" value="Register" /></td>
</tr>
</table>
</form>

这段代码应该可以工作,但第一个问题是:

action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"

这段代码必须将我发送到同一页面,在本例中是register.php,但不知何故它会将我发送到index.php页面。因此,如果我删除此代码并将操作留空或仅在其中输入 register.php,则数据将被插入,但用户名和电子邮件除外,它们都是 Varchars..

希望有人能帮助我

谢谢!

最佳答案

您正在使用 mysql_功能以及与 mysqli_ 连接API。

您不能混合使用这些功能。从连接到查询,您必须使用相同的 MySQL API。

  • 您需要更改 mysql_real_escape_string 的所有实例至mysqli_real_escape_string($conn, $_POST['variable'])

  • variable是您的 POST 数组。

另外,请确保您确实正在连接 mysqli_而不是mysql_或 PDO 与此相关。

  • 不确定您正在连接哪个 MySQL API,即使您的代码确实包含多个 mysqli_功能。
<小时/>

将错误报告添加到文件顶部的 PHP 打开标记之后。

例如<?php error_reporting(E_ALL); ini_set('display_errors', 1);然后剩下的代码,看看它是否产生任何结果, 以及 or die(mysqli_error($conn))mysqli_query() .

<小时/>

旁注: sha1()不被认为是存储哈希值的最安全方法。

摘自 ircmaxell 的答案 https://stackoverflow.com/a/29778421/并将 PDO 与准备好的语句一起使用和 password_hash() .

只需使用一个库即可。严重地。它们的存在是有原因的。

不要自己做。如果您自己制作盐,您就错了。您应该使用一个可以为您处理该问题的库。

$dbh = new PDO(...);

$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$hash = password_hash($password, PASSWORD_DEFAULT);

$stmt = $dbh->prepare("insert into users set username=?, email=?, password=?");
$stmt->execute([$username, $email, $hash]);

登录时:

$sql = "SELECT * FROM users WHERE username = ?";
$stmt = $dbh->prepare($sql);
$result = $stmt->execute([$_POST['username']]);
$users = $result->fetchAll();
if (isset($users[0]) {
if (password_verify($_POST['password'], $users[0]->password) {
// valid login
} else {
// invalid password
}
} else {
// invalid username
}

关于php - 脚本未插入数据 [MySQL/PHP],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30328764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com