检查数组值 - PHP

时间:2016-05-10 10:00:53

标签: php

如何检查数组中的条件,例如

for ($i=0; $i<count($_FILES['uploaded']['name']); $i++) {
    $tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
    $filesize = $_FILES['uploaded']['size'][$i];

    if ($tmpFilePath != '' || !empty($tmpFilePath)) {
        // if condition is true upload the images and stored in db.
    } else {
        //no image uploaded.
   }
}

在我的情况下,用户最多可以上传3张图片。在上述条件下,如何检查:

Array
    (
        [1] => filename
        [2] => filename
        [3] => 
    )

以上仍然是 true ,但在我的代码中,它会变为false。

  

解决方案

for ($i=0; $i < count($_FILES['uploaded']['name']); $i++) {
    $tmpFilePath = $_FILES['uploaded']['tmp_name'][$i];
    $filesize = $_FILES['uploaded']['size'][$i];

    if ($tmpFilePath != '' || !empty($tmpFilePath)) {
      // sql to insert to db here
      $result = mysqli_query($conn, $sql);
      if (mysqli_query($conn, $sql)) {
        $pr_idEnc = urlencode(encryptor('encrypt', $pr_id));
        // mysqli_close($conn); 
        header("Location: ".BASE_PATH."update-purchase.php?success&pr_id=".$pr_idEnc."");                   
      } else {
        echo "Error updating record: " . mysqli_error($conn);
        // mysqli_close($conn);
      }
}
mysqli_free_result($result);
mysqli_close($conn); // edit here 

找出问题源于mysqli_close($conn)内的for loop。如果我的编码错误或可以改进,请告诉我或告诉我。谢谢

0 个答案:

没有答案