gpt4 book ai didi

php - 更新表 php-mysql 中的值

转载 作者:行者123 更新时间:2023-11-29 05:10:47 26 4
gpt4 key购买 nike

我有这张表:

+----+------------+-----------------------+
| ID | ID_PRODUCT | LINK_DOWNLOAD |
+----+------------+-----------------------+
| 1 | 2369 | folder/2015.03.12.pdf |
| 2 | 3694 | folder/2014.01.10.pdf |
| 3 | 56 | folder/2016.09.25.pdf |
+----+------------+-----------------------+

结束代码 php:

  <form action="upload_file.php" method="post" enctype="multipart/form-data" name="upload_file"><br>
<label for="UploadFileField"></label><br>
<input type="file" name="UploadFileField"/>
<input type="submit" name="UploadButton" value="Upload" /><br>
<input type ="text" name="UPLOAD_COD" />
</form>


<?php
include "bd_cnx.php";
if (isset ($_FILES['UploadFileField'])){
$UploadName = $_FILES['UploadFileField'] ['name'];
$UploadName = mt_rand (100000, 999999).$UploadName;
$UploadTmp = $_FILES['UploadFileField'] ['tmp_name'];
$UploadType = $_FILES['UploadFileField'] ['type'];
$FileSize = $_FILES['UploadFileField'] ['size'];

$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);

if(($FileSize > 1250000)){
die ("Error - File to big");
}

if(!$UploadTmp) {
die ("No File Selected");
}
else {
if (move_uploaded_file($UploadTmp, "Upload/$UploadName")) {
$UPLOAD_COD = $_POST['UPLOAD_COD'];
$sql = "INSERT INTO download (ID_PRODUCT,LINK_DOWNLOAD) VALUES ('$UPLOAD_COD','Upload/$UploadName')";
$result = $conn->query($sql);
echo '<script> alert("Successfully inserted"); window.location ="/index.php";</script>';
}
}
}
?>

2015.03.12.pdf 文件今天更新并成为 2016.09.25.pdf 。当我在输入“UPLOAD_COD”中输入数字 2369 时,我想从服务器中删除文件 2015.03.12.pdf 并替换为 2016.09.25.pdf,并在 LINK_DOWNLOAD 列中更新到新链接(文件夹/2015.03.12.pdf)。谢谢!

最佳答案

所以你想删除 ID_PRODUCT = 2369 的文件?只需从数据库中获取 LINK_DOWNLOAD

上传并插入文件后放置此代码

//first create a query that select the row of the data that you are finding
$currentSql = "Select * from download where ID_PRODUCT = '".$UPLOAD_COD."'";
$current = $conn->query($currentSql);


// then fetch it's result then unlink the file from the server
while($row = $current->fetch_assoc()) {
$file = $row['LINK_DOWNLOAD'];
// check if file is exist then delete
if (file_exists($file)) {
unlink($file);
}
}

关于php - 更新表 php-mysql 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684678/

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