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

时间:2016-02-02 17:35:48

标签: php mysql if-statement isset

我的代码语法问题正确,我想要一些帮助。我想使用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">';
}

?>

我如何将它们结合起来?

感谢。

4 个答案:

答案 0 :(得分:3)

就个人而言,我完全不同地处理表单提交。

但是,考虑到你提供的结构,我建议做一些不同的事情,以便轻松:

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

答案 1 :(得分:2)

更改此行

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

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

答案 2 :(得分:2)

将行更改为

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

这肯定会有用

答案 3 :(得分:2)

尝试使用:

#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <CUnit/Console.h>

int suite_init(void) {
    return 0;
}

int suite_clean(void) {
    return 0;
}

void test_example(void) {
    CU_ASSERT(3 == 2);
}

void gradle_cunit_register() {
    CU_pSuite mySuite = CU_add_suite("operator tests", suite_init, suite_clean);
    CU_add_test(mySuite, "test", test_example);
}

#ifdef OUTSIDE_GRADLE
int main() {
    CU_initialize_registry();
    gradle_cunit_register();
    CU_console_run_tests();
}
#endif

有关$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">'; } http://php.net/manual/en/function.isset.php

的详细信息