gpt4 book ai didi

PHP 准备好的语句 - 无法从数据库检索字符串

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

我对准备好的语句还很陌生,因为它是由另一位用户引起我的注意的。我已经能够创建一个注册函数来正确准备语句、绑定(bind)它然后执行它。它很好地进入数据库。但是,我不确定我是否理解登录部分是如何工作的。我试图获取一行,但我不断得到的结果是“1”,但不是行+行内的数据。有什么建议吗?

数据库: Picture of database

login.php(表单所在的位置)

<form id="loginform" class="form-horizontal" role="form" action="" method="post">

<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="login-username" type="text" class="form-control" name="Lusername" placeholder="Username or Email">
</div>

<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="login-password" type="password" class="form-control" name="Lpassword" placeholder="Password">
</div>

<div class="input-group">
<div class="checkbox">
<label>
<input id="login-remember" type="checkbox" name="remember" value="1"> Remember me
</label>
</div>
</div>

<div style="margin-top:10px" class="form-group">
<!-- Button -->

<div class="col-sm-12 controls">
<button id="btn-login" type="submit" class="btn btn-success"><i class="icon-hand-right"></i>Submit</button>
</div>
</div>
</form>

脚本:

    <script type="text/javascript">
$(function() {
$("#loginform").bind('submit',function() {
var username = $('#login-username').val();
var password = $('#login-password').val();
$.post('scripts/loginFunction.php',{username:username, password:password}, function(data){
$('#signupsuccess').show();
}).fail(function(){{
$('#signupalert').show();
}});
return false;
});
});
</script>

登录函数.php

<?php
require 'connection.php';
$username = $_POST['username'];
$password = $_POST['password'];

if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}

$stmt = $conn->prepare("SELECT `Username`, `Password` FROM `users` WHERE `Username` = ?");
$stmt->bind_param('s',$username);
$stmt->execute();
$stmt->store_result();
echo $stmt->num_rows;
/*if($stmt->num_rows == 1){
$result = $stmt->get_result();
$row = $result->fetch_assoc();
print_r($row);
// here is where you could verify the password
if(password_verify($password, $row['Password'])) {
// good password
echo 'all good!';
}
} else {
//echo "failed to find row";
}*/
?>

loginFunction.php 可以正常工作并正确查询数据库

require 'connection.php';
$username = $_POST['username'];
$password = $_POST['password'];

if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM users WHERE username='$username'";
$result = $conn->query($query);
if($result->num_rows == 1){
$row = mysqli_fetch_array($result);
if(password_verify($password, $row['Password'])){
echo "Login successful!";
}
else{
echo "Login failed.";
}
}
<小时/>

编辑:这是您应该使用的代码。请注意 $stmt 是如何在整个过程中进行的:

$stmt = $conn->prepare("SELECT `Username`, `Password` FROM `users` WHERE `Username` = ?");
$stmt->bind_param('s',$username);
$stmt->execute();
$stmt->store_result();
echo $stmt->num_rows;
/*if($stmt->num_rows == 1){
$result = $stmt->get_result();
$row = $result->fetch_assoc();
print_r($row);
// here is where you could verify the password
if(password_verify($password, $row['Password'])) {
// good password
echo 'all good!';
}
} else {
//echo "failed to find row";
}*/

最佳答案

我已经解决了这个问题。当我去获取时,我需要将其存储在一个单独的变量中,而不是用户存储输入的密码的变量中。这可以反射(reflect)在下面的代码中:

<?php
require 'connection.php';
$username = $_POST['username'];
$password = $_POST['password'];
$dbusername = ""; //These two being the new variables
$dbpassword = "";

if($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}

$stmt = $conn->prepare("SELECT Username, Password FROM users WHERE Username = ?;");
$stmt->bind_param('s', $username);
$stmt->execute();
if($stmt->execute() == true){
$stmt->bind_result($dbusername, $dbpassword);
$stmt->fetch();
if(password_verify($password, $dbpassword)) {
echo 'successful';
}
else{
echo 'failed';
}
}
else{
echo 'failed';
}

?>

关于PHP 准备好的语句 - 无法从数据库检索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41831253/

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