gpt4 book ai didi

php - 需要从数据库表中删除旧图像名称

转载 作者:行者123 更新时间:2023-11-30 23:19:18 24 4
gpt4 key购买 nike

hi2all 我有一个包含 2 个字段的表格


|名字|图片_|______________< em>_____________

当用户登录我的网站时,我会在 session 中注册它的名字,当他上传他的新照片时

我想删除它的旧照片并将他的名字设置为新照片怎么办???

我将每个上传的图像插入数据库的代码如下我应该添加到我的

$me=$_SESSION['logged-in'];


$con=mysqli_connect("localhost","root","","register_form");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



@mysqli_query($con,"INSERT INTO imgs (name, imags)
VALUES ('$me', '$filename')");



mysqli_close($con);

这是我的代码来展示它

  <?php




$me=$_SESSION['logged-in'];
$con=mysqli_connect("localhost","root","","register_form");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT imags FROM imgs where name LIKE '$me' ");


while($row = mysqli_fetch_array($result))
{
$pathimg=$row['imags'];

$d = dir("image");

$entry = $d->read();
/*while (false !== ($entry = $d->read())) {
echo "<li><img src=\"/ConnectMe/html/image/" . $entry . "\">" . $entry . "</li>";
}

*/
}

mysqli_close($con);

echo"

<img width='50' height='60' src=\"/ConnectMe/html/image/$pathimg" . @$entry . "\">
";

最佳答案

如果我理解你的意思,你想在插入新照片之前删除包含当前登录名称的旧表行。

将您的 INSERT 语句更改为该行代码。

检查表中是否有包含该名称的行。如果是这样,请用新照片更新该行。如果不只是使用 INSERT 语句插入新行。

此链接可能会有帮助:mysqli_affected_rows .

$result = mysqli_query($con,"SELECT * FROM imgs WHERE name = '$me'");
if(mysqli_affected_rows() == 1) //it means that user uploaded a photo before
{
$update = mysqli_query($con, "UPDATE imgs SET imgs = '$filename' WHERE name = '$me'");
if(mysqli_affected_rows() == 1) //it means that UPDATE was successfull
{echo 'Your photo was updated successfully';}
}
else
{
@mysqli_query($con,"INSERT INTO imgs (name, imags) VALUES ('$me', '$filename')");
}

如果你想从文件系统中删除文件你应该使用unlink将先前路径作为参数的函数。

我希望这对你有用。

关于php - 需要从数据库表中删除旧图像名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240493/

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