I researched a lot and found some solution ,probably not a convincing solution for me.Hence i am posting this question,Please help me
I Have A checkbox with same name and different values like
1.cate.php
<form action="mobile-phones-category.php" method="post">
<input type="checkbox" value="samsung" name="mobile[]"> sams
<input type="checkbox" value="nokia" name="mobile[]"> nolkia
<input type="submit" name="submit" value="SUBMIT" class="btn btn-primary pull-right">
</form>
2.) mobile-phones-category.php
I retrieve the values of check box on submit[array format] and want to search from db..I am using normal mysql_query(not pdos)
$search=$_POST["mobile"];
$search_string = implode(', ', $search);
echo $search_string;
Here i Get something like Nokia,Sams
Next I write a single sql query
include('connection.php');
$query = mysql_query("SELECT * FROM tablename where titles like '%$search_string%' ") or die(mysql_error());
What is happening is that only one value in the array is searched and not all the values in array..What changes should i Make so that all the array element should get searched
Thanks and regards
Use IN keyword in your query instead of LIKE
$query = mysql_query("SELECT * FROM tablename where titles IN ($search_string)" ) or die(mysql_error());
Usage Example:
$query = mysql_query("SELECT * FROM tablename where titles IN ('Nokia','Sams')" ) or die(mysql_error());
This will give you records with title Nokia & Sams from the table.
Like User016 said, I would also recommend using the IN Statement. It searchs for several searchterms, splitted by a ,.
You can find the Doc there:
http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_in
Related
i am trying to filter multiple products from database using php. i have check box in html page with 4 category,the values will be stored in a array on submit values post to php page , where data is filtered. see below example.
bellow is my html
<input class="le-checkbox" type="checkbox" name="filter1[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
<input class="le-checkbox" type="checkbox" name="filter2[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
<input class="le-checkbox" type="checkbox" name="filter3[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
<input class="le-checkbox" type="checkbox" name="filter4[]" value="<?=$bkey?>" /> <label><?=$bkey?></label>
values are stored in filter1[],filter2[],filter3[],filter4[]
in php
$filter1=$_POST['filter1'];
$filter2=$_POST['filter2'];
$filter3=$_POST['filter3'];
and also implode
i
f(isset($filter1)){
$pbrand= implode(",",$filter1);
}
if(!empty($filter2)){
$pcolor= implode(",",$filter2);
}
if(!empty($filter3)){
$pfab= implode(",", $filter3);
}
now check with mysql
$result = $dbh->prepare("SELECT * FROM products_list WHERE status='Available' AND brand IN (?) OR color IN (?) OR fabric IN (?)");
//$result->bindValue(1, $status);
$result->bindValue(1, $pbrand);
$result->bindValue(2, $pcolor);
$result->bindValue(3, $pfab);
its working fine when i select single checkbox from each category. when i check multiple checkbox from single catagory, its not working and also my condition status='Available' not working i don't understand why its not working
$result = $dbh->prepare("SELECT * FROM products_list WHERE status='Available' AND (brand IN (?) OR color IN (?) OR fabric IN (?))"
brackets is need after AND
So I've been trying to create a search form on my website that depends on what the user check ( checkboxes), I've spent many time researching on web but didn't find anything, so I had to do things myself.
so here is the code for the html
<li><input type="checkbox" name="checkbox[]" value="action"> Action</li>
<li><input type="checkbox" name="checkbox[]" value="Adventure"> Adventure</li>
<li><input type="checkbox" name="checkbox[]" value="Animation"> Animation</li>
<li><input type="checkbox" name="checkbox[]" value="cars"> Cars</li>
<li><input type="checkbox" name="checkbox[]" value="cartoon"> Cartoon</li>
<li><input type="checkbox" name="checkbox[]" value="comedy"> Comedy</li>
and here is the php
<?php
if(!empty($_POST['checkbox'])) {
$Gen3 = implode(" AND * ", $_POST['checkbox']);
$Gen2= str_replace('*', 'Genre like ', $Gen3);
$table='animelist';
$sql = "SELECT * FROM $table WHERE Genre like '".$Gen2."' ORDER BY NAME DESC" ;
echo $sql;
$result = mysqli_query($conn, $sql) or die('Erreur SQL !'.$req.'<br>'.mysql_error());
while($row = $result->fetch_assoc()){
?>
<li><?php echo $row['Name']; ?></li>
<?php ;
}
};
?>
the echo $sql if I check two checkboxes for example result
SELECT *
FROM animelist
WHERE Genre like 'action AND Genre like Adventure'
ORDER BY NAME DESC
so the thing here is that the "movie" that the user would research might be a SET of "Action,Adventure" , so when the user checks the checkboxes the result have to be the "movies" which are both Action and adventure not only Action.
for now if I only check one checkbox evrything works fine but when I check two or more I get nothing
so I though that the ' in the sql query before and after the variable are the one ruining it, so I had to get ride of them, so I tried to use mysql_real_escape_string, but since its not accepter anymore and the mysqli_real_escape_string doesn't do the job I am stuck here.
You are searching for a genre named 'action AND Genre like Adventure' where you should be doing somehting like
SELECT *
FROM animelist
WHERE Genre like 'action' OR Genre like 'Adventure'
ORDER BY NAME DESC
Thats for your question, but you should use prepared statements to prevent SQL Injection and use = instead of like whenever possible.
Okey I solved it by using FIND_IN_SET
For a games reservation system that I have created, I have made a search bar to allow for easier navigation of the games available. However, to make the system quicker to make a reservation, I would like to create a link to a form that would automatically post the details of the game into the reservation form, but I'm not sure how.
Here is the code I used to create the search bar, if needed.
$sql= "select * from games ";
if (isset($_POST['search_box'])) {
$search= mysql_real_escape_string( $_POST['search_box']);
$sql .= "WHERE GameName LIKE '%{$search}%' ";
$sql .= " OR GameDescription LIKE '%{$search}%'";
$sql .= " OR GameID LIKE '%{$search}%'";
}
$result = mysql_query($sql) or die(mysql_error());
After the PHP code, I used HTML, to create a search form:
<form name="search_form" method="POST" action="index.php">
Search: <input type="text" name="search_box" value="">
<input type="submit" name = "search" value = "Search">
</form>
You could pass the parameters in the url. For example, build a link like so:
Link to reservation form
Then on your reservation form page, use php to check the query string for those parameters using $_GET['game'] (would return "Zelda") and $_GET['sky'] (would return "blue").
My database holds a table with the names of 30 users. I have the following html form which is a search form:
<form method="POST" name="go" action="search_form_all.php" >
<input name="value" type="text" id="search_form_1" size="65" />
<input type="submit" value="" name="submit" />
</form>
Then usign the following php script, the form as a result displays all names from my database:
if(isset($_POST['value'])== true && empty($_POST['value']) == false){
$value = mysql_real_escape_string($_POST['value']);
$name_and_surname = explode(" ", "$value ");
$name = $name_and_surname[0];
$surname = $name_and_surname[1];
$query = mysql_query(" SELECT `name`, `surname`, `email`, `user_id` FROM users WHERE (surname LIKE '$name%' AND name LIKE '$surname%') OR (surname LIKE '$surname%' AND name LIKE '$name%') LIMIT 10 ");
while($run = mysql_fetch_array($query)){
$name = $run['name'];
echo" $name ";
}
by executing the above code, I get the first 10 names (because in my sql query have limit 10). All I want is to have a button that when the user press it to extract the rest 10 names and then the rest 10 until all 30 names extracted. How can I do this?
the sql LIMIT function can handle more than just one number, for example LIMIT 10, 20 will output the 10th to 20th result.
I'm sure you can figure out something with your form and posting to use that to your advantage
More info about the SQL LIMIT here: http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
SELECT `name`, `surname`, `email`, `user_id`
FROM users
WHERE (surname LIKE '$name%' AND name LIKE '$surname%')
OR (surname LIKE '$surname%' AND name LIKE '$name%')
LIMIT $offset, 10;
where offset can be 1,11,21 in your case
You can use a classy solution called PAGINATION in web lingo..It simply gets a chunk of data and prints out the links pointing to the next page..http://www.phpfreaks.com/tutorial/basic-pagination may help
You can use that value and manipulate according to your needs..For example you can use it in in your LIMIT Statement..
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. These 3 will be entered in text boxes.
Please mention the particular query for this 3 fields search.
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