Multiple textbox search using PHP and MySQL
I have a table in MySQL with 5 data fields: id
, name
, total marks
, percentage
, and rank
. I already fetch and displayed all data, but I want search and display by 3 fields name: total marks
, and rank
.开发者_Go百科 These 3 will be entered in text boxes.
As you've had to ask this question, I'd like to first of all point you towards the MySQL manual and the PHP manual. However, I'll also give you some pointers.
First of all, you'll need to post these search values to your PHP code. This can be done using a form.
<form method="POST" action="script.php">
<input name="name" type="text" />
<input name="total_marks" type="text" />
<input name="rank" type="text" />
<input type="submit" value="Search" />
</form>
Then, you'll need to access these values in your PHP script as such.
// script.php
$name = mysql_real_escape_string($_POST['name']);
$total_marks = mysql_real_escape_string($_POST['total_marks']);
$rank = mysql_real_escape_string($_POST['rank']);
// I'll leave SQL injection protection up to you
Finally, you'll need to pass these queries to an SQL query to retrieve the items from your database. As you haven't posted your exact scheme, you'll have to modify this to suit your needs. Also, I've left the actual database loading/access to you.
// script.php
$sql = "SELECT * FROM `table` WHERE (
`name` = '{$name}' AND
`total_marks` = '{$total_marks}' AND
`rank` = '{$rank}'
)";
Rather than passing the variables directly to the SQL query and using mysql_real_escape_string
or similar functions, I'd look in to using PDO for security and for some database abstraction.
If I understand you correctly
Select *
FROM Keys
WHERE name = 'stringName' AND total_marks = numberTotalMarks AND rank = procent
精彩评论