In SQL, I have four <inputtype = & quot; text & quot; name = & quot;/& gt; is there any way to reasonably let the SQL statement on the php page judge whether the input value action is sent to the php page when the field is in SQL?
I have four Such input value
Action to php page for processing
Is there any way to reasonably let the SQL statement on the php page determine that when the field is blank, it will not be used as the search condition?
What other methods can be used in addition to the effort?
$ SQL = "select * from table where ..."
Instance:
Html
Chuli. php
// The database connection part is omitted.
$ SQL = "select * from student where name = '$ name' and age =' $ age' and sex = '$ sex' and height =' $ height '; // This is all
$ Result = mysql_query ($ SQL, $ con );
// Is there any way to judge the $ SQL statement when the Input data is null?
// If the input is 4 + 6 + 4 + 1 + 1 = 16, right? C41 + c42 + c43 + c44 + 1 = 4 + 6 + 4 + 1 + 1 = 16?
------ Solution --------------------
PHP code
Foreach (array_diff ($ _ POST, array ('') as $ k => $ v) $ r [] =" $ k = '$ V '"; $ SQL = "select * from student where ". join ('and', $ r );
------ Solution --------------------
It should be, the above should be less''
PHP code
$sql="select * from student where 1=1";if(!empty($_POST['name'])) $sql=" and name='$_POST[name]'";if(!empty($_POST['age'])) $sql=" and age='$_POST[age]'";if(!empty($_POST['sex'])) $sql=" and sex='$_POST[sex]'";if(!empty($_POST['height'])) $sql=" and height='$_POST[height]'";