gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:17:46 24 4
gpt4 key购买 nike

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

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

关于PHP - if 语句 - with - if isset - 使用多个变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35160160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com