NoName84
NoName84

Reputation: 407

PHP - if statement - with - if isset - using multiple variables issue

I am having a problem of syntax my code correctly and i would like some help. I would like to use an if statement and if isset with multiple variables. My existing code is this.

$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">';
}

?>

How can i combine them?

Thanks.

Upvotes: 4

Views: 4373

Answers (4)

Sanchit Gupta
Sanchit Gupta

Reputation: 3224

Try to use like:

$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">';
}

For more detail about isset(): http://php.net/manual/en/function.isset.php

Upvotes: 2

random_user_name
random_user_name

Reputation: 26160

Personally, I handle form submissions completely differently.

BUT, given the structure you have provided, I would recommend doing things a bit differently for ease:

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

Upvotes: 3

Jatin Luthra
Jatin Luthra

Reputation: 176

Change the line to

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

This will work for sure

Upvotes: 2

slbteam08
slbteam08

Reputation: 681

Change this line

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

to

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

Upvotes: 2

Related Questions