gpt4 book ai didi

php - 不会查询程序的更新部分

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

我很难确定我的代码问题的原因,也就是说,它不会查询“UPDATE”部分,但“SELECT”部分确实有效。当我尝试使用 print_r 函数时,它给出了一个错误/警告:

"Warning: mysqli_query(): Couldn't fetch mysqli"** and **"Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given"

if(!isset($_POST['n_pass'])&&!isset($_POST['n_pass'])){

if(!isset($_POST['password'])||$_POST['password']==""){
echo 'enter current password';
die;
} else {
include 'include/database.php';
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn,$_POST['lname']);
$email = mysqli_real_escape_string($conn,$_POST['email']);
$username = mysqli_real_escape_string($conn,$_POST['uname']);
$password = mysqli_real_escape_string($conn,$_POST['password']);

//Check if the password is equal to the password inside database
$sql = "SELECT password FROM users where id = $id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$cpass = $row['password'];
$verify_pass = password_verify($password,$cpass); //check if current password is equal to the existing password

if($verify_pass != 1){
echo 'incorrect password';
die;
} else {
**//Update Data
$sql="UPDATE users SET firstname=$fname, lastname=$lname, email=$email, username=$username where id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

print_r($row['firstname']);
die;
header("Location: profile.php?successfullyupdated");
}
}
}

最佳答案

您的查询缺少字符串周围的引号:

$sql="UPDATE users SET firstname='$fname', lastname='$lname', email='$email', username='$username' where id=$id";

您只能跳过 id 字段,因为它是一个整数。

旁注:您对 SQL 注入(inject)持开放态度。您应该使用准备好的语句。google 上有很多资源可以从这个主题开始

最后,请注意

$row = mysqli_fetch_assoc($result); 
print_r($row['firstname']);

完全没用,因为您没有从 UPDATE 查询返回任何内容。你可以这样做:

if(mysqli_query($conn, $sql)){
//query was successful - run your code here for success
}else{
//query failed - run your code here for fail
}

关于php - 不会查询程序的更新部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52429452/

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