How to auto-fill a form after doing a search - php

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").

Related

Dynamically Collecting Data from 2 tables to add to another

So I have the following code to collect information for two sets of information, customers and products :
if(isset($_POST['q'])) {
$q = $_POST['q'];
$select = $db->query("SELECT * FROM products WHERE Model LIKE '%$q%' OR Description LIKE '%$q%' ORDER BY id DESC");
} else {
$select = $db->query("SELECT * FROM products ORDER BY id DESC");
}
<form method="post">
<input type="text" name="q" /> <input type="submit" name="search" />
</form>
The code is duplicated across both products and customers. I would like to create another page that will let me type in a form field that auto-populates with each database table (customers/products) and, after information is selected, allow the update of a third table (invoices).
In the same page, I would like to allow the addition of as many "products" as the user would like, via an icon "+" sign next to each row of products.
What would be the best way to approach this?

PHP PDO search Issue using multiple input fields with LIKE statment giving all results even if 1 param given

I'll try to show my search form and continue on bits of public method code.
Here's my form (basic):
<form action="" method="get">
Name:<input type="text" name="name">
Last Name:<input type="text" name="last_name">
Age:<input type="text" name="age">
<input type="submit" value="search" name="search">
</form>
Moving on to my get method:
public function get($table){
$ssql = 'SELECT * FROM '.$table.' WHERE `name` LIKE :name || `last_name` LIKE :last_name';
$name = '%'.$_GET['name'].'%';
$last_name = '%'.$_GET['last_name'].'%';
$stm = $this->dbh->prepare($ssql);
$stm->bindParam(':name',$name,PDO::PARAM_STR);
$stm->bindParam(':last_name',$last_name,PDO::PARAM_STR);
$stm->execute();
echo $stm->rowCount();
while($result = $stm->fetch(PDO::FETCH_OBJ)){
echo $result->name.' '.$result->last_name.'<br>';
}
}
Basically this should give me results based on my $_GET form fields. My problem is that if I enter name "juris" it does not outputs only 3 rows of juris which are in database but all fields from database. But if I enter lastname and then submit it, it gives out only juris records but then ignores lastname search term.
Shouldn't query like "WHERE etc LIKE "term1" OR etc LIKE "term2" give out results based on OR between 2 given variables? It should separate results if one variable $_get['value'] is given and others are empty?
Got it to work as intended but maybe some one could give a bit cleaner answer how to separate "%%" this wild card?
$ssql = 'SELECT * FROM '.$table.' WHERE (`name` LIKE :name) || (`last_name` LIKE :last_name)';
if(empty($_GET['name'])){
$name = "";
}else {
$name = '%'.$_GET['name'].'%';
}
if(empty($_GET['last_name'])){
$last_name = "";
}else{
$last_name = '%'.$_GET['last_name'].'%';
}
used if statement to separate %% wildcard.
You need to use parenthesis to group the two field comparisons like this:
SELECT * FROM '.$table.' WHERE (`name` LIKE :name && :name_prov = 1) || (`last_name` LIKE :last_name && :last_name_prov = 1)
So yesterday i tryed to figure out how could i separate "%%" wildcard from SQL query.
My answer for this problem is to check if fields are empty, then change $variable to empty string. If not, use it with $_GET method and wrapping it with "%%" wildcard. To get desired result using query with LIKE statement.

write sql from multiple checkbox

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

fetch data from mysql table by pressing button at doses

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..

PHP/MySQL Like Button

I've made a 'like' button for my product pages with this code:
<?php
if('POST' == $_SERVER['REQUEST_METHOD']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like"/>
</form>
Works like a charm excpet for one minor problem being that every visit to the page registers a 'like'.
Could someone help explain what i need to chnage/add in order that new 'likes' are only registered when the actual form is submitted?
Thanks
Dan
A better solution rather than submitting the page and the whole page reloading would be to make an AJAX request, this is how Facebook 'likes' work.
This can be achieved using the jQuery JavaScript library.
The general outline would be:-
1) Click button
2) Send AJAX request
3) Update HTML to show button has been clicked and prevent reclicking of button.
<?php
if($_POST['like']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like" name='like'/>
</form>
This should work ;-)
<?php
if ($_POST['like']){
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" name="like" value = "like"/>
</form>
First of all - in your sql you have:
`product_id` = '1'
do not use id value as a string:
`product_id` = 1
About your problem:
Add another condition:
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if ( !empty($_POST['submitType']) && ( $_POST['submitType'] == 'like' ) ) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
}
and in html:
<input type = "submit" name="submitType" value = "like"/>
Sounds like some kind of old question, but I wonder why noone has said, that op's approach doesn't sound quite right. You try to just count likes (set likes=likes+1). It has many disadvantages:
You miss information, who gave the like. Thus you won't be able to reconstruct the whole picture
Users won't be able to "undo" likes (as you don't record who liked the post)
In case of many concurrent likes I feel like you'd get some kind of data race or a long delays, because MySQL would need to process every request on a single field in order.
Much better idea is to create separate table in the DB named "product_likes" with columns like product_id, user_id, date. Of course, product id and user id should be unique together.
Thus you'll always know the full picture and will be able to see who liked the product. Even if accidentally you'll issue the second like from the same user about the same product, it won't be stored due to db constraints.
Also it will be possible to extend it to i.e. emotions-reactions, just by adding new column like "like_type" and updating the constraint correspondingly.

Categories