当我上传jpg或png以外的内容时,它表示照片未更新。请仅上传JPG或PNG照片!但在此之后会有一条跟进消息,产品已成功更新。 我想只删除后续消息。
<?php
include('session.php');
$id=$_GET['id'];
$p=mysqli_query($conn,"select * from product where productid='$id'");
$prow=mysqli_fetch_array($p);
$name=$_POST['name'];
$category=$_POST['category'];
$supplier=$_POST['supplier'];
$price=$_POST['price'];
$qty=$_POST['qty'];
$fileInfo = PATHINFO($_FILES["image"]["name"]);
if (empty($_FILES["image"]["name"])){
$location=$prow['photo'];
}
else{
if ($fileInfo['extension'] == "jpg" OR $fileInfo['extension'] == "png") {
$newFilename = $fileInfo['filename'] . "_" . time() . "." . $fileInfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"], "../upload/" . $newFilename);
$location = "upload/" . $newFilename;
}
else{
$location=$prow['photo'];
?>
<script>
window.alert('Photo not updated. Please upload JPG or PNG photo only!');
</script>
<?php
}
}
mysqli_query($conn,"update product set product_name='$name', supplierid='$supplier', categoryid='$category', product_price='$price', photo='$location', product_qty='$qty' where productid='$id'");
if($qty!=$prow['product_qty']){
mysqli_query($conn,"insert into inventory (userid,action,productid,quantity,inventory_date) values ('".$_SESSION['id']."','Update Quantity', '$id', '$qty', NOW())");
}
?>
<script>
window.alert('Product updated successfully!');
window.history.back();
</script>
<?php
?>
答案 0 :(得分:0)
有很多方法可以解决这个问题。 尝试一种可能的解决方案
<?php
include('session.php');
$id=$_GET['id'];
$p=mysqli_query($conn,"select * from product where productid='$id'");
$prow=mysqli_fetch_array($p);
$name=$_POST['name'];
$category=$_POST['category'];
$supplier=$_POST['supplier'];
$price=$_POST['price'];
$qty=$_POST['qty'];
$fileInfo = PATHINFO($_FILES["image"]["name"]);
$flag = 0;
if (empty($_FILES["image"]["name"])){
$location=$prow['photo'];
$flag = 1;
}
else{
if ($fileInfo['extension'] == "jpg" OR $fileInfo['extension'] == "png") {
$newFilename = $fileInfo['filename'] . "_" . time() . "." . $fileInfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"], "../upload/" . $newFilename);
$location = "upload/" . $newFilename;
$flag = 1;
}
else{
$location=$prow['photo'];
?>
<script>
window.alert('Photo not updated. Please upload JPG or PNG photo only!');
</script>
<?php
}
}
if($flag)
{
mysqli_query($conn,"update product set product_name='$name', supplierid='$supplier', categoryid='$category', product_price='$price', photo='$location', product_qty='$qty' where productid='$id'");
if($qty!=$prow['product_qty']){
mysqli_query($conn,"insert into inventory (userid,action,productid,quantity,inventory_date) values ('".$_SESSION['id']."','Update Quantity', '$id', '$qty', NOW())");
}
?>
<script>
window.alert('Product updated successfully!');
window.history.back();
</script>
<?php
}
?>
答案 1 :(得分:0)
如果条件差不多,请将更新代码移到内部 - 例如
..
..
if ($fileInfo['extension'] == "jpg" OR $fileInfo['extension'] == "png") {
$newFilename = $fileInfo['filename'] . "_" . time() . "." . $fileInfo['extension'];
move_uploaded_file($_FILES["image"]["tmp_name"], "../upload/" . $newFilename);
$location = "upload/" . $newFilename;
// write your update query here
mysqli_query($conn,"update product set product_name='$name', supplierid='$supplier', categoryid='$category', product_price='$price', photo='$location', product_qty='$qty' where productid='$id'");
if($qty!=$prow['product_qty']){
mysqli_query($conn,"insert into inventory (userid,action,productid,quantity,inventory_date) values ('".$_SESSION['id']."','Update Quantity', '$id', '$qty', NOW())");
}
?>
<script>
window.alert('Product updated successfully!');
window.history.back();
</script>
<?php
else{
$location=$prow['photo'];
?>
<script>
window.alert('Photo not updated. Please upload JPG or PNG photo only!');
</script>
<?php
}
?>
或者您可以使用@Nirali建议的标志
答案 2 :(得分:-1)
# Please check below image upload code (Please do not forget it :- enctype="multipart/form-data") #
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
# The "upload.php" file contains the code for uploading a file #
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>