2017-04-13 62 views
0
<?php 
    $age1 = isset($_POST['age1']) ? $_POST['age1'] : false; 

    if ($age1 <= "18") 
    { 
    $sql = 'select * from events where type_id='.$id.' and id = 1 '; 
    } 
    else if (isset($_POST['age1']) > "18") 
    { 
    $sql = 'select * from events where type_id='.$id.''; 
    } 

    $tid = mysqli_query($conid, $sql); 
?> 

這裏是我的HTML代碼找不到輸出

<tr> 
    <td class="style1">Date of Birth:</td> 
     <td class="style2"><input type="text" name="dob" id="dob" class="form-control22" required autocomplete="off"></td> 
</tr> 
<tr> 
    <td class="style1">Age:</td> 
    <td class="style2"><input type="text" name="age1" id="age1" class="form-control22" autocomplete="off"></td> 
</tr> 
<tr> 
    <td class="style1">Event Type:</td> 
    <td class="style2"> 
     <select name="event_id" class="ddl" id="event_id" onchange="geteventprice()"> 
      <option value="0">--Select Event Type--</option> 
       <?php while($evt= mysqli_fetch_object($tid)){ 
       echo '<option value="'.$evt->id.'">'.$evt->name.'</option>'; } ?> 
     </select> 

    </td> 

選擇dob活動將是展會。
18歲以下是不同的事件,超過18歲是不同的年齡。在上面的代碼中,我只能得到其他值之前。

+0

假也將小於18改變在'''isset假($ _ POST [ 'AGE-1'])? $ _POST ['age1']:false;'''爲數值。 –

+0

'if(isset($ _ POST ['age1'])>「18」)' – Peon

+0

**警告**:使用mysqli時,應該使用[參數化查詢](http://php.net/manual /en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **不要**將'$ _POST','$ _GET'或**任何**用戶數據直接放入查詢中,如果有人試圖利用您的錯誤,這可能會非常有害。 – tadman

回答

0

您否則,如果條件將返回false始終即使年齡大於18,因爲isset($_POST['age1'])將返回布爾(真)和你的病情> 18永遠不會滿足

else if (isset($_POST['age1']) > "18") 


else if ($age1 > 18) 
+0

不能正常工作.... – Shri

+0

是否有任何錯誤顯示? – keviveks

+0

無錯顯示... – Shri

0

其替換此代碼因爲else if (isset($_POST['age1'])將是真或假,不能是> 18 這樣做;

$age1 = isset($_POST['age1']) ? $_POST['age1'] : false; 
if($age1) { 
    if($age1 <= 18) { 
    $sql = "below 18"; 
    } 
    if($age1 > 18) { 
    $sql = "over 18"; 
    } 
    echo $age1; 
} else { 
    echo 'no age given'; 
} 

http://sandbox.onlinephpfunctions.com/code/3f756d52ed64c2c1334f628a27312d2501b51395

+0

顯示錯誤--- if($ age1)<= 18){ – Shri

+0

有一個錯字必須是'if($ age1 <= 18){...}'if if $ age1> 18){..}'我編輯了我的回答 –

+0

@Shri您是否嘗試過編輯過的答案?也看到沙盒鏈接與工作示例 –