繁体   English   中英

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

[英]PHP - if statement - with - if isset - using multiple variables issue

我正确的语法问题我的代码,我想要一些帮助。 我想使用if语句,如果使用多个变量。 我现有的代码是这样的。

$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 != '')`

改变这一行

if($album_name !='') && if (isset($artist, $company, $price, $buy_now)){

if ($album_name!='' && isset($artist, $company, $price, $buy_now)) {

将行更改为

if (isset($artist, $company, $price, $buy_now)){
if($album_name !=''){
}
}

这肯定会起作用

尝试使用像:

$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(!empty($album_name) && isset($album_name,$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">';
}

有关isset()更多详细信息: http//php.net/manual/en/function.isset.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM