Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 493
NoName84 Avatar asked Jan 06 '23 17:01

NoName84


2 Answers

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 != '')`
like image 96
random_user_name Avatar answered Jan 16 '23 21:01

random_user_name


Change this line

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

to

if ($album_name!='' && isset($artist, $company, $price, $buy_now)) {
like image 43
slbteam08 Avatar answered Jan 16 '23 20:01

slbteam08