hello everybody, pls help me. I've got four text boxes for searching result from mysql database using php select query. User may leave any one or all text boxes empty or enter values in all text boxes. Initially all text boxes are empty. How to write the select query. Thnx in advance
try this: $table = "TABLE"; // enter your database table here $field1 = "FIELD1"; // enter the name of criteria field 1 in your table here $field2 = "FIELD2"; // enter the name of criteria field 2 in your table here $field3 = "FIELD3"; // enter the name of criteria field 3 in your table here $field4 = "FIELD4"; // enter the name of criteria field 4 in your table here $sql = "SELECT * FROM " . $table; if($_POST[['textbox1'] != "" || $_POST[['textbox2'] != "" || $_POST[['textbox3'] != "" || $_POST[['textbox4'] != "") { $sql = $sql . " WHERE " } if($_POST[['textbox1'] != "") { $sql = $sql . $field1 . " = '" . $_POST[['textbox1'] . "' AND "; } if($_POST[['textbox2'] != "") { $sql = $sql . $field2 . " = '" . $_POST[['textbox2'] . "' AND "; } if($_POST[['textbox3'] != "") { $sql = $sql . $field3 . " = '" . $_POST[['textbox3'] . "' AND "; } if($_POST[['textbox4'] != "") { $sql = $sql . $field4 . " = '" . $_POST[['textbox4'] . "' AND" ; } $sql = substr($sql, 0, strlen($sql) - 5); Code (markup):