我的代码即使没有显示错误也正在插入数据

时间:2018-10-03 10:15:53

标签: php

代码正在插入数据而没有显示错误。就像当我放置一个大于2mb的图像时,它仍然提交数据。我不明白为什么会这样。

代码如下:

$err = "";
$db = $user->getDb();
$value = "SELECT * FROM payment_properties WHERE id=1";
$query = $db->query($value) or die($db->error);
$data = $query->fetch_array(MYSQLI_ASSOC);
$admin_price = $data['admin_cash'];

if (isset($_REQUEST['submit'])) {

    $admin_id = $user->validate($_POST['admin_id']);
    $p_brand = $user->validate($_POST['pbrand']);
    $p_cat = $user->validate($_POST['pcat']);
    $p_title = $user->validate($_POST['ptitle']);
    $p_price = $user->validate($_POST['pprice'] + $admin_price);
    $p_desc = $user->validate($_POST['pinfo']);
    $p_key =$user->validate($_POST['pkey']);
    $p_qty =$user->validate($_POST['pqty']);
    $image = $user->validate($_FILES['avatar']['name']);
    $tmp_dir = $_FILES['avatar']['tmp_name'];
    $imgSize = $_FILES['avatar']['size'];


    $upload_dir = '../../product_images/'; // upload directory. My problem

    $imgExt = strtolower(pathinfo($image, PATHINFO_EXTENSION)); // get image extension

    // valid image extensions
    $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions

    // rename uploading image
    $image = rand(25, 100) . "." . $imgExt;

    if (in_array($imgExt, $valid_extensions)) {
        if ($imgSize < 2000000) {
            move_uploaded_file($tmp_dir, $upload_dir . $image);
            $value = "INSERT INTO products (product_brand, product_cat, product_title, product_price, product_desc, product_image, product_keywords, product_qty, admin_id)
            VALUES ('{$p_brand}','{$p_cat}','{$p_title}','{$p_price}','{$p_desc}','{$image}','{$p_key}','{$p_qty}','{$admin_id}')";
            $query = $db->query($value) or die($this->db->error);
            if ($query) {
                $err = "<div class='alert alert-success'>
                            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                            <b>Successfully uploaded</b>
                        </div>";
            } else {
                $err = "<div class='alert alert-danger'>
                            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                            <b>Error sending data</b>
                        </div>";
            }

        } else {

            $err = "<div class='alert alert-danger'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <b> Sorry, Image size should be less than 2mb';</b>
                    </div>";

        }
    } else {
        $err = "<div class='alert alert-success'>
                    <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                    <b>Only JPG, JPEG, PNG & GIF files are allowed.</b>
                </div>";
    }
}

其余的错误在我测试时也不会显示。

1 个答案:

答案 0 :(得分:0)

尝试使用getimagesize函数来计算图像尺寸。

$size = getimagesize($_FILES['avatar']['tmp_name']);