无法将图片上传变量设置为 NULL
Can't set image upload variable to NULL
如此奇怪又烦人的问题,我正在尝试从表单上传图片(一切正常)但是当没有图片时它应该将自己设置为 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,改为先设置值,只有在设置
时才赋值
同样的事情,见下面的输出截图。
你可以试试
$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;
}
}
如此奇怪又烦人的问题,我正在尝试从表单上传图片(一切正常)但是当没有图片时它应该将自己设置为 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,改为先设置值,只有在设置
时才赋值同样的事情,见下面的输出截图。
你可以试试
$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;
}
}