gpt4 book ai didi

javascript - 请查看/评论 : php mysql javascript (not school)

转载 作者:行者123 更新时间:2023-11-29 11:25:48 26 4
gpt4 key购买 nike

非常感谢大家。 html 很好,我相信 javascript 仍然是一个问题,php 看起来还不错,但我需要更多的指针。我已按如下方式编辑了代码...

html:

<input name="uname" type="text" placeholder="please enter a user name" required><br />
<input name="upassword" type="text" placeholder="please enter a password or passphrase" required><br />
<input id="register" name="register" type="submit" class="btn btn-primary btn-lg outline" onclick="register();"/>

函数寄存器() {

JavaScript

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "register.php",
data: { name: "uname", password: "password"},
success: function (result) {
alert("success");
}
});
}

我最大的问题是 php

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('USER', 'root');
define('PASS', 'Schwinny2156!');

try {
$conn = new PDO("mysql:host=localhost;dbname=users;", USER, PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('INSERT INTO `users` (name, password VALUES (:name,:password)');
$stmt->bindParam(':name', $_POST['uname']);
$stmt->bindParam(':password', $_POST['upassword']);

$stmt->execute()
}
catch(PDOException $e)
{
echo $stmt . "<br>" . $e->getMessage();
}
$stmt = null;

最佳答案

我修复了所有看起来错误的地方,不确定我是否错过了什么。

编辑:忘记提及,查找password_hash并实现该计划。以明文形式存储密码是不行的。

编辑 2:添加了密码哈希函数以开始使用。使用password_verify验证登录时的密码。

<form onsubmit="return register(this);">
<input name="uname" type="text" placeholder="please enter a user name" required><br />
<input name="upassword" type="text" placeholder="please enter a password or passphrase" required><br />
<input id="register" name="register" type="submit" class="btn btn-primary btn-lg outline"/>
</form>

js

function register(form) {
$.post("register.php", $(form).serialize());
return false;
}

php

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('USER', 'root');
define('PASS', 'password');

try {
$conn = new PDO("mysql:host=localhost;dbname=users;", USER, PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

define('uname', $_POST['uname']);
$passwordHash = password_hash($_POST['upassword'], PASSWORD_DEFAULT);
define('upassword', $passwordHash);

$sql = 'INSERT INTO `users` (name , password) VALUES (?, ?)';
$conn->prepare($sql)->execute([uname, upassword]);
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

关于javascript - 请查看/评论 : php mysql javascript (not school),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38217430/

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