colleyboy Posted July 7, 2010 Share Posted July 7, 2010 I am trying to make a script which connects to the database and searches within the database table and brings back matches. I have used two files. Search.php (for the form) and srch.php (the php that does the work). Could somebody please tell me where I am going wrong? It just keeps returning the "Please enter search string" all the time. boo. Search.php: <html> <head><title>Searching the Guest Book</title> </head> <body bgcolor=#ffffff> <h1>Searching the Database</h1> <form method="post" action="srch.php"> <table width=90% align=center> <tr><td>search for:</td><td><input type=text name='search' size=60 maxlength=255></td></tr> <td></td><td><input type=submit></td></tr> </table> </form> </body> </html> srch.php: <? include("dbinfo.inc.php"); if ($search) // perform search only if a string was entered. { mysql_connect(localhost,$username,$password) or die ("Problem connecting to Database"); $srch="%".$search."%"; $query = "select * from contacts WHERE first LIKE '$srch' || last LIKE '$srch' || phone LIKE '$srch' || mobile LIKE '$srch' || fax LIKE '$srch || email LIKE '$srch' || web LIKE '$srch'"; $result = mysql_db_query("contacts", $query); if ($result) { echo "Here are the results:<br><br>"; echo "<table width=90% align=center border=1><tr> <td align=center bgcolor=#00FFFF>First Name</td> <td align=center bgcolor=#00FFFF>Last Name</td> <td align=center bgcolor=#00FFFF>Phone Number</td> <td align=center bgcolor=#00FFFF>Mobile Number</td> <td align=center bgcolor=#00FFFF>fax</td> <td align=center bgcolor=#00FFFF>Email</td> <td align=center bgcolor=#00FFFF>Web</td> </tr>"; while ($r = mysql_fetch_array($result)) { // Begin while $first = $r["first"]; $last = $r["last"]; $phone = $r["phone"]; $mobile = $r["mobile"]; $fax = $r["fax"]; $email = $r["email"]; $web = $r["web"]; echo "<tr> <td>$first</td> <td>$last</td> <td>$phone</td> <td>$mobile</td> <td>$fax</td> <td>$email</td> <td>$web</td></tr> <tr> <td colspan=4 bgcolor=\"#ffffa0\">$comment</td> </tr>"; } // end while echo "</table>"; } else { echo "problems...."; } } else { echo "Search string is empty. <br> Go back and type a string to search"; } ?> Many thanks if anyone can help me. =D Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 if ($search) // perform search only if a string was entered. $search is not defined. The value would be referenced as $_POST['search'] Add this right before the IF condition $search = trim($_POST['search']); Quote Link to comment Share on other sites More sharing options...
colleyboy Posted July 7, 2010 Author Share Posted July 7, 2010 i sort of get it... but how do i implement it into the form? sorry im fairly new at php. Quote Link to comment Share on other sites More sharing options...
colleyboy Posted July 7, 2010 Author Share Posted July 7, 2010 I see. I have put that code in. But now when i press submit it produces the page: "Problems..." Have I done something wrong? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 That indicates the query is failing. Change the $result line to this and see if you don't get an error . . . $result = mysql_db_query("contacts", $query) or die('<br />This query string cratered: ' . $query . '<br />With this error: ' . mysql_error() ); Quote Link to comment Share on other sites More sharing options...
colleyboy Posted July 7, 2010 Author Share Posted July 7, 2010 Did not work. Instead it through back this error message at me: This query string cratered: select * from contacts WHERE first LIKE '%ian%' || last LIKE '%ian%' || phone LIKE '%ian%' || mobile LIKE '%ian%' || fax LIKE '%ian% || email LIKE '%ian%' || web LIKE '%ian%' With this error: Access denied for user 'wormste1_stock'@'localhost' to database 'contacts' Just to make sure. My database name is: wormste1_stock table within database name: contacts ----- I did try to change to "contacts" name to "wormste1_stock" but when i searched "ian" in the box and pressed submit it through this error at me: This query string cratered: select * from contacts WHERE first LIKE '%ian%' || last LIKE '%ian%' || phone LIKE '%ian%' || mobile LIKE '%ian%' || fax LIKE '%ian% || email LIKE '%ian%' || web LIKE '%ian%' With this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%ian%' || web LIKE '%ian%'' at line 1 ---- More help needed I think as I seem well out of my depth Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 Well "Problems..." isn't very helpful is it? Why don't you actually display the error (at least during the developement phase) so you can know what to fix? You query is failing because it is misformatted. You should echo the query and the error to the page. Here is a more logic format for the page - plus it will show you the errors needed to correct. <?php include("dbinfo.inc.php"); $HTMLoutput = ''; $search = trim($_POST['search']); if (empty($search)) { //No search value was entered $HTMLoutput .= "Search string is empty.<br />Go back and type a string to search."; } else { //Perform the database query mysql_connect(localhost,$username,$password) or die ("Problem connecting to Database"); $srch="%{$search}%"; $query = "SELECT first, last, phone, mobile, fax, email, web, comment FROM contacts WHERE first LIKE '$srch' OR last LIKE '$srch' OR phone LIKE '$srch' OR mobile LIKE '$srch' OR fax LIKE '$srch OR email LIKE '$srch' OR web LIKE '$srch'"; $result = mysql_db_query("contacts", $query); if (!$result) { //There was an error in running the query $HTMLoutput .= "problems....<br /><br />"; //The following line is for debugging only and //can be commented out once in production. $HTMLoutput .= "<b>Query:</b><br />{$query}<br /><b>Error:</b><br />" . mysql_error(); } elseif(mysql_num_rows($result)<1) { //No results from query $HTMLoutput .= "There were no matches."; } else { //Query ran successfully and had results $HTMLoutput .= "Here are the results:<br><br>"; $HTMLoutput .= "<table width=\"90%\" align=\"center\" border=\"1\">\n"; $HTMLoutput .= "<tr>\n"; $HTMLoutput .= "<th>First Name</th>\n"; $HTMLoutput .= "<th>Last Name</th>\n"; $HTMLoutput .= "<th>Phone Number</th>\n"; $HTMLoutput .= "<th>Mobile Number</th>\n"; $HTMLoutput .= "<th>fax</th>\n"; $HTMLoutput .= "<th>Email</th>\n"; $HTMLoutput .= "<th>Web</th>\n"; $HTMLoutput .= "</tr>\n"; while ($r = mysql_fetch_array($result)) { $HTMLoutput .= "<tr>\n"; $HTMLoutput .= "<td>{$r['first']}</td>\n"; $HTMLoutput .= "<td>{$r['last']}</td>\n"; $HTMLoutput .= "<td>{$r['phone']}</td>\n"; $HTMLoutput .= "<td>{$r['mobile']}</td>\n"; $HTMLoutput .= "<td>{$r['fax']}</td>\n"; $HTMLoutput .= "<td>{$r['email']}</td>\n"; $HTMLoutput .= "<td>{$r['web']}</td>\n"; $HTMLoutput .= "</tr>\n"; $HTMLoutput .= "<tr><td colspan=\"7\" bgcolor=\"#ffffa0\">{$r['comment']}</td></tr>\n"; } $HTMLoutput .= "</table>\n"; } } ?> <html> <head> <style> th { text-align: center; background-color: #00FFFF; } </style> </head> <body> <?php echo $HTMLoutput; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
colleyboy Posted July 7, 2010 Author Share Posted July 7, 2010 Just tryed that and I am getting exactly the same problems appearing as in my post before this. It is better to have it neater and i should have problems explained and not hidden as i am in experimental phase lol. I dont get whats going wrong though . Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 FYI: you're missing the closing single-quote here: fax LIKE '%ian% Quote Link to comment Share on other sites More sharing options...
colleyboy Posted July 7, 2010 Author Share Posted July 7, 2010 Excellent!!! All working now... lol me and my missing ' !! MANY Thanks!!!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 I really don't care if you use my code or not, but I did include code to handle when there are no results which your code does not. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 I was pointing out the missing quote because it's also missing in the code you wrote. if I implied the existing code should be used instead, I didn't mean to. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 7, 2010 Share Posted July 7, 2010 I was pointing out the missing quote because it's also missing in the code you wrote. if I implied the existing code should be used instead, I didn't mean to. No worries. I did notice that I copied the error. I did fix a lot of minor HTML formatting errors in the original code and provided a more logical flow. So, I hope my time wasn't wasted. But, no matter, the OP will do what he will. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.