lalarukh12 Posted August 6, 2012 Share Posted August 6, 2012 hi.... i followed the ecommerce chapter of your book and want to know if we want to add a search form in the page and perform fulltext searcher what modifications we have to do in the code ? please help Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 7, 2012 Author Share Posted August 7, 2012 HELP ME PLEASE I AM STUCK Link to comment Share on other sites More sharing options...
Edward Posted August 7, 2012 Share Posted August 7, 2012 That's a very brief question, how are we supposed to know that considering we have no idea even what your web site is or what the functions of it must be? 1 Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 7, 2012 Author Share Posted August 7, 2012 <form action="browse_dresses.php" method="get"> <fieldset> <legend>Site Search</legend> <input type="text" name="a" value= "<? $product_search; ?>" /> <input type="submit" name="go" id="go" value="Search" /> </fieldset> <?php require_once ('../mysqli_connect.php'); $query = ("SELECT * FROM first_name,last_name, MATCH(first_name, last_name) AGAINST('$product_search') AS first_name FROM designers WHERE MATCH(first_name, last_name) AGAINST('$product_search') ORDER BY first_name DESC"); // Perform Query $results = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$results) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } // Store item information $chosen_item = mysql_fetch_array($results); ?> i am using this code and having a number of errors and have no idea how to fix Link to comment Share on other sites More sharing options...
margaux Posted August 7, 2012 Share Posted August 7, 2012 What errors are you receiving? Your query is incorrect. You need to have a tablename following the first FROM. The second FROM clause is redundant and will also throw up an error. Which fields do you want to retrieve? You've specified 2 sets of fields - all by using *, and first_name, last_name. Run your query directly using phpmyadmin or some other mysql client and you will get some helpful error messages. where is $product_search coming from - your code does not show where you are setting it to something that you can search against so I'm guessing you are getting some kind of undefined variable error message. 1 Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 7, 2012 Author Share Posted August 7, 2012 actually i followed the same code as in the book php6 and mysql chapter ecommerce with different table and field names modified and i want to add a search form on the home page to let the users enter a keyword and it ll search the two designers and dresses tables. as they were artists and prints in the chapter and have the same fields as in chapter. what ll be the correct code then? Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 7, 2012 Author Share Posted August 7, 2012 please help me Link to comment Share on other sites More sharing options...
Jaepee Posted August 7, 2012 Share Posted August 7, 2012 You could use the query $first_name = $_POST['first_name']; //don't forget mysqli_real_escape_string() function. $last_name = $_POST['last_name']; $q = "SELECT * FROM your_table WHERE first_name LIKE '%$first_name%' AND last_name LIKE '%$last_name%'"; Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 8, 2012 Author Share Posted August 8, 2012 oki i used this query but what code should i use above and below it :-s Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 8, 2012 Author Share Posted August 8, 2012 Notice: Undefined index: first_name in C:\xampp\htdocs\includes\header.php on line 52 Notice: Undefined index: last_name in C:\xampp\htdocs\includes\header.php on line 53 Notice: Undefined variable: query in C:\xampp\htdocs\includes\header.php on line 56 Notice: Undefined variable: query in C:\xampp\htdocs\includes\header.php on line 62 Invalid query: Query was empty Whole query: it gives these errors Link to comment Share on other sites More sharing options...
HartleySan Posted August 8, 2012 Share Posted August 8, 2012 Something's likely wrong with your $_POST array. Are values being properly posted to the PHP script? Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 8, 2012 Author Share Posted August 8, 2012 yeah i just corrected it now it is giving no errors but it gives no results and returns the same page..... what is the problem now? Link to comment Share on other sites More sharing options...
HartleySan Posted August 8, 2012 Share Posted August 8, 2012 Perhaps the SQL search you are performing doesn't actually match any records in the DB. Before you worry about the PHP logic, I would recommend doing some testing by running SQL queries with static values directly on the DB from phpMyAdmin, or something like that. Once you have confirmed that the queries themselves are okay, then confirm you're getting the values you're expecting in the PHP script by echoing the POST values to the screen. If that's okay, then go from there. Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 8, 2012 Author Share Posted August 8, 2012 $q = "SELECT * FROM your_table WHERE first_name LIKE '%$first_name%' AND last_name LIKE '%$last_name%'"; this query returns nothing but when i use some alphabet like h between % this, then i get results matching with the alphabet. what should i do? Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 8, 2012 Author Share Posted August 8, 2012 your_table is designers in my script Link to comment Share on other sites More sharing options...
HartleySan Posted August 8, 2012 Share Posted August 8, 2012 If you echo $first_name and $last_name to the screen, do you get what you expect? Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 8, 2012 Author Share Posted August 8, 2012 Notice: Undefined variable: first_name in C:\xampp\htdocs\includes\header.php on line 16 Notice: Undefined variable: last_name in C:\xampp\htdocs\includes\header.php on line 16 these errors come up when i echo Link to comment Share on other sites More sharing options...
Larry Posted August 8, 2012 Share Posted August 8, 2012 Just as an aside, if you ever again post a message that just says "please help me" or something along those lines, I will permanently ban you from the forums. You've posted your message, now be patient. People will help when and if they can. If you need something to pass the time, try reading the forum guidelines. 1 Link to comment Share on other sites More sharing options...
Jaepee Posted August 9, 2012 Share Posted August 9, 2012 <input type="text" name="a" value= "<? $product_search; ?>" /> I beleive is incorrect. it should be use 'post' <input type="text" name="product_search" size="40" maxlength="60" /> Then $product_search = $_POST['product_search']; // you have to assign your GET or POST data to the variable that you are trying to match in the database... if (isset($_POST['product_search']) { $product_search = $_POST['product_search']; } I highly recommend that you read more than just the e-commerce section of the book. Doing so will enable you to fully understand the internal workings of php and the code that is above. cheers jp Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 12, 2012 Author Share Posted August 12, 2012 hi thanks for your reply. i am using this code now to search a single table when user enters a first name. the code is : <html> <head> <title>Search the Database</title> </head> <body> <html> <head> <title>Search the Database</title> </head> <body> <form action="searchlast.php" method="post"> Search: <input type="text" name="term" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> </body> </html> <?php echo $_POST['term']; require_once ('mysqli_connect.php'); $term = $_POST['term']; $sql = mysql_query("select * from designers where first_name like '%$term%'"); $term = $_POST['term']; $sql = mysql_query("select * from designers where first_name like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo 'designer_id: '.$row['designer_id']; echo '<br/> First Name: '.$row['first_name']; echo '<br/> Last Name: '.$row['last_name']; echo '<br/><br/>'; } ?> and i am getting these errors: Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 30 Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 40 Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 48 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\searchlast.php on line 52 do someone know the solution please? Link to comment Share on other sites More sharing options...
lalarukh12 Posted August 12, 2012 Author Share Posted August 12, 2012 thanks for your reply. now i am using the following code to search a single table when user enters a keyword. <html> <head> <title>Search the Database</title> </head> <body> <html> <head> <title>Search the Database</title> </head> <body> <form action="searchlast.php" method="post"> Search: <input type="text" name="term" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> </body> </html> <?php echo $_POST['term']; require_once ('mysqli_connect.php'); $term = $_POST['term']; $sql = mysql_query("select * from designers where first_name like '%$term%'"); $term = $_POST['term']; $sql = mysql_query("select * from designers where first_name like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo 'designer_id: '.$row['designer_id']; echo '<br/> First Name: '.$row['first_name']; echo '<br/> Last Name: '.$row['last_name']; echo '<br/><br/>'; } ?> and getting the following errors Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 30 Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 40 Notice: Undefined index: term in C:\xampp\htdocs\searchlast.php on line 48 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\searchlast.php on line 52 do someone know the solution please? Link to comment Share on other sites More sharing options...
HartleySan Posted August 12, 2012 Share Posted August 12, 2012 You can't just blindly try to grab values from the $_POST array. You have to first test whether the $_POST array has any values in it to begin with, which will only be the case when the form is submitted. Try putting a majority (if not all) of your PHP code within the following if condition: if (isset($_POST['term'])) Link to comment Share on other sites More sharing options...
margaux Posted August 12, 2012 Share Posted August 12, 2012 As an aside It would help if you proofread your post before posting as you appear to have duplicate tags and queries. Also it would help if you inserted your code between code tags (there is an icon you can use on the edit bar). Here's why you're getting the undefined index error: The first time your script is run, the form has not been submitted so the $_POST array will not have any values. You need to start with a conditional to check for this index if (isset($_POST['term'])) { You're using the improved version of mysql (mysqli) to connect to the database and the old version mysql to make your query. Here is a simplified version of what I think you're trying to do using mysqli. You may need to change the $dbc variable to match what is in your mysqli_connect.php file <form action="searchlast.php" method="post"> Search: <input type="text" name="term" /><br /> <input type="submit" name="submit" value="Submit" /> </form> <?php if (isset($_POST['term'])) { echo $_POST['term']; require_once ('mysqli_connect.php'); $term = $_POST['term']; $query = "select * from designers where first_name like '%$term%'"; $result = mysqli_query($dbc, $query); while ($row = mysqli_fetch_array($result)){ echo 'designer_id: '.$row['designer_id']; echo '<br/> First Name: '.$row['first_name']; echo '<br/> Last Name: '.$row['last_name']; echo '<br/><br/>'; } } ?> 1 Link to comment Share on other sites More sharing options...
Jaepee Posted August 12, 2012 Share Posted August 12, 2012 I was gonna post but I like Margauxs' post ... Link to comment Share on other sites More sharing options...
Recommended Posts