1

hi2all 我有一个表,其中包含 2 个这样的字段


|姓名| 图像 _ | __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _

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

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

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

$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 . "\">
";
4

1 回答 1

0

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

将您的 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函数,将先前的路径作为参数。

我希望这对你有用。

于 2013-04-26T16:16:49.090 回答