【问题标题】:Can't set image upload variable to NULL无法将图像上传变量设置为 NULL
【发布时间】:2021-05-30 04:18:34
【问题描述】:

很奇怪很烦人的问题,我正在尝试从表单上传图像(一切正常)但是当没有图像时它应该将自己设置为 NULL,但是因为我使用的是 $target_dir 。 basename 它一直采用 $target_dir 的最后一部分而不是设置为 NULL 并将单词 products 插入数据库而不是什么都没有。但是 obv 需要 $target_dir 以便在上传时将其放置在正确的位置。请参阅下面的代码,非常感谢所有帮助。

$target_dir = "../images/products/";


      if (!isset ($_FILES["img1"]["name"])) {
        $target_file1 = NULL;
      } else {
        $target_file1 = $target_dir . basename($_FILES["img1"]["name"]);
          }

          if (!isset ($_FILES["img2"]["name"])) {
            $target_file2 = NULL;
          } else {
            $target_file2 = $target_dir . basename($_FILES["img2"]["name"]);
          }

          if (!isset ($_FILES["img3"]["name"])) {
            $target_file3 = NULL;
          } else {
            $target_file3 = $target_dir . basename($_FILES["img3"]["name"]);
          }

          if (!isset ($_FILES["img4"]["name"])) {
            $target_file4 = NULL;
          } else {
            $target_file4 = $target_dir . basename($_FILES["img4"]["name"]);
          }

SQL查询及相关信息

<pre>
$image1 = basename($target_file1);
$image2 = basename($target_file2);
$image3 = basename($target_file3);
$image4 = basename($target_file4);

echo $image1;
echo $image2;
echo $image3;
echo $image4;

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO products (product_code, product_name, category, 
filter, description, specification, img1, img2, img3, img4, price) 
VALUES('$product_code', '$product_name', '$category', '$filter', 
'$description', '$specification', '$image1', '$image2', '$image3', 
'$image4', '$price')";

if (mysqli_query($conn, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
              ?>
</pre>

K,改为先设置值,如果 isset 才赋值

同样的事情,看下面的输出截图。

【问题讨论】:

  • 你也可以添加你的 SQL 调用吗,看不出你给出的问题有多大。
  • 当我将 $target_dir 从等式中取出时,它会保留 mysql 值 Null 但不会将图像上传到目录
  • 您可以尝试在 if 语句之前将它们定义为 null,并在插入之前添加验证。因此,如果他们是 isset,您就会为您的目标增加价值。

标签: php mysql image null upload


【解决方案1】:

你可以试试

$target_dir = "../images/products/";


if (!isset ($_FILES["img1"]["name"])) {
    $target_file1 = NULL;
} else {
    if (!empty($_FILES["img1"]["name"])) {
        $target_file1 = $target_dir . basename($_FILES["img1"]["name"]);
    } else {
        $target_file1 = NULL;
    }
}

if (!isset ($_FILES["img2"]["name"])) {
    $target_file2 = NULL;
} else {
    if (!empty($_FILES["img2"]["name"])) {
        $target_file2 = $target_dir . basename($_FILES["img2"]["name"]);
    } else {
        $target_file2 = NULL;
    }
}

if (!isset ($_FILES["img3"]["name"])) {
    $target_file3 = NULL;
} else {
    if (!empty($_FILES["img3"]["name"])) {
        $target_file3 = $target_dir . basename($_FILES["img3"]["name"]);
    } else {
        $target_file3 = NULL;
    }
}

if (!isset ($_FILES["img4"]["name"])) {
    $target_file4 = NULL;
} else {
    if (!empty($_FILES["img4"]["name"])) {
        $target_file4 = $target_dir . basename($_FILES["img4"]["name"]);
    } else {
        $target_file4 = NULL;
    }
}

【讨论】:

  • 谢谢谢谢谢谢你的工作就像一个魅力。现在我只需要弄清楚我是如何破坏图像的实际上传的,哈哈。
猜你喜欢
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多