sorting data using button in php - php

I tried to create a button which the function is to sort data either descending or ascending. However, I don't have idea how to do it
I did some research in internet, but none of them give the answer.
anyone know how to do it or some source code which can be a references???
this is my code
test.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th>test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td></td>
<td>
<select name="country">
<option value="US">United States</option>
<option value="NZ">New Zealand</option>
<option value="JP">Japan</option>
</select>
</td>
</tr>
<td>
<input type="submit" name="formSubmit" value-"Submit">
</td>
</table>
</form>
</body>
</html>
showDB.php
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
if($_POST['formSubmit'] == "Submit")
{
$country = $_POST['country'];
}
//query the database
if($country == 'US') {
// query to get all US records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'");
}
elseif($country == 'NZ') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'");
}elseif($country == 'JP') {
// query to get all AUD records
$query = mysql_query("SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'");
} else {
// query to get all records
$query = mysql_query("SELECT * FROM auip_wipo_sample");
}
//fetch the result
Print "<table border cellpadding=3>";
//ascending descending button
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print "<td>".$row['invention_title'] . "</td>";
Print "<td>".$row['invention-title'] . " </td></tr>";
}
//sorting the data, I got from internet but doesn't work
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
{
$query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title ASC";
}else{
$query = "SELECT * FROM auip_wipo_sample ORDER BY invention_title DESC";
}
Print "</table>";
?>

Change this:
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value-'Asc'></input></th></tr>";
To
Print "<tr><th colspan='2'><input type='submit' name='asc_sort' value='Asc'></input></th></tr>";
And this
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']) && $_POST['asc_sort']==1)
to
if(isset($_POST['asc_sort']) && !empty($_POST['asc_sort']))

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Data Mining</title>
</head>
<body>
<form action="showDB.php" method="post">
<table border="0">
<tr>
<th colspan="3">test</th>
</tr>
<tr>
<td>Select Foreign Agent Country</td>
<td>
<select name="country">
<option value="US">United States</option>
<option value="NZ">New Zealand</option>
<option value="JP">Japan</option>
</select>
</td>
<td><input type="checkbox" name="asc" value="1" /> Ascending?</td>
</tr>
<tr>
<td colspan="3">
<input type="submit" name="formSubmit" value-"Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Few things:
You were pulling a query statement in the middle of a while where
you're still retrieving the previous statement. This would require
pulling the data into multiple arrays and then processing.
You should be using something like pdo or mysqli for this, but here's an
example with your current methods.
Also you were using isset on POST... if POST has anything in it, it will return as set.
Referencing a POST variable that is not set will cause an error, So I'm checking to make sure that country is set then I'm provided a NULL answer to the switch.
Example Code:
<?php
//connect to server
$connect = mysql_connect("localhost", "root", "");
//connect to database
//select the database
mysql_select_db("fak_databases");
//submit button
if(!empty($_POST['formSubmit'])&&($_POST['formSubmit']=="Submit")&&(!empty($_POST['country']))){
$country = $_POST['country'];
} else {
$country = '';
}
if(!empty($_POST['asc']))
{
$append = " ORDER BY invention_title ASC";
}else{
$append = " ORDER BY invention_title DESC";
}
switch($country){
case 'US':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='US'$append";
break;
case 'NZ':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='NZ'$append";
break;
case 'JP':
$query = "SELECT * FROM auip_wipo_sample WHERE applicant1_country='JP'$append";
break;
default:
//all records
$query = "SELECT * FROM auip_wipo_sample$append";
}
//query the database
if($result = mysql_query($query)){
//fetch the result
print "<table border cellpadding=3>";
while($row = mysql_fetch_array($result))
{
print "<tr>";
print "<td>".$row['invention_title'] . "</td>";
print "<td>".$row['invention-title'] . " </td></tr>";
}
//sorting the data, I got from internet but doesn't work
print "</table>";
} else {
//For Testing
echo "Query Failed:<br />$query";
}
?>

Related

Add Edit Delete form not adding into MySQL no errors

