IMG dir 不能存储在 db 中,而是从查询中使用的相同变量中查看

2022-08-30 23:34:24

我正在尝试使用Sanwebe的以下表单将图像上传到我的服务器。可以在这里找到。但是,当我按上传时,新的拇指加载完全正常。但是,我的图像无法使用与查看图像完全相同的变量上传到数据库。怎么会这样?我尝试将信息放在查询的前面。喜欢这个:db

echo '<div align="center">';
echo '<img src="images/profile-pictures/'.$thumb_prefix . $new_file_name.'" alt="Thumbnail">';
echo '</div>';

$profile_pic_temp = "../images/profile-pictures/" . $thumb_prefix . $new_file_name;
$profile_pic_full_temp = "../images/profile-pictures/" . $new_file_name;
$session_user = $_SESSION['user_confirm'];

require 'database.php';

$profile_pic_db_upload = $db->prepare("UPDATE login SET profile_picture_temp = :profile_pic_temp, profile_picture_full_temp = :profile_pic_full_temp WHERE user_session = :session_user");
$profile_pic_db_upload->bindParam(':session_user', $session_user, PDO::PARAM_STR);
$profile_pic_db_upload->bindParam(':profile_pic_temp', $profile_picture_temp, PDO::PARAM_STR);
$profile_pic_db_upload->bindParam(':profile_pic_full_temp', $profile_picture_full_temp, PDO::PARAM_STR);
$profile_pic_db_upload->execute();
$confirm_upload_db = $profile_pic_db_upload->rowCount();

if($confirm_upload_db != 0){
    $popup_message = "Profile picture has been uploaded.";
    echo $popup_message;
}
else{
    $popup_message = "Profile picture could not be uploaded.";
    echo $popup_message;
}

编辑二:查询现在运行,但是,我收到反馈“无法上传个人资料图片”。为什么查询无法正常运行?

编辑四:我已尝试将 更改为。然后我上传成功,但是,该值仅插入并设置为0。绑定参数以某种方式更改了值。为什么?user_session = :session_userid = 1profile_picture_temp

编辑三:我现在已经尝试过使用。同样的结果在这里。无法上载返回。但是, 不会更改 DB 中的值。mysqli

$sql = "UPDATE login SET profile_picture_temp = ? AND profile_picture_full_temp = ? WHERE user_session = ?";
$stmt = $mysqli->prepare($sql) or die ("Database error<br>" . $sql . "<br><b>Error message:</b> " . $mysqli->error);
$stmt->bind_param("sss", $profile_picture_temp, $profile_picture_full_temp, $session_user);
$stmt->execute() or die("Something went wrong");
if($stmt->fetch()){
    $popup_message = "Profile picture has been uploaded.";
    echo $popup_message;
}
else{
    $popup_message = "Profile picture could not be uploaded.";
    echo $popup_message;
}
$stmt->free_result();
$stmt->close();

答案 1

您确定此行不会引发PHP错误吗...

$confirm_upload_db = $$profile_pic_db_upload->rowCount();
                     ^^

(两个美元符号)是我们引用变量的方式;但不包含另一个变量的名称,它是对 PDO 语句对象的引用。$$$profile_pic_db_upload

另一个注意事项。该函数返回受语句影响的行数;如果 UPDATE 语句成功,但未对行进行任何实际更改(因为分配给列的值与列中已存储的值相同),则将返回 0。rowCount()UPDATErowCount()

(要更改该行为,使其返回匹配的行数,可以使用 )。PDO::MYSQL_ATTR_FOUND_ROWS


答案 2

使用以下查询修复了该问题:

$profile_picture_temp = "../images/profile-pictures/" . $thumb_prefix . $new_file_name;
$profile_picture_full_temp = "../images/profile-pictures/" . $new_file_name;
$session_user = $_SESSION['user_confirm'];

$sql = "UPDATE login l SET l.profile_picture_temp = ?, l.profile_picture_full_temp = ? WHERE l.user_session = ?";
$stmt = $mysqli->prepare($sql) or die ("Database error<br>" . $sql . "<br><b>Error message:</b> " . $mysqli->error);
$stmt->bind_param("sss", $profile_picture_temp, $profile_picture_full_temp, $session_user);
$stmt->execute() or die("Something went wrong");

$result = $stmt->affected_rows;
if($result == 1){
    $popup_message = "Profile picture has been uploaded.";
    echo $popup_message;
}
else{
    $popup_message = "Profile picture could not be uploaded.";
echo $popup_message;
}
$stmt->free_result();
$stmt->close();

我无法识别问题本身,但是,我设法通过添加.使用别名以某种方式修复了它。UPDATE login l


推荐