不能检查同一个if语句中是否有超过1个post数据集


Can't check if more than 1 post data isset in the same if statement

我正在尝试验证数据,但我遇到了麻烦。

问题是我正在检查是否设置了某些帖子数据。问题是它要么总是设置或不,我认为这是因为我试图检查多个东西在一个不正确的格式在我的if语句?

我想只有这一行是不对的,但是我有某种思维障碍。

我知道这可能是一件微不足道的事情,但我想不出一个方法来表达它,通过谷歌找到解决方案。

if ((!isset($_POST['first_name'])) || (!isset($_POST['contact_no'])) || (!isset($_POST['device'])) || (!isset($_POST['fault']))) {
echo 'no Name, contact number, device or fault given.';
}

我知道我可以检查单独的if,但我真的想刷新我如何检查同一语句中的多个项目..

在尝试建议后,没有任何运气,添加更多信息:

if (isset($_POST['posted']) == 'TRUE') {
$error = array();
    if( !isset($_POST['first_name']) ){ $error[] = 'no Name given.'; }
elseif( !isset($_POST['contact_no']) ){ $error[] = 'no contact number given.'; }
elseif( !isset($_POST['device']) ) {    $error[] = 'no Device given.'; }
elseif( !isset($_POST['fault']) ) {     $error[] = 'no fault given.'; }
if( count($error)!==0){
    echo 'Error(s) occured:<br />'.implode('<br />', $errors);
}
else {
$db->query("INSERT INTO repairs (r_oem, r_device, r_mod, r_reserve, r_price, r_s_date, r_e_date, r_notes, rc_fname, rc_lname, rc_email, rc_contactno, rc_return, rc_status, rc_status_2, rc_status_txt, rc_status_txt_2, booked_by, passcode) VALUES( '$make', '$device', '$model', '$fault', '$price', '$date', '$date2', '$notes', '$fname', '$lname', '$email', '$cno', '','$status', '', '', '', '$bookedby', '$pass')");
$get_id = $db->query("SELECT LAST_INSERT_ID()");
$print_id = $db->fetch_row($get_id);
$last_r_id_insterted = $db->query("SELECT * FROM `repairs` ORDER BY `repairs`.`r_id` DESC LIMIT 1");
$plrid = $db->fetch_row($last_r_id_insterted);
echo '<br />';
echo <<<EOF
<script>
alert("Successfully created record for {$fname} {$lname}''s {$make} {$device} {$model}. Job Reference: {$plrid['r_id']}");
 </script>
 EOF;
 print "<span style='float: left; margin-left:25px;'><a class='button positive' 'href='"print_label.php?ref={$plrid['r_id']}'" target='"_blank'">Printable Sticky Label.</a></span><span class='fright'><a class='button positive' href='"print_card.php?ref={$plrid['r_id']}'" target='"_blank'">Printable Customer Card.</a></span><br /><br />";
if (isset($_POST['item1'])) {
        $db->query("UPDATE repairs SET part1id={$_POST['item1']} WHERE r_id={$plrid['r_id']}");
        $db->query("UPDATE stock SET commited=commited+1, s_count=s_count-1 WHERE id={$_POST['item1']}");   
}
if (isset($_POST['item2'])) {
        $db->query("UPDATE repairs SET part2id={$_POST['item2']} WHERE r_id={$plrid['r_id']}");
        $db->query("UPDATE stock SET commited=commited+1, s_count=s_count-1 WHERE id={$_POST['item2']}");
}
if (isset($_POST['item3'])) {
        $db->query("UPDATE repairs SET part3id={$_POST['item3']} WHERE r_id={$plrid['r_id']}");
        $db->query("UPDATE stock SET commited=commited+1, s_count=s_count-1 WHERE id={$_POST['item3']}");
}
if (isset($_POST['item4'])) {
        $db->query("UPDATE repairs SET part4id={$_POST['item4']} WHERE r_id={$plrid['r_id']}");
        $db->query("UPDATE stock SET commited=commited+1, s_count=s_count-1 WHERE id={$_POST['item4']}");           
}
if (isset($_POST['item5'])) {
        $db->query("UPDATE repairs SET part5id={$_POST['item5']} WHERE r_id={$plrid['r_id']}");
        $db->query("UPDATE stock SET commited=commited+1, s_count=s_count-1 WHERE id={$_POST['item5']}");
}
else {
    print "No Parts Selected<br />";
}
}
}

您可以使用

检查$_POST变量
    if(isset($_POST))
    {
    }

,然后验证$_POST var

的每个键
if(isset($_POST))
{
    if(!isset($_POST['first_name']))
     echo "no name";
    elseif(!isset($_POST['contact_no']))
     echo "no number";
}

我想你应该用

$_POST['first_name'] == ""

我相信,只要字段first_name包含在表单中,它就会被设置(),即使它不包含任何数据。

示例代码(提交有和没有填写输入字段的表单,结果相同:

<?php
if (isset($_POST['first_name'])) {
        echo "it's set!";
}
?>
<form method='post'>
        <input type='text' name='first_name'/>
        <input type='submit' value='submit'/>
</form>

首先,你可以通过连接isset:

中的值来压缩它
if ( isset($_POST['first_name'], $_POST['contact_no'], $_POST['device'], $_POST['fault']) ) {}

请注意,我不使用!反转。如果其中一个值没有设置,isset()返回false。
我想你是在找if( isset($_POST) ){ /* ... */ }


除此之外,我不太确定你的意思,但我认为是这样的:

if( isset($_POST) ){
        if( !isset($_POST['first_name']) ){ echo 'no Name given.'; }
    elseif( !isset($_POST['contact_no']) ){ echo 'no contact number given.'; }
    elseif( !isset($_POST['device']) ) {    echo 'no Device given.'; }
    elseif( !isset($_POST['fault']) ) {     echo 'no fault given.'; }
    else{
       echo "everything OK";
    }
}

另一个方法是连接字符串:

if( isset($_POST) ){
    $error = array();
        if( !isset($_POST['first_name']) ){ $error[] = 'no Name given.'; }
    elseif( !isset($_POST['contact_no']) ){ $error[] = 'no contact number given.'; }
    elseif( !isset($_POST['device']) ) {    $error[] = 'no Device given.'; }
    elseif( !isset($_POST['fault']) ) {     $error[] = 'no fault given.'; }
    if( count($error)!==0){
        echo 'Error(s) occured:<br />'.implode('<br />', $errors);
    }
    else{
       echo "everything OK";
    }
}

我可能会创建一个函数:

function fieldIsSet($field) {
  if(is_array($field) {
    foreach($field as $f) {
      if( ! fieldIsSet($f)) {
        return false;
      }
    }
  } else {
    if( !isset($_POST[$field]) || empty($_POST[$field])) {
      return false;
    }
  }
  return true;
}

用法:

// with an string[]
fieldIsSet(array('first_name', 'contact_no', 'device', 'fault')) or echo 'no Name, contact number, device or fault given.';
// with a single string
fieldIsSet('first_name') or echo 'no Name given.';

我认为可以通过使用

if ($ _POST.length<4){…代码…},

假设您通过post请求只发送4个变量