我的代码语法有问题,我需要一些帮助。我想使用 if 语句和 if isset 多个变量。我现有的代码是这样的。
$album_name = $_POST['album_name'];
$img = $_POST['image'];
$artist = $_POST['artist'];
$company = $_POST['company'];
$genre = $_POST['genre'];
$price = $_POST['price'];
$buy_now = $_POST['buy_now'];
if($album_name !='') && if (isset($artist, $company, $price, $buy_now)){
$sql = mysql_query ("INSERT INTO top_albums_info (album_name, image, artist,company,genre,price,buy_now) VALUES ('$album_name','$img','$artist','$company','$genre','$price','".$buy_now."') ");
echo'<meta http-equiv="refresh" content="0;url=../index.php?page=top_section&action=list">';
}else{
echo'<meta http-equiv="refresh" content="0;url=../index.php?page=top_section&action=add&msg=empty">';
}
?>
如何组合它们?
谢谢。
最佳答案
就个人而言,我处理表单提交的方式完全不同。
但是,鉴于您提供的结构,我建议您做一些不同的事情以方便操作:
// create a list of fields to check in an array
$fields = array(
'album_name',
'image',
'artist',
'company',
'price',
'buy_now'
);
// iterate over the fields defined in your array
foreach ($fields AS $field) {
// assign the value to the variable ONLY IF the $_POST is set, otherwise empty string
${$field} = (isset($_POST[$field])) ? $_POST[$field] : '';
}
// Now you KNOW its set, so you just check if the field "is"
if ($album_name && $image && $artist && $company && $price && $buy_now) {
// Do stuff. Form submitted and complete
} else {
// Do other stuff. Form not submitted or incomplete
}
When you write `if ($album)`, it's essentially the same as `if ($album != '')`
关于PHP - if 语句 - with - if isset - 使用多个变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160160/