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.
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 != '')`
Change this line
if($album_name !='') && if (isset($artist, $company, $price, $buy_now)){
to
if ($album_name!='' && isset($artist, $company, $price, $buy_now)) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With