Jump to content

Searching through mysql database using php. Probably an easy fix

- - - - -

  • Please log in to reply
No replies to this topic

#1
pathtotake

pathtotake

    Newbie

  • Members
  • PipPip
  • 13 posts
I copied code from a tutorial website but I cant get it to work. I striped the drop down menu to show fields because I didnt need that.

But here is what I have in the search page (page1.php)

<h2>Search</h2>

 <form name="search" method="post" action="Page2.php">

 Seach for: <input type="text" name="name" /> in

 <input type="submit" name="search" value="Search" />

 </form>



And here is the search code (page2.php)


<?


 $username="a4579259_sean";

$password="******";

$database="a4579259_phptest";

$host = "mysql12.000webhost.com";



$name = $_POST ['name'];

 //This is only displayed if they have submitted the form

 


 //If they did not enter a search term we give them an error

 if ($name != "")

 {

  echo "<h2>Results</h2><p>";


 


 // Otherwise we connect to our Database

 mysql_connect($host,$username,$password);


@mysql_select_db($database) or die( "Unable to select database");

$data="CREATE TABLE students (id int(6) NOT NULL auto_increment,first varchar(15)

NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,address text

 NOT NULL,dob date NOT NULL, PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))

";

mysql_query($query);



$data = "INSERT INTO students VALUES ('','$first','$last','$phone','$address','$dob')";

mysql_query($data);


 // We preform a bit of filtering

 $name = strtoupper($name);

 $name = strip_tags($name);

 $name = trim ($name);


 //Now we search for our search term, in the field the user specified

 $data = mysql_query("SELECT * FROM students WHERE upper($first) LIKE'%$name%'");

$result=mysql_query($data);


 //And we display the results

 while($result = mysql_fetch_array( $data ))

 {

 echo $result['name'];

 echo " ";


 }


 //This counts the number or results - and if there wasn't any it gives them a little message explaining that

 $anymatches=mysql_num_rows($data);

 if ($anymatches == 0)

 {

 echo "Sorry, but we can not find an entry to match your query<br><br>";

 }


 //And we remind them what they searched for

 echo "<b>Searched For:</b> " .$name;

 }

 ?>

I get these errors:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a4579259/public_html/mysqlstudsearch/Page2.php on line 44

and

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a4579259/public_html/mysqlstudsearch/Page2.php on line 52

and regardless of what i search for it says:

Sorry, but we can not find an entry to match your query

Searched For: SEAN

Which is what its meant to say if there is no one named sean but there is:

http://seanstest.comlu.com/mysqlstudentexe/Page2.php


Now Ive just realised that when i search for something it adds it to the database.
I think its the - $data = "INSERT INTO students VALUES ('','$first','$last','$phone','$address','$dob')";

ive taken that out now.


The site i got the code from:

http://php.about.com/od/phpwithmysql/ss/php_search_3.htm



Any help?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users