Php 更新记录,不更改当前图像
嗨,我是PHP新手,我试图在不改变图像的情况下更新记录。我尝试了很多事情,但都失败了。 当使用当前图像进行更新时,它会给出“允许的图像扩展”错误,否则在im添加新图像时效果良好。 你能帮忙吗 谢谢Php 更新记录,不更改当前图像,php,mysql,pdo,Php,Mysql,Pdo,嗨,我是PHP新手,我试图在不改变图像的情况下更新记录。我尝试了很多事情,但都失败了。 当使用当前图像进行更新时,它会给出“允许的图像扩展”错误,否则在im添加新图像时效果良好。 你能帮忙吗 谢谢 <?php $getIdfromUrl = $_GET['id']; if (isset($_POST["Submit"])) { $titlePost = $_POST["PostTitle"]; $categoryP
<?php
$getIdfromUrl = $_GET['id'];
if (isset($_POST["Submit"])) {
$titlePost = $_POST["PostTitle"];
$categoryPost = $_POST["Category"];
$Admin = "admin";
$file= $_FILES['Image'];
$fileName = $_FILES['Image']['name'];
$fileTmpName = $_FILES['Image']['tmp_name'];
$fileSize = $_FILES['Image']['size'];
$fileError = $_FILES['Image']['error'];
$fileType = $_FILES['Image']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png','gif','tiff'); // allow specific extensions
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0 ) {
if ($fileSize < 3000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt; //uniqueid
$fileDestination = 'uploads/'.$fileNameNew;
} else {
$_SESSION["errorMessage"]= "Image Size No More Than 3MB";
redirect_To("posts.php");
}
} else {
$_SESSION["errorMessage"]= "Something with the Image";
redirect_To("posts.php");
}
else {
$_SESSION["errorMessage"]= "Allowed jpg,jpeg,png,gif only";
redirect_To("posts.php");
}
}
$textPost = $_POST["PostDescription"];
$currentTime=time();
$dateTime = strftime("%d-%m-%Y %H: %M: %S",$currentTime);
$connectingDB;
if (!empty($_FILES['Image']['name'])) {
$sql="UPDATE posts SET title='$titlePost', category='$categoryPost', post='$textPost',
datetime='$dateTime', image='$fileNameNew' WHERE id='$getIdfromUrl'";
} else {
$sql="UPDATE posts SET title='$titlePost', category='$categoryPost', post='$textPost',
datetime='$dateTime' WHERE id='$getIdfromUrl'";
}
警告:使用PDO时,应与占位符值一起使用,并将任何用户数据作为单独的参数提供。在这段代码中,您可能会遇到严重的问题。永远不要使用字符串插值或串联,而是直接在查询中使用并且永远不要放置$\u POST
、$\u GET
或任何用户数据。有关一般指导和建议,请参阅。通过检查$\u FILES['Image']['name']
是否为空,您只能在最后区分这两种情况-您需要提前执行此操作(或在多个位置重复相同的检查),以便检查允许的文件后缀,也只有当这不是空的时候才会发生。hanks@tadman供参考。CBroe好的,我把空字符串移到if(in_数组)上面,它工作了,但是为什么它不更新当前的帖子描述和日期时间,但是它更新了一个空字符串?好的,修复了它,再次感谢