Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/151503-search-app/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/151503-search-app/#findComment-795731
Share on other sites

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  ."%';

Link to comment
https://forums.phpfreaks.com/topic/151503-search-app/#findComment-795962
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/151503-search-app/#findComment-796198
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.