hi2all我有一个包含2个字段的表格
|名称| imags 的 _ | __ _ __ _ __ _ __ _ __ < EM> _ __ _ __ _ __ _ __ _
当用户在我的网站上登录时,我在会话中注册了它的名字,当他上传新照片时
我想删除它的旧照片,并将他的名字设置为新照片怎么做?
我在db中插入每个上传图像的代码如下所示我应该添加到我的
$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 . "\">
";
答案 0 :(得分: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函数将上一个路径作为参数。
我希望这适合你。