我正在制作一个表单,用于在电子商务项目中插入管理员产品。在验证表单数据期间,我尝试检查表单数据是否为空。
因此我引入了一个if
语句并检查了从表单传递的所有参数的值,并在if
块中写了一个echo语句。
这是我的代码:
<? php
//text variables
if(isset($_POST['insert_product'])){
$product_title = $_POST['product_title'];
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_status = 'on';
$product_keywords = $_POST['product_keywords'];
//images variables
$product_img1 = $FILES['product_img1']['name'];
$product_img2 = $FILES['product_img2']['name'];
$product_img3 = $FILES['product_img3']['name'];
//temp names
$temp_name1 = $FILES['product_img1']['tmp_name'];
$temp_name2 = $FILES['product_img2']['tmp_name'];
$temp_name3 = $FILES['product_img3']['tmp_name'];
//validation and insertion query
if($product_title == '' OR $product_cat == '' OR $product_brand == '' OR $product_keywords == '' OR $product_img1 == '' OR $product_price == ''){
echo"<script>alert('you have not entered all the values')</script>";
exit();
}
}
?>
正在产生类似
的输出(...提醒('你没有输入所有值');“; exit();}} ....)
请帮我解决这个问题。
我正在使用Sublime文本并在Notepad ++中检查相同但是它给出了同样的错误。
答案 0 :(得分:0)
这对我来说很好。尝试在循环中迭代$ _POST,以这种方式检查值。它会压缩您的代码并使将来更容易阅读:
<?php
if (isset($_POST)) {
foreach ($_POST as $key => $value) {
if (!isset($_POST[$key]) || $value == "") {
echo "<script> alert('you have not entered all the values'); </script>";
exit;
}
}
}
也就是说,如果您使用用户提供的数据,您应该尝试保留$ _POST并使用已清理的字符串:
<?php
if (isset($_POST)) {
// Clean a string, making it safe for using with a database.
function clean_string($string) {
global $mysqli;
$string = $mysqli->real_escape_string($string);
stripslashes($string);
return $string;
}
foreach ($_POST as $key => $value) {
if (!isset($_POST) || $value == "") {
echo "<script> alert('you have not entered all the values'); </script>";
exit;
}
// If you're going to be using this $_POST data with a database you should also
// clean the strings before using them in any way.
$Data = new stdClass();
foreach ($_POST as $key => $value) {
if (isset($_POST[$key]) && $_POST[$key] !== "") {
$Data->$key = clean_string($value);
}
}
}
// All of the $_POSTed items are set and cleaned for use. Do what you will with them below.
}
答案 1 :(得分:0)
echo"<script>alert('you have not entered all the values');</script>";
使用这个......你忘记了semicon之后)