Help - Search - Members - Calendar
Full Version: php newbie, script not working properly
Hostony Board > General Support > PHP/MySQL
NMB
Hi, this is a simple form processor to show results of a search in singers table, for singer_name field. the search form points to this script on another page, but i keep getting the "problems" error as defined in the script see:
/////////////////////////////////////////////////////////////////////////////////////////////

<?php
$username="x";
$password="x";
$database="x";
$link = mysql_connect("localhost");
$query = mysql_connect("localhost");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

if ($search) // perform search only if a string was entered.
{
mysql_connect() or die ("Problem connecting to Database");

$query = mysql_query("SELECT singer_name FROM `singers` ORDER BY `singer_name` ASC");
$result = mysql_query("SELECT singer_name FROM `singers` ORDER BY `singer_name` ASC");

if ($query)
{
echo "<table width=100% border=0 cellpadding=0 cellspacing=5><tr>";

print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";

echo "</tr></table>";
} else { echo "problems...."; }
} else {
echo "Search string is empty. <br> Go back and type a string to search";
}

?>

/////////////////////////////////////////////////////////////////////////////////////////////

Any help appreciated, there may be a couple of errors here to sort... thanks
Niels
mmm there is alot of things wrong with that script.
I did this one for you , didnt test it tho , i hope it helps:

CODE
//GET search string
 $q=$GET['q'];
 
 //trim white spaces
 $var=trim($q);
 
 //empty check
 if($var==''){
 echo"Empty String";
 exit;
 }
 
 $query= "SELECT * FROM singers WHERE singer_name LIKE '%$var%' ORDER BY singer_name ASC";
 $result = mysql_query($query) or die(mysql_error());
 
 //count results
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
 //no results
 if($numrows==0){
 echo"No Results";
 exit;
 }
 
 while ($row= mysql_fetch_array($result)) {
     $singer_name = $row["singer_name"];
     echo"$singer_name<br>";
     }

blink.gif
NMB
I cant thank you enough, will test it now. Unfortunately im learning the hard way that not all scripts you find on the web are good! I have tried a lot of tutorials on the web, there isnt a lot of information on searching forms, and form processors. Just mail forms.

Taking the time to help me on my journey is very much appreciated smile.gif
NMB
I think i have a problem with writing my search form, when i submit this form i just get the error in process.php (the script given above) saying no search string entered.. where am i going wrong? Thanks

<body>
<?php
$username="x";
$password="x";
$database="x";
$link = mysql_connect("localhost");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
?>
<form name="form1" method="get" action="process.php">
<input name="singer_name" type="text" id="singer_name" value="">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
Niels
Try:

CODE
<form name="form1" method="get" action="process.php">
<input name="q" type="text" id="q" value="">
<input type="submit" name="Submit" value="Submit">
</form>


For debug you could always set a hard value for $q just replace:

$q=$GET['q'];

with

$q='michael jackson'; blink.gif

Cheers,
Niels
NMB
i changed the input and id name to q

still getting the empty string error.

(head hits desk)

do any special permissions have to be set in these files? i assume since its producing an error thats defined in process.php, its partially working
Niels
Oops my bad. change

$q=$GET['q'];

to

$q=$_GET['q'];
NMB
That works a treat. Your the man.
Niels
laugh.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.
IPS Driver Error

IPS Driver Error

There appears to be an error with the database.
You can try to refresh the page by clicking here