gpt4 book ai didi

php - 我如何在当前登录用户的帐户中插入图像名称?

转载 作者:行者123 更新时间:2023-11-30 21:33:39 26 4
gpt4 key购买 nike

当我使用插入所有用户行的脚本图像名称时。我怎样才能插入当前 session 用户的行

auth.php

<?php
session_start();
if(!isset($_SESSION["username"]) ){
header("Location: login.php");
exit(); }
?>

home.php

    <?php

include("php-includes/auth.php");



//Include database configuration file
include_once 'php-includes/dbConfig.php';

//Get current user ID from session
$userId = $_SESSION["username"];

//Get user data from database
$result = $db->query("SELECT * FROM user WHERE username = $userId");
$row = $result->fetch_assoc();

//User profile picture
$userPicture = !empty($row['picture'])?$row['picture']:'no-image.png';
$userPictureURL = 'uploads/images/'.$userPicture;


?>

最佳答案

只需修改两处即可:

1) 你必须从 session 中获取当前用户的名字

$userId = $_SESSION['username'];

2) 给查询中的username值加上单引号。

$update = $db->query("UPDATE user SET picture = '".$fileName."' WHERE username = '$userId'");

注意:

If you are providing a value to database query, if it is non-numeric, you need to add single quotes to it.

This tells that the passed string is a value and not any MySQL reserved word/Database name/Table name/Field name.

关于php - 我如何在当前登录用户的帐户中插入图像名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55016858/

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