如果只有一个输入空白,则在php中显示错误

时间:2016-09-04 04:33:09

标签: php html sql

如果其中任何一个为空白,则显示错误消息。但它不起作用。我的意思是,如果输入和选项之间的任何一个保持为空,那么错误将显示。必须选择其中一个。

这是image of my form

<?php include "head.htm";?>
<br>
<br>
<body bgcolor="#FEF5E7">
<title>Search Profile</title>

<form method="post" action="search.php">
    <input type="text"  placeholder="Enter Student's ID/Name" name="query" />
    <select name="BRANCH">
        <option value="">Select</option>
        <option value="Banani">Banani</option>
        <option value="RayerBazar">RayerBazar</option>
    </select>
    <input type="submit" value="Find" name="completedsearch" />
</form>

<?php
if(isset($_POST['completedsearch']))
{
    $term = $_POST['query'];
    $term1 = $_POST['BRANCH'];
    $mysql = mysql_connect("localhost","password","null");
    mysql_select_db("mydb");

    $qu = mysql_query("SELECT * FROM `stu` WHERE STUDENTID LIKE '%{$term}%' OR STUDENTID LIKE '%{$term}%' OR BRANCH LIKE '%{$term1}%' OR STUDENTID LIKE '%{$term}%' "); //selects the row that contains ANYTHING like the submitted string
    $qu1 = mysql_query("SELECT * FROM `stu` WHERE BRANCH LIKE '%{$term1}%'  ");

    if ($term  == "" || $term1  == "") {
      // no results
      echo '<a style="color:red;font-size: 30px;">Please Put Name OR ID Here</a><br><body  background="ghost.gif"   style="background-repeat:no-repeat;background-size: cover">';
    } else {
        echo "<table style='width:100%'>
                    <th style='width:10%;font-size:20px;border: 2px solid  red; '>Info ID</th>
                    <th style='width:20%;font-size:20px;border: 2px solid  red; '>School ID</th>
                    <th style='width:20%;font-size:20px;border: 2px solid  red; '>Name</th>
                    <th style='width:10%;font-size:20px;border: 2px solid  red; '>Class</th>
                    <th style='width:10%;font-size:20px;border: 2px solid  red; '>Shift</th>
                    <th style='width:10%;font-size:20px;border: 2px solid  red; '>Branch</th>
                    <th style='width:20%;font-size:20px;border: 2px solid  red; '>Search</th>
                    ";
        while($row = mysql_fetch_array($qu))
       {
            echo "<tr><td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";  
            echo $row['id'];
            echo "</p></td>";
            echo "<td style='width:20%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['STUDENTID'];
            echo "</p></td>";
            echo "<td style='width:20%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['STUDENTNAME'];
            echo "</p></td>";
            echo "<td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['CLASS'];
            echo "</p></td>";
            echo "<td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['SHIFT'];
            echo "</p></td>";
            echo "<td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['BRANCH'];
                   echo "</p></td>";
            echo " <td style='width:20%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'><a href='id.php?id=";
            echo $row['id'];
            echo "'>Visit This Profile</a></p></td>";
        }

        while($row = mysql_fetch_array($qu1))
       {
            echo "<tr><td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";  
            echo $row['id'];
            echo "</p></td>";
            echo "<td style='width:20%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['STUDENTID'];
            echo "</p></td>";
            echo "<td style='width:20%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['STUDENTNAME'];
            echo "</p></td>";
            echo "<td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['CLASS'];
            echo "</p></td>";
            echo "<td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['SHIFT'];
            echo "</p></td>";
            echo "<td style='width:10%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'>";
            echo $row['BRANCH'];
            echo "</p></td>";
            echo " <td style='width:20%;font-size:15px;border: 2px solid  blue;'><p style='text-slign: center;font-family: cursive;text-align: center'><a href='id.php?id=";
            echo $row['id'];
            echo "'>Visit This Profile</a></p></td>";
        }
    }
}
?>
    </tr>
</table>

2 个答案:

答案 0 :(得分:0)

当你问如果输入和选项之间的任何一个保持为空时,则显示错误而不是使用

if(!empty($_POST['query']) || !empty($_POST['BRANCH']) ) {
   // one of them or both select
 } else {
   die("please select one of them ")
} 

http://php.net/manual/en/function.empty.php

请更喜欢使用PDO或mysqli而不是这个msql
如果你想在第一页而不是search.php上显示错误而不是使用Ajax

如你所说,试试这个

if(empty($_POST['query']) && empty($_POST['BRANCH'])) {
     die ("Please select any one or both")
} else if (!empty($_POST['query']) && !empty($_POST['BRANCH']) {  

  // it mean user selected both than try to write your query whith And 
  // SELECT * FROM `table` WHERE `student_id` LIKE '%$id%' AND `BRANCH` LIKE `$branch` 
}  else if (!empty($_POST['query'])) {
   // user only write in  text box 
   //SELECT * FROM `table` WHERE `student_id` LIKE '%$id%'
  } else {
   //user choose select option 
   //SELECT * FROM `table` WHERE  `BRANCH` LIKE `%$branch%` 
  }

答案 1 :(得分:0)

if(isset($_POST['completedsearch'])){
   if(empty($_POST['query']) || empty($_POST['BRANCH'])) {
     echo "Show your error!"; 
   }else {
     //execute your code.
   }
}