I have this Add Edit Delete form, the problem is:
when I put everything and I click on ADD it says "Data added successfully." but the data isn't in my table of phpAdmin and it not shows in the page...
Or is simply because my hoster doens't work with MySQLi but with MySQL?
Without talking about SQL Injections because Im not so expert and dont know how protect from that, this pages will be protected with login area so only restricted members will access to it.
index.php
<?php
//including the database connection file
include_once("config.php");
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM `user` ORDER BY id DESC"); // using mysqli_query instead
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
Add New Data<br/><br/>
<table width='80%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Steam Username</td>
<td>Steam Password</td>
<td>Steam Guard Code</td>
<td>Update</td>
</tr>
<?php
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['steamUE']."</td>";
echo "<td>".$res['steamPW']."</td>";
echo "<td>".$res['steamGC']."</td>";
echo "<td>Edit | Delete</td>";
}
?>
</table>
</body>
</html>
add.html
<html>
<head>
<title>Add Data</title>
</head>
<body>
Home
<br/><br/>
<form action="add.php" method="post" name="form1">
<table width="25%" border="0">
<tr>
<td>Steam Username</td>
<td><input type="text" name="steamUE"></td>
</tr>
<tr>
<td>Steam Password</td>
<td><input type="text" name="steamPW"></td>
</tr>
<tr>
<td>Steam Guard Code</td>
<td><input type="text" name="steamGC"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</table>
</form>
</body>
</html>
edit.php
<?php
// including the database connection file
include_once("config.php");
if(isset($_POST['update']))
{
$id = mysqli_real_escape_string($mysqli, $_POST['id']);
$steamUE = mysqli_real_escape_string($mysqli, $_POST['steamUE']);
$steamPW = mysqli_real_escape_string($mysqli, $_POST['steamPW']);
$steamGC = mysqli_real_escape_string($mysqli, $_POST['steamGC']);
// checking empty fields
if(empty($steamUE) || empty($steamPW) || empty($steamGC)) {
if(empty($steamUE)) {
echo "<font color='red'>Steam Username field is empty.</font><br/>";
}
if(empty($steamPW)) {
echo "<font color='red'>Steam Password field is empty.</font><br/>";
}
if(empty($steamGC)) {
echo "<font color='red'>Steam Guard Code field is empty.</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($mysqli, "UPDATE `user` SET steamUE='$steamUE',steamPW='$steamPW',steamGC='$steamGC' WHERE id='$id'");
//redirectig to the display page. In our case, it is index.php
header("Location: index.php");
}
}
?>
<?php
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM `user` WHERE id='$id'");
while($res = mysqli_fetch_array($result))
{
$steamUE = $res['steamUE'];
$steamPW = $res['steamPW'];
$steamGC = $res['steamGC'];
}
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
Home
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Steam Username</td>
<td><input type="text" name="steamUE" value="<?php echo $steamUE;?>"></td>
</tr>
<tr>
<td>Steam Username</td>
<td><input type="text" name="steamPW" value="<?php echo $steamPW;?>"></td>
</tr>
<tr>
<td>Steam Guard Code</td>
<td><input type="text" name="steamGC" value="<?php echo $steamGC;?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
delete.php
<?php
//including the database connection file
include("config.php");
//getting id of the data from url
$id = $_GET['id'];
//deleting the row from table
$result = mysqli_query($mysqli, "DELETE * FROM `user` WHERE id='$id'");
//redirecting to the display page (index.php in our case)
header("Location: index.php");
?>
add.php
<html>
<head>
<title>Add Data</title>
</head>
<body>
<?php
//including the database connection file
include_once("config.php");
if(isset($_POST['Submit'])) {
$steamUE = mysqli_real_escape_string($mysqli, $_POST['steamUE']);
$steamPW = mysqli_real_escape_string($mysqli, $_POST['steamPW']);
$steamGC = mysqli_real_escape_string($mysqli, $_POST['steamGC']);
// checking empty fields
if(empty($steamUE) || empty($steamPW) || empty($steamGC)) {
if(empty($steamUE)) {
echo "<font color='red'>Steam Username field is empty.</font><br/>";
}
if(empty($steamPW)) {
echo "<font color='red'>Steam Password field is empty.</font><br/>";
}
if(empty($steamGC)) {
echo "<font color='red'>Steam Guard Code field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO `user` (steamUE,steamPW,steamGC) VALUES ('$steamUE','$steamPW','$steamGC')");
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
?>
</body>
</html>
config.php
<?php
/*
// mysql_connect("database-host", "username", "password")
$conn = mysql_connect("localhost","root","root")
or die("cannot connected");
// mysql_select_db("database-name", "connection-link-identifier")
#mysql_select_db("test",$conn);
*/
/**
* mysql_connect is deprecated
* using mysqli_connect instead
*/
$databaseHost = 'sql.website.com';
$databaseName = '';
$databaseUsername = '';
$databasePassword = '';
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
?>
It not doesn't says or shows any errors or any other problems, it says only data added successfully and nothing else. I don't understand why it doesn't add any data in my tables, i checked everything again and again, maybe because i'm tired but i tried to rename tables names but nothing change, is the same...
Spotted three errors,
add.php: Column names should be without ''. Check the following
$result = mysqli_query($mysqli, "INSERT INTO user (steamUE,steamPW,steam_GC) VALUES ('$steamUE','$steamPW','$steamGC')");
edit.php: '' missing from $id. Check the following
$result = mysqli_query($mysqli, "UPDATE user SET steamUE='$steamUE',steamPW='$steamPW',steamGC='$steamGC' WHERE id='$id'");
delete.php: '' missing from $id. Check the following
$result = mysqli_query($mysqli, "DELETE * FROM user WHERE id='$id'");
If the connection with DB is successful, it must work (and this answer deserves a green tick from you :D).
Or is simply because my hoster doens't work with MySQLi but with
MySQL?
Wherever I faced issues, I got some error or a blank page.
Check your dB connection. Turn to mysqli, declair it with $sql with (errno), but call your param before $sql. Use if condition to check your connection. On your add please use prepared with $stmnt and execute it.

HTML table won't update after entering values using PHP POST method

I have a database with course IDs and their corresponding course names. I'm trying to create a filter using a dropdown menu that filters the displayed courses.
Here's the code -
<body>
<div id="wrapper">
<?php // sqltest.php
include 'header.php';
include 'nav.php';
require_once 'login.php';
$conn = mysqli_connect($hn, $un, $pw, $db);
if (!$conn) {
die ('Fail to connect to MySQL: ' . mysqli_connect_error());
}
echo '<form action="" method="post">
<select name="Course Name">
<option value="All" selected="selected">All course names</option>
<option value="Introduction to Data Science">Introduction to Data Science</option>
<option value="Database Management Systems">Database Management Systems</option>
<option value="Data Visualization">Data Visualization</option>
</select>
<input type="submit" value="Search" name="submit" />
</form>';
$cname = isset($_POST['Course Name']) ? $_POST['Course Name'] : 'All';
if(isset($_POST)) {
if ($cname == 'Introduction to Data Science') {
$query = "SELECT * FROM courses WHERE Cname='Introduction to Data'";
} elseif ($cname == 'Database Management Systems') {
$query = "SELECT * FROM courses WHERE Cname='Database Management'";
} elseif ($cname == 'Data Visualization') {
$query = "SELECT * FROM courses WHERE Cname='Data Visualization'";
} elseif ($cname == 'All') {
$query = "SELECT * FROM courses";
}
$result = mysqli_query($conn, $query);
if (!$result) {
echo 'Could not get data: ' . mysqli_error($conn);
}
echo '<br>Available courses for hardcoded student 000-01-0002<br><br>';
echo '<table>
<thead>
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Add</th>
</tr>
</thead>
<tbody>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>
<td>' . $row['cid'] . '</td>
<td>' . $row['Cname'] . '</td>
<td>Add</td>
</tr>';
}
}
echo '
</tbody>
</table>
</body>';
It displays all the courses when the page first loads but picking any one of the courses and clicking on submit does not change the initial table at all.
Remove the space in Course Name post field. It's not recognized as a valid POST field in your isset check because of the space.
Make it name="course_name" or something on select field.

get multiple ID while POST multiple selection option value to next form

how can I get multiple ID while POST multiple selection option value to next form? I only get the first selection ID from array. Can you guys can suggest any ideas to me?
here is my code when select the value.
<tr>
<label>Auditor: </label>
<select class="form-control" name="auditor[]" multiple="multiple" >
<?php
$result = $db->query("SELECT * FROM auditor");
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row["auditor_name"].'">'.$row["auditor_name"].'</option>';
}
echo "</select>";
?>
</tr>
here is another code while POST to the next page.
$myselected = $_POST["auditor"];
if(count($myselected)>1){
$auditor = implode ("','",$myselected);
}else{
$auditor =$myselected;
}
$query10 = "SELECT * FROM auditor WHERE auditor_name IN ('$auditor') ";
$result10 = $db->query($query10);
$row10 = $result10->fetch_array();
?>
<form action="audit_action/audit_action.php" role="form" method="post" name="auditformdetails" onsubmit="return(validate());">
<table width='100%' border='0' class="table">
<tr>
<td colspan=6>Audit details</td>
<td colspan=6>Outlet details</td>
</tr>
<tr>
<td><b>Auditor:</b></td>
<td colspan='5'>
**<?php
echo'<input type="hidden" name="auditor_id" value="'.$row10["id"].'">';
foreach ($myselected as $auditor){
echo $auditor."<br>\n";
}
?>**
</td>
You can not compare string with mysql IN Clause. So, you have to connect each of your value with or condition in query as i written below.
$myselected = $_POST["auditor"];
$sql_cond = "";
if(count($myselected)>1){
foreach($myselected as $selected){
if($sql_cond != "")
$sql_cond.=" or auditor_name = ".$selected;
else
$sql_cond.=" auditor_name = ".$selected;
}
}else{
$auditor =$myselected;
}
$query10 = "SELECT * FROM auditor WHERE ".$sql_cond;

Unable to get search results to filter

I'm trying to filter the search results of my MySQL database, I am wanting to filter the results by selecting values from two drop down boxes, the first box being Club and the second being Division so I can filter the results by Division and Club.
My issue is that when I make a selection in the club field nothing happens, I can select from the Division field and the results will display the selected division but nothing happens when I select by club.
<?php
include("config.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Club Search</title>
<style>
.style1 {
text-align: center;
}
</style>
</head>
<body>
<fieldset style="width: 327px"><legend>Filter Results</legend>
<form id="filter" name="filter" method="post" action="search.php" style="width: 316px">
<label for="club">Club</label>
<select name="club">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY club ORDER BY club";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["club"]."'".($row["club"]==$_REQUEST["club"] ? " selected" : "").">".$row["club"]."</option>";
}
?>
</select>
<label>Division</label>
<select name="division">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY division ORDER BY division";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["division"]."'".($row["division"]==$_REQUEST["division"] ? " selected" : "").">".$row["division"]."</option>";
}
?>
</select>
<input type="submit" name="button" id="button" value="Filter" />
<a href="search.php">
reset</a>
</form></fieldset>
<br /><br />
<fieldset style="width: 720px"><legend>Search Results</legend>
<br/>
<table width="700" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td bgcolor="#CCCCCC" class="style1"><strong>Division</strong></td>
<td bgcolor="#CCCCCC" class="style1"><strong>Club</strong></td>
<td bgcolor="#CCCCCC" class="style1"><strong>Last Name</strong></td>
<td bgcolor="#CCCCCC" class="style1"><strong>First Name</strong></td>
<td bgcolor="#CCCCCC" class="style1"><strong>ID</strong></td>
</tr>
<?php
if ($_REQUEST["club"]='') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";
}
if ($_REQUEST["division"]<>'') {
$search_division = " AND division='".mysql_real_escape_string($_REQUEST["division"])."'";
}
if ($_REQUEST["club"]='' and $_REQUEST["division"]='') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE club = '".mysql_real_escape_string($_REQUEST["club"])."' AND division = '".mysql_real_escape_string($_REQUEST["division"])."'".$search_string.$search_division;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td class="style1"><?php echo $row['division']?></td>
<td class="style1"><?php echo $row['club']?></td>
<td class="style1"><?php echo $row['last_name']?></td>
<td class="style1"><?php echo $row['first_name']?></td>
<td class="style1"><?php echo $row['id']?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td></tr>
<?php
}
?>
</table>
<br/>
</fieldset>
</body>
</html>
UPDATE:
I changed the following code from this:
if ($_REQUEST["club"]='') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";
}
to this:
if ($_REQUEST["club"]<>'') {
$search_club = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%')";
}
and now it will allow me to filter by club and division but I still can't do just by club alone!
Change this:
if ($_REQUEST["club"]='') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";
and this:
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;
To this:
if ($_REQUEST["club"]<>'') {
$search_club = " AND club='".mysql_real_escape_string($_REQUEST["club"])."'";
}
And this:
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_club.$search_division;
And it will work!
You are using = operator in your if statements, you want to use == comparison operator like follows:
if ($_REQUEST["club"] == '') {
$search_string = " AND (club LIKE '%".mysql_real_escape_string($_REQUEST["club"])."%' OR (division LIKE '%".mysql_real_escape_string($_REQUEST["division"])."%')";
}
if ($_REQUEST["division"] <> '') {
$search_division = " AND division='".mysql_real_escape_string($_REQUEST["division"])."'";
}
if ($_REQUEST["club"] == '' and $_REQUEST["division"] == '') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE club = '".mysql_real_escape_string($_REQUEST["club"])."' AND division = '".mysql_real_escape_string($_REQUEST["division"])."'".$search_string.$search_division;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_division;
}

search with multiple category and criteria

My table has 4 columns. "isbn, author, title, price".
I want to search one of them combining all 4 fields.
Like: if author "kayle" write 4 books with same price ($50) but with different title, for that in search page, if i select author:kayle and hit search, then it shows all books with all different or may be same prices and titles. Now i want to select author:kayle and price:$50 at the same time, and hit enter. For that it will show only $50 prices books wrote by kayle and it will appear in the table with 4 rows.
I try to combine it but stuck with the second step searching query. Here is my code if any one understand what i want to do, please share it. Here is my code:
<form method="post" action="search_form.php">
<input type="hidden" name="submitted" value="true"/>
<table border="1">
<tr>
<td style="padding:3px 10px; font-weight:bold;">ISBN</td>
<td style="padding:3px;">
<input type="hidden" name="category_isbn" id="category_isbn" value="isbn"/>
: <select name='criteria_isbn' id="criteria_isbn" onchange="ajaxFunction()">
<option selected="selected">- Select -</option>
<?php
$order = "SELECT isbn FROM books" or die (mysql_error());
$result = mysql_query($order);
while($data = mysql_fetch_array($result))
{
echo ("<option> $data[isbn] </option>");
}
?>
</select>
</td>
<td style="padding:3px 10px; font-weight:bold;">Author</td>
<td style="padding:3px;">
<input type="hidden" name="category_author" id="category_author" value="author"/>
: <select name='criteria_author' id="criteria_author" onchange="ajaxFunction()">
<option selected="selected">- Select -</option>
<?php
$order = "SELECT author FROM books" or die (mysql_error());
$result = mysql_query($order);
while($data = mysql_fetch_array($result))
{
echo ("<option> $data[author] </option>");
}
?>
</select>
</td>
<td style="padding:3px 10px; font-weight:bold;">Title</td>
<td style="padding:3px;">
<input type="hidden" name="category_title" id="category_title" value="title"/>
: <select name='criteria_title' id="criteria_title" onchange="ajaxFunction()">
<option selected="selected">- Select -</option>
<?php
$order = "SELECT title FROM books" or die (mysql_error());
$result = mysql_query($order);
while($data = mysql_fetch_array($result))
{
echo ("<option> $data[title] </option>");
}
?>
</select>
</td>
<td><input type="submit" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submitted']))
{
$category_isbn = $_POST['category_isbn'];
$criteria_isbn = $_POST['criteria_isbn'];
$query = "SELECT * FROM books WHERE $category_isbn LIKE '%".$criteria_isbn."%'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if(isset($_POST['criteria_isbn']))
{
$category_author = $_POST['category_author'];
$criteria_author = $_POST['criteria_author'];
$query = "SELECT * FROM books WHERE $category_author LIKE '%".$criteria_author."%'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if(isset($_POST['criteria_author']))
{
$category_title = $_POST['category_title'];
$criteria_title = $_POST['criteria_title'];
$query = "SELECT * FROM books WHERE $category_title LIKE '%".$criteria_title."%'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows results found";
echo "<table border= 1>";
echo "<tr> <th>ISBN</th> <th>AUTHOR</th> <th>TITLE</th> <th>PRICE</th> </tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['isbn'];
echo "</td><td>";
echo $row['author'];
echo "</td><td>";
echo $row['title'];
echo "</td><td>";
echo $row['price'];
echo "</td></tr>";
}
echo "</table>";
}
}
}
?>
Thanks in advance.
try this
for first search
select* from table where author="kayle";
for second search
select * from table where author="kayle" and price="50";

Categories