错误的查询顺序,消息(产品已成功添加)

时间:2018-04-11 02:15:47

标签: php

有没有办法在添加错误的项目后删除添加成功的产品消息

E.G。首先添加项目,然后添加照片,如果我添加除jpg或png以外的任何内容。它不会添加项目,您将获得的错误消息(照片未添加。请仅上传JPG或PNG照片!)之后(产品已成功添加!)。

PHP代码

sudo -u <username> -H <command>

谢谢大家!

2 个答案:

答案 0 :(得分:0)

<?php
    include('session.php');

    $name=$_POST['name'];
    $category=$_POST['category'];
    $price=$_POST['price'];
    $supplier=$_POST['supplier'];
    $qty=$_POST['qty'];


    if($qty == 0){
echo "<script language='JavaScript'>
window.alert('Quantity cannot be 0');
window.location.href='product.php';</script>";
exit;
}

    if($price == 0){
echo "<script language='JavaScript'>
window.alert('Price cannot be 0');
window.location.href='product.php';</script>";
exit;
}
    $fileInfo = PATHINFO($_FILES["image"]["name"]);

    if (empty($_FILES["image"]["name"])){
        $location="";
    }
    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="";
            ?>
                <script>
                    window.alert('Photo not added. Please upload JPG or PNG photo only!');
window.location.href='product.php';
                </script>
            <?php
            exit;
        }
    }

    mysqli_query($conn,"insert into product (product_name,categoryid,product_price,product_qty,photo, supplierid) values ('$name','$category','$price','$qty','$location', '$supplier')");
    $pid=mysqli_insert_id($conn);

    mysqli_query($conn,"insert into inventory (userid, action, productid, quantity, inventory_date) values ('".$_SESSION['id']."', 'Add Product', '$pid', '$qty', NOW())");

    ?>
        <script>
            window.alert('Product added successfully!');
            window.history.back();
        </script>
    <?php
?>

只需添加退出;在window.alert之后(&#39;照片未添加。请仅上传JPG或PNG照片!&#39;);

答案 1 :(得分:0)

我理解的是,您希望在上传任何图片时验证图片。你可以使用代码,我想这会对你有帮助

$target_dir = "uploads/";
$target_file = basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}else{
// check image is empty or not
if(!empty($_FILES["image"]["name"])){
      if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_dir.$target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
        $location = $target_dir.$target_file;
    } else {
        echo "Sorry, there was an error uploading your file.";
        $location = "";
    }
}
}