View Full Version : script problem
Hi all? How are you doing? What I am doing is i try to run "searching script" If a user are looking for a PHP book, it comes out PHP book infomation from my database.
but my script doesn't work at all
my php editor says"
Notice: Undefined variable: searchtype in c:itosearch.php on line 8
you have not entered search details. Please go back and try again.
and my php is 4.22
could you please help this script?
Here is code
<html>
<head></head>
<body>
<?php
if(!$searchtype || !$searchterm)
{
echo"you have not entered search details. Please go back and try again.";
exit;
}
$searchtype = addcslashes($searchtype);
$searchterm = addcslashes($searchterm);
@ $db = mysql_pconnect("localhost","itohideo","1234");
if(!$db){
echo"Error: Could not connet to dateabse. Please try again later.";
exit;
}
trim($searchterm);
mysql_select_db("itohideo");
$sql="select * from books where".$searchtype." like '%".$searchterm."%'";
$result=mysql_query($sql);
$num_results=mysql_num_rows($result);
echo"<p>Number of books found: ".$num_results."</p>";
for($i=0; $i<$num_results; $i++)
{
$row=mysql_fetch_array($result);
echo "<p><strong>".($i+1).".Title: ";
echo htmlspecialchars( stripslashes($row["title"]));
echo"</strong><br>Author: ";
echo htmlspecialchars( stripslashes($row["atthor"]));
echo"<br>ISBN: ";
echo htmlspecialchars( stripslashes($row["isbn"]));
echo"<br>Price: ";
echo htmlspecialchars( stripslashes($row["price"]));
echo "</p>";
}
?>
</body>
</html>
craigh@mac.com
09-19-2002, 09:13 PM
it looks to me like set_globals is off and your script expects it to be on.
register_globals is off
how can I swtich to "ON"??
Is that any problem "register_globals"??
Moreover I think that my database command should be wrong.
it doesn't come data from my database. Something worng.
Just only show my text
PHP:
"you have not entered search details. Please go back and try again."
Please tell me what it is going on
Thank you very much
craigh@mac.com
09-19-2002, 09:34 PM
register_globals - yeah that's what I meant. :-)
It is difficult to tell what is wrong without knowing more. Are you using someone else's script? I'm guessing so - and I'm also guessing that it was written pre-PHP 4.1 so it is expecting register_globals to be ON. There is some security risk in turning it on however so you may want to reconsider. You could just define the variables at the top of the script. Is this script processing a form? If so, then I think you could do this:
$searchtype=$_POST["searchtype"];
$searchterm=$_POST["searchterm"];
at the very beginning. maybe.
Spookster
09-19-2002, 10:22 PM
Also it's really not a good idea to check if a variable has been set using this:
if(!$searchtype || !$searchterm)
Instead you can use php's isset function:
if(!isset($searchtype) || !isset($searchterm))
I did your way but it jsut comes this message
you have not entered search details. Please go back and try again.
I think that my sql is problem becasue I get that message above
"you have not entered search details. Please go back and try again."
i did command like this
create table books( isbn char(13) not null primary key
author char(30),title cahr(60),price float(4,2));
Is it right???
I am not sure it must be wrong???
Here is new code
<html>
<head></head>
<body>
<?php
if(!isset($searchtype) || !isset($searchterm))
{
echo"you have not entered search details. Please go back and try again.";
exit;
}
$searchtype = addcslashes($searchtype);
$searchterm = addcslashes($searchterm);
@ $db = mysql_pconnect("localhost","itohideo","1234");
if(!$db){
echo"Error: Could not connet to dateabse. Please try again later.";
exit;
}
trim($searchterm);
mysql_select_db("itohideo");
$sql="select * from books where".$searchtype." like '%".$searchterm."%'";
$result=mysql_query($sql);
$num_results=mysql_num_rows($result);
echo"<p>Number of books found: ".$num_results."</p>";
for($i=0; $i<$num_results; $i++)
{
$row=mysql_fetch_array($result);
echo "<p><strong>".($i+1).".Title: ";
echo htmlspecialchars( stripslashes($row["title"]));
echo"</strong><br>Author: ";
echo htmlspecialchars( stripslashes($row["atthor"]));
echo"<br>ISBN: ";
echo htmlspecialchars( stripslashes($row["isbn"]));
echo"<br>Price: ";
echo htmlspecialchars( stripslashes($row["price"]));
echo "</p>";
}
?>
</body>
</html>
it is not working. I think that my Html code must be wrong.
Could you please check it for me. Thank you for your time.
please help me
Here is code
<body>
<h1>Book-O-Rama Catalog Search</1>
<form action="result.php" metod="post">
Choose Search Type:<br>
<select name="searchtype">
<option value="author">author
<option value="title">title
<option value="isbn">ISBN
</select>
<br>
Enter Search Term:<br>
<input name="searchterm" type="text">
<br>
<input type="submit" value="Search">
</form>
</body>
</html>
Please help me.
Thank you very much
Spookster
09-20-2002, 07:35 PM
Did you not put this at the top of your script as Craig mentioned?
$searchtype=$_POST["searchtype"];
$searchterm=$_POST["searchterm"];
I sitll get this message
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\ito\ps\result.php on line 29
Number of books found:
I don't know why???
mordred
09-20-2002, 08:03 PM
Tip: Copy your SQL query to the clipboard and run it in phpMyAdmin. That way you can make sure that the query works properly before you run it in your script, and any eventual errors will be directly shown to you.
Spookster
09-20-2002, 08:29 PM
Your problem is right here:
$sql="select * from books where".$searchtype." like '%".$searchterm."%'";
You need to put a space after that WHERE command. Like this:
$sql="select * from books where ".$searchtype." like '%".$searchterm."%'";
Also you need to spell method correctly in your form:
<form action="result.php" metod="post">
metod != method.
Thanks guys it works
Thank you very much for helping
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.