dubfoundry Posted March 28, 2009 Share Posted March 28, 2009 hi im building a search app and running into some prob... my code seem to be written correctly it connects to the database perfectly...but it doesnt yeild any results ..i just get the same page in return with no records...nor no errors. here's the code... <?php if (isset($_POST['submit'])){ if(isset($_GET['go'])){ if(preg_match("^/[A-Za-z]+/^", $_POST['name'])){ $name=$_POST['name']; //connect to the database $db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); //-select the database to use $mydb=mysql_select_db("sakila"); $sql="SELECT actor_id, first_name, last_name FROM actor WHERE first_name LIKE '%" . $name . "%' OR last_name LIKE '%" . $name ."%'"; $result=mysql_query($sql); //-create while loop and loop through result set while($row=mysql_fetch_array($result)){ $FirstName =$row['first_name']; $LastName=$row['last_name']; $ID=$row['actor_id']; //-display the result of the array echo "<ul>\n"; echo "<li>" . "<a href=\"search.php?id=$ID\">" .$FirstName . " " . $LastName . "</a></li>\n"; echo "</ul>"; }} }else{ echo "<p>please enter a search parameter</p>"; } } ?> any ideas are welcome thanks guys... Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 28, 2009 Share Posted March 28, 2009 are you posting this to a page like /search.php?go=xxx I don't have a problem with mixing get and post data - just wondering if you have used $_GET['go'] when you meant $_POST. to check i suggest you echo out your query string just before you run it. Quote Link to comment Share on other sites More sharing options...
dubfoundry Posted March 28, 2009 Author Share Posted March 28, 2009 no its posting to itself with that get variable....but that should'nt make a difference really i tried it with out passing any get variable to the url and got the same results so ... anymore ideas? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 28, 2009 Share Posted March 28, 2009 did you echo out the query string? you can always coy and paste it to run in your db management tool - see what it says about the query. Quote Link to comment Share on other sites More sharing options...
dubfoundry Posted March 28, 2009 Author Share Posted March 28, 2009 yea your right its the sql syntax it doesnt work at all when queried directly i got the result was empty...i took the $name variable out and used a letter like p... This worked once or twice but stop working it went back to just giving a empty respoinse.... can you tell if my syntax is inccorect check it SELECT actor_id, first_name, last_name FROM actor WHERE first_name LIKE '%" . p . "%' OR last_name LIKE '%" . p ."%'; Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 29, 2009 Share Posted March 29, 2009 last check - put... if (isset($_POST) && count($_POST) > 0){ print_r($_POST); } at the top of your code and see what is passed to the page. You should be able to see what is in the 'name' element. also echo out your query immediately before running it to see if the string contains your search terms.. echo $sql; $result=mysql_query($sql); use that information to check that you are assigning the correct value to $name. The query in your code is fine providing that $name is being corretly assigned a value. Quote Link to comment Share on other sites More sharing options...
dubfoundry Posted March 29, 2009 Author Share Posted March 29, 2009 looks like the correct value is being assigned this gets printed out Array ( [name] => pineapple plus [submit] => Search ) i typed pineapple as my search criteria 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.