PHP - How Write Php For A Search Form?
How would i begin to start php coding to work for this form shown in the image? could anyone point me to useful tutorials, i would appreciate it, thanx
Similar TutorialsSo let's assume i'm making a script for searching cars by different criteria. Name of the car, color, and mileage. I used to do this by using LIKE and just adding AND for as many times as needed. But i've read that that is obsolete and is giving servers a hard time, so i went ahead and started making a new script that will use fulltext search. I've hit a dead end. Code: [Select] function search($table,$what,$string,$limit,$start,$country) { global $totrows; global $pages; $string = explode("*",$string); $what = explode("*",$what); $cname = trim($string[0]); $t = 0; $parameters = ""; while (isset($what[$t])){ if ($t>0) { $paramm .= ", "; } $paramm .= trim($what[$t]); if ($t>0) { $parama .= " "; } $parama .= trim($string[$t]); $t++; } $parameters .= " AND locationc = '".$country."'"; $sql = "SELECT *, MATCH(".$paramm.") AGAINST('".$parama."') AS score FROM main WHERE MATCH(".$paramm.") AGAINST('".$parama."') ORDER BY score DESC"; $result = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_array($result)) { var_dump($row); } } How does this work: The user selects and enters his parameters> The script then joins them all into a single variable which is then sent to the function(the reason i do this i because the user might input only the name, so that way i can add more criteria and not need to change the script)> The script explodes those into arrays and then it processes them into a form for use with sql> I run the sql query and then return the results. What's the problem? There's two of them actually> The first one is that mysql returns an error "Can't find FULLTEXT index matching the column list" even though i did setup fulltext indexes. (phpmyadmin c/p) Code: [Select] name FULLTEXT No No name 0 YES color FULLTEXT No No color 0 YES mileage FULLTEXT No No mileage 0 YES The second problem is that the script is not selective and will not work as intended. For example, a car's name is 300, the users input's 300, and the script will return those rows that had mileage 300 or 300000 or whatever. How do i fix this, and is FULLTEXT the right way to go with multi criteria search? There are two pieces to this- The HTML Form and the resulting php. I can't seem to make the leap, from the code to having the form produce the php page so others can view it until the form is again submitted overwriting the php, thus generating new content. The environment I am working in is limited to IIs 5.1 and php 5.2.17 without mySQL or other DB I'm new to php, this isn't homework,or commercialization, it's for children. I am thinking perhaps fwrite / fread but can't get my head around it. Code snipets below. Any help, please use portions of this code in hopes I can understand it Thanks Code snipet from Output.php Code: [Select] <?php $t1image = $_POST["t1image"]; $t1title = $_POST["t1title"]; $t1info = $_POST["t1info"]; $t2image = $_POST["t2image"]; $t2title = $_POST["t2title"]; $t2info = $_POST["t2info"]; ?> ... <tbody> <tr><!--Headers--> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Animal</td> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Image thumb<br> </td> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Date<br> </td> <td style="vertical-align: top; text-align: center; background-color: rgb(204, 255, 255);">Information<br> </td> </tr> <tr> <td style="vertical-align: top; text-align: center;">Monkey </td> <td style="vertical-align: top; text-align: center;"><img src="<?php echo $t1image.'.gif'; ?>"><!--single image presented selected from radio buttons--> </td> <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?><!--time stamp generated when submitted form populates all fields at once--> </td> <td style="vertical-align: top; text-align: center;"><a href="#monkey" rel="facebox"><?php echo $t1title ?></a><!--Link name provided by "Title 1", that links to hidden Div generated page with content from "Info1" field--> <div id="Monkey" style="display:none"> <?php echo $t1info; ?> </div> </td> </tr> <tr> <td style="vertical-align: top; text-align: center;">Cat<br> </td> <td style="vertical-align: top; text-align: center;"><img src="<?php echo $t2image.'.gif'?>"></td> <td style="vertical-align: top; text-align: center;"><?php echo date("m/d/Yh:i A"); ?></td> <td style="vertical-align: top; text-align: center;"><a href="#Cat" rel="facebox"><?php echo $t2title ?></a> <div id="Cat" style="display:none"> <?php echo $t2info; ?> </div> </td> </tr> <tr> This replicates several times down the page around 15-20 times ( t1### - t20###) Code Snipet from HTML Form Code: [Select] <form action="animals.php" method="post"> <div style="text-align: left;"><big style="font-family: Garamond; font-weight: bold; color: rgb(51, 51, 255);"><big><big><span>Monkey</span></big></big></big><br> <table style="text-align: left; width: 110px;" border="0" cellpadding="2" cellspacing="0"> <tbody><tr> <td style="vertical-align: top;">Image thumb<br> <input type="radio" name="t1image" value="No opinion" checked><img src="eh.gif" alt="Eh"> <input type="radio" name="t1image" value="Ok"><img src="ok.gif" alt="ok"> <input type="radio" name="t1image" value="Like"><img src="like.gif" alt="Like"> <input type="radio" name="t1image" value="Dont"><img src="dont.gif" alt="Don't Like"> <input type="radio" name="t1image" value="Hate"><img src="hate.gif" alt="Hate"> <input type="radio" name="t1image" value="Other"><img src="other.gif" alt="Other"> <br> Why Title:<input type="text" name="t1title" size="45" value="..."/></td> <td style="vertical-align: top;"> Explain:<br> <textarea name="t1info" cols=45 rows=3 value="..."></textarea> </td></tr></table> <br> <!--Next--> How do I get the Form data to save to the php page for others to view? Need help ASAP. i need the following form to right to mysql table "table1" and columns "name" "email" and "comment".
<form action="sign.php" method="post" class="pure-form pure-form-stacked"> <fieldset> <label for="name">Your Name</label> <input id="name" type="text" placeholder="Your Name" name="name"> <label for="email">Your Email</label> <input id="email" type="email" placeholder="Your Email" name="email"> <label for="comment">Your Comments</label> <input id="comment" type="text" placeholder="Your Comments" name="comment"> <a href="thanks.html" button type="submit" class="pure-button">SUBMIT</button> </a> </fieldset> </form> My PHP File looks like this: <html> <body> <?php $myUser = "dbuser"; $myPassd = "********"; $myDB = "dbname"; $myserver = "192.168.1.80:3306"; $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; // Create connection $dbhandle = mysql_connect($myserver,$myUser,$myPassd) or die("Unable to connect to MySQL"); echo "Connected to MySQL<br>"; //select a database to work with $selected = mysql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB"); //if (mysqli_connect_errno()) { //echo "Failed to connect to MySQL: " . mysqli_connect_error(); //} //echo "connected"; $sql = "INSERT INTO signatures (name, comment) VALUES ( $name, $comment)"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo "record added"; mysql_close($dbhandle); ?> </body> </html> I also need this to redirect to another html page after it run. Like i said any help would be great because i cant get it to work! Hi all, I'm using a gateway for CC, when they payment is made and then users are redirected back to my site, I grab the order number and echo it as $info so I can display on the user's receipt. Code: [Select] <?php $info=$_POST['OrderNo']; echo "$info"; ?> Is it possible to use PHP to either to automatically (onload) submit a form or somehow write $info to a text file so I can reconcile the books later? I can do the former successfully with JavaScript... but I'd rather make it server-side. Thanks. I have an existing GET form that I use and want to write to a MYSQL table everytime a user uses that form. Code: [Select] <FORM ACTION="https://someurl.com" METHOD=GET> Would the easiest to be a javascript function that uses the onclick event? What would be the best way to continue to pass the user along while capturing data and writing that to a MYSQL table. Thanks! I am pretty new to php and trying to teach myself. I can't get the values from this form to write to my flat file called orders.txt: browse_index.php <?php include("includes/menu_members.php") ?> <div id="content"> <h1>SHOPPING CART</h1> <a href="browse_index.php">CLICK HERE TO CONTINUE SHOPPING</a> <?php echo ' <table border="0"> <tr> <td><form id="f2" method="post"name="f2"><input type="submit" action="order_summary.php" name="submit2" value="submit order"></td> '; if(isset($_POST['submit'])) { $itemname = $_POST['h1']; //echo $_SESSION['itemname'][$itemname]; unset($_SESSION['itemqty'][$itemname]); unset($_SESSION['itemprice'][$itemname]); unset($_SESSION['itemname'][$itemname]); } echo "<br/><br/>"; echo "<table border='8' bgcolor='#efefef'>"; echo "<tr><th>Name</th><th>Quantity</th><th>Price</th><th>Subtotal</th></tr>"; foreach($_SESSION['itemname'] as $key=>$value) { echo '<tr><td><b>'.$_SESSION['itemname'][$key].'</b></td> <td>'.$_SESSION['itemqty'][$key].'</td> <td>$'.$_SESSION['itemprice'][$key].'</td> <td name="subtotal">$'.($_SESSION['itemqty'][$key] * $_SESSION['itemprice'][$key]).'</td> <td><form id="f1" method="post" name="f1"><input type="submit" name="submit" value = "delete"><input type="hidden" name="h1" value='.$key.'></td></tr>' ; } ?> order_summary.php: <?php session_start (); $date = date ("H:i jS F"); $outputstring = $date."/t" .$_POST['h1']. ":" .$_SESSION['itemqty'][$key]. ":" .$_SESSION['subtotal'][$key]. ":" ."\n"; $fp = fopen("orders.txt","a"); fwrite($fp, $outputstring); fclose($fp); ?> Can someone direct me where I am going wrong??? I'm not sure why, but once I added a search form in my nav menu, it made my other forms on the website such as login and signup form take them to where the search button would take them. any ideas??? I want to know how to display results from mysql database by filling in a form. but i have found a tutorial which shows typing in a text box which displays results. if i follow this tutorial will it help me to understand and create php coding to display results for my form? I Have been working on this search for for a while which is working but lacks a few feature i wanna add. 1st is if the make/model/price is empty i want to search the whole db I have attached the php file Also each card has a image and need help adding these to the search. the images are located in a uploads folder Iv grafted hard on this and need some help Also is this the correct way to process a form search? hi im new to these stuff and appreciate any help.
im creating a simple form with 1 textbox and a search button. when the button is pressed a select query should run and check the mysql database and list that result on that same page. For example, lets say there is a textbox named Lastname, then the search button should search mysql and list down the last name matching that name.
Form code :
<!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> <title>Search</title> </head> <body> <form action="post.php" method="GET"> <input type="text" name="query" /> <input type="submit" value="Search" /> </form> </body> </html>Any help? I need to show the result right bellow the button, and if no match found it should display an error : no match found thanks Hello!To search in a table from my database i use this: mysql_connect ("localhost", "","") or die (mysql_error()); mysql_select_db (""); $key = $_POST['key']; $sql = mysql_query("select * from internet_securitate where nume_produs like '%$key%'"); while ($row = mysql_fetch_array($sql)){ echo 'Produs ID: '.$row['internet_securitateID']; echo '<br/> Denumire Produs: '.$row['nume_produs']; echo '<br/> Descrie '.$row['descriere']; echo '<br/> Disponibilitate: '.$row['disponibilitate_produs']; echo '<br/><br/>'; } can i expand this to searh in 3 tables? :-? I have a search form and have set up php, but at the moment its not making the correct searches, what am i doing wrong. Help is appreciated. heres my php code: Code: [Select] <?php $server = ""; // Enter your MYSQL server name/address between quotes $username = ""; // Your MYSQL username between quotes $password = ""; // Your MYSQL password between quotes $database = ""; // Your MYSQL database between quotes $con = mysql_connect($server, $username, $password); // Connect to the database if(!$con) { die('Could not connect: ' . mysql_error()); } // If connection failed, stop and display error mysql_select_db($database, $con); // Select database to use // Query database $result = mysql_query("SELECT * FROM Properties"); // Set up our error check and result check array $error = array(); $results = array(); // First check if a form was submitted. // Since this is a search we will use $_GET if (isset($_GET['search'])) { $searchTerms = trim($_GET['search']); $searchTerms = strip_tags($searchTerms); // remove any html/javascript. if (strlen($searchTerms) < 10) { $error[] = "Search terms must be longer than 10 characters."; }else { $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection. } // If there are no errors, lets get the search going. if (count($error) < 1) { $searchSQL = "SELECT sid, sbody, stitle, sdescription FROM simple_search WHERE "; // grab the search types. $types = array(); $types[] = isset($_GET['images'])?"`simages` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Location'])?"`sLocation` LIKE '%{$searchTermDB}%'":''; $types[] = isset($_GET['Number of bedrooms'])?"`snumberofbedrooms` LIKE '%{$searchTermDB}%'":''; $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked) if (count($types) < 1) $types[] = "`simages` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked $andOr = isset($_GET['matchall'])?'AND':'OR'; $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `sLocation`"; // order by title. $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}"); if (mysql_num_rows($searchResult) < 1) { $error[] = "The search term provided {$searchTerms} yielded no results."; }else { $results = array(); // the result array $i = 1; while ($row = mysql_fetch_assoc($searchResult)) { $results[] = "{$i}: {$row['sLocation']}<br />{$row['sNumberofbedrooms']}<br />{$row['sbody']}<br /><br />"; $i++; } } } } function removeEmpty($var) { return (!empty($var)); } if (!$result) { echo "Error running query:<br>"; trigger_error(mysql_error()); } elseif(!mysql_num_rows($result)) { // no records found by query. echo "No records found"; } else { $i = 0; echo '<div class="container" style="float:left;">'; while($row = mysql_fetch_array($result)) { // Loop through results $i++; echo '<div class="imageholder" style="float:left;">'; echo '<img class="image1" src="'. $row['images'] .'" />'; //image echo '</div>'; echo '<div class="textholder" style="font-family:helvetica; font-size:13px; float:left; padding-top:10px;">'; echo "<span style=\"color:green;\"><b>Displaying record $i<br>\n</b><br></span>"; echo "<b>" . $row['id'] . "</b><br>\n"; // Where 'id' is the column/field title in the database echo "Location: ". $row['Location'] . "<br>\n"; // Where 'location' is the column/field title in the database echo "Property Type: ". $row['Property_type'] . "<br>\n"; // as above echo "Bedrooms: ". $row['Number_of_bedrooms'] . "<br>\n"; // .. echo "Purchase Type: ". $row['Purchase_type'] . "<br>\n"; // .. echo "Price: ". $row['Price_range'] . "<br>\n"; // .. echo '</div>'; echo '<div style="clear:both"></div>'; } echo '</div>'; } mysql_close($con); // Close the connection to the database after results, not before. ?> I just changed around my database and as such I need to update my search form. The way it used to work was a user could use the search bar to look for actors. In changing my database there in no longer a actor column, there are how ever actor_1, actor_2 ... until actor_7. What I am trying to do it set it up so my form looks the same but the back end will look in all the actor columns for the name. So when a user selects actor from the drop down menu it will look in all 7 actor columns. Here is what i had before that worked. for the search bar Code: [Select] <form action="/search.php" method="post"> <span class="rulesub">Search: </span><input type="text" name="search"> <select size="1" name="dropdown"> <option value="" selected>Search by...</option> <option value="title">Title</option> <option value="actors">Actors</option> <option value="difficulty">Difficulty 1-5</option> </select> <input type="Submit" value="Search" name="Submit"> </form> for the search page Code: [Select] $connect = mysql_connect('localhost', $username, $password) or die ("Unable to connect to host"); mysql_select_db($database) or die ("Unable to connect to database"); $search = empty($_POST['search'])? die ("Please enter search criteria.") : mysql_escape_string($_POST['search']); $dropdown = empty($_POST['dropdown'])? die ("Please select from search criteria.") : mysql_escape_string($_POST['dropdown']); $query = mysql_query("SELECT * FROM movie WHERE $dropdown Like '%$search%'") or die (mysql_error()); if(mysql_num_rows($query) == 0) { printf("Could not find $search while looking in $dropdown, please try again."); //exit; } else{ $num=mysql_numrows($query); mysql_close($connect); ?> Output thanks Hi, I was wondering could anyone help me here please, I'm pretty stuck and not sure what's wrong. I have also posted this topic on Daniweb if that is OK as I'm kind of stuck for time on this (If I'm not allowed post on multiple forums this can be deleted). I have a database that contains files and I want to be able to search those files by putting in a ID number into a textbox on my homepage of a website I'm working on. In my databse my doc_id is the primary key, and I want the user to be able to enter a document ID and have all the information returned to them. Can anyone help me here as I don't understand why it isn't working? Here is the code as it is at the moment - Code: [Select] <?php include 'connect_db.php'; include 'newheader.php'; function sanitize_data($data) { $data = array_map('trim',$data); $data = array_map('strip_tags',$data); $data = array_map('htmlspecialchars',$data); $data = array_map('mysql_real_escape_string',$data); return $data; } $post = sanitize_data($_POST); if (isset($_POST['searchID'])) { $find = $_POST['find']; $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $field = $_POST['field']; $data = mysql_query("SELECT * FROM tc_tool.forms WHERE upper(".$field.") LIKE'%$find%'") or die(mysql_error()); while ($result = mysql_fetch_array( $data )) { echo $result['doc_number']; echo " "; echo $result['doc_title']; echo " "; echo "<br>"; echo "<br>"; } ?> The error returned to me is this - Incorrect parameter count in the call to native function 'upper' Here is the code to my button too if it is any use - Code: [Select] <body onLoad = "documentNumber.focus()"> <input type= "text" id= "documentNumber" name= "searchbyequipmenttype" class= "eSearchFormInput"/> <input type= "submit" name= "searchID" value="Search By Doc ID" /><br /> And I have no idea why it is, can anyone help if possible? I have tried it a few ways so if needed I'll post the other ways I've tried it. Thanks if anyone can help hi iam trying to make a simple search form to search the members tables based on there input. iam new php so most of my code is guess work Code: [Select] <form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm"> <label for="country">Country:</label> <input type="text" name="country" /> <label for="state">State:</label> <input type="text" name="state" /> <label for="city">City:</label> <input type="text" name="city" /> <input type="submit" value="submit" /> </form> <? if ($_GET == array ()) { //Define a variable that will be used to query the members; in this case, it would select all members $query = "SELECT * FROM users"; } else { //If the user typed at least one thing (in the form OR the url) if (count ($_GET) == 1) { //If what they typed is only for one criteria, define a variable that creates a query for only ONE criteria to search for $query = "SELECT * FROM users WHERE 1"; foreach ($_GET as $field => $value) { $query .= " AND $field = '$value'"; } //If the user has typed in more than one field and hits search } else { //Define a variable for a query that selects members based off each criteria $query = "SELECT * FROM users WHERE 1"; foreach ($_GET as $field => $value) { $query .= " AND $field LIKE '%$value%'"; } } while($info = mysql_fetch_array( $data )) { Echo "<img src='http://datenight.netne.net/images/".$info['img'] ."' width='150' height='250''> <br>"; Echo "<b>Name:</b> ".$info['username'] . "<br> <hr>"; Echo "<b>Sex:</b> ".$info['sex'] . " <br><hr>"; Echo "<b>Intrested in</b>" . "<br><hr>"; Echo "".$info['rel'] . " "; Echo "".$info['frwb'] . " "; Echo "".$info['ons'] . " "; Echo "".$info['fr'] . "<br><hr>"; Echo "<b>About me:</b> ".$info['aboutme'] . "<br><hr> "; Echo "<b>Looking for:</b> ".$info['looking'] . " <br><hr>"; Echo "<a href='login_success.php'>'Back'</a>"; } ?> </body> </html> while($info = mysql_fetch_array( $data )) { is not vaild error Hi, Hoping someone could help me, im not great at php programming and im trying to implement a search form which searches a sql database but for some reason its not working i keep on getting the following error.. " Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\site\contact\search.php on line 141" Im not sure why this happens and its really starting to bug me now :/ I have to pages to the search feature, one page which contains the <form> and the other with the PHP code.... the form.. <form method="get" action="search.php"> <label>Search Term: <input type="text" id="query" name="query"/> </label> <label> <select name="category" id="category"> <option value="none">Please Select a Type</option> <option value="teamname">Manager Name</option> <option value="manager">Team Name</option> <option value="age">Age Group</option> </select> </label> <input name="Search" type="submit" value="Search"/> </form> the PHP code.. <?php $query=$_GET['query']; $db_host="localhost"; $db_username="root"; $db_password=""; $db_name="info"; $db_tb_name="info"; $db_tb_atr_name=$_GET['category']; mysql_connect("$db_host","$db_username","$db_password"); mysql_select_db("$db_name"); $query_for_result=mysql_query("SELECT * FROM $tb_name WHERE $db_tb_atr_name like '%".$query."%'"); while($data_fetch=mysql_fetch_array($query_for_result)) { echo "<p>"; echo $data_fetch['table_attribute']; echo "</p>"; } mysql_close(); ?> Id be really great full for any light you guys could shed on this for me with thanks, fozze Hi, I have search form which works apart from 1 part, I have set the results limit to 10 per page with a next link if there are more than 10 results, however when the next link is clicked, i get the error 'you do not have a search parameter' which comes from this code: Code: [Select] if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } The next link is defined by the code below: Code: [Select] // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } Can anyone see where I have gone wrong with this code without me sending the whole search form? Any help would be very much appreciated. Thanks Hey guys, Can someone help me code kind of a "advanced search" form, that will get a URL: Code: [Select] <form method="get"> <input type="checkbox" name="level" value="PreK" /> <input type="checkbox" name="level" value="Elem" /> <input type="checkbox" name="level" value="MS" /> <input type="checkbox" name="level" value="HS" /> <input type="checkbox" name="subject" value="Math" /> <input type="checkbox" name="subject" value="Reading" /> <input type="checkbox" name="level" value="Science" /> <input type="submit" value="submit" /> </form> So... if I ticked PreK, Elem and Math, the resulting link would be: www.mysite.com?level=PreK&Elem&subject=Math The most complicated thing... how would I get place the "&" in between variables? Thank you once again. ~Wayne |