PHP - if 语句 - with - if isset - 使用多个变量问题

标签 php mysql if-statement isset

我的代码语法有问题,我需要一些帮助。我想使用 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/

相关文章:

PHP:cURL 并跟踪所有重定向

mysql - 跨多个表查找记录的最小和最大日期

mysql查询以获取特定日期之间的记录

mysql子查询取决于字段值

c - Arduino增量运算符不影响变量吗?

php - 为大型网站设计移动网站,需要您的意见

javascript - 从 php 传递编码变量并在 javascript 中解码?

php - 如何一次更改所有学生的年级

php - 更新选项在 PHP 的购物车中不起作用

javascript - 我的 JavaScript 函数/if 语句仅在按特定顺序放置时才有效