What does this mean?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Robbie
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2003 6:52 am
Location: Melbourne, Australia
Contact:

What does this mean?

Post by Robbie »

Hello there :)

I'm going through the process of trying to teach myself php and mysql. I'm working from a book and I've come accross a problem. It's a basic search function on a web page. The code on search.php page where the user would select the criteria of the search goes like this:

<body>
<h1>Book-O-Rama Catalog Search</h1>

<form action="results.php" method="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="searchitem" type=text>
<br>
<input type=submit value="Search">
</form>

and the code on my results.php page looks like this:

<h1>Book-O-Rama Search Results</h1>
<?php
trim($searchitem);
if (!searchtype || !$searchitem)
{
echo "You have not entered Search details. Please go back and try again";
exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

@ $db = mysql_pconnect("localhost", "dbname", "dbname");

if (!$db)
{
echo "Error. Could not Connect to database. Please try again";
exit;
}

mysql_select_db("books");
$query = "select * from books where ".$searchtype." like'%".$searchterm."%'";
$result = mysql_num_row($query);
$num_results = mysql_num_rows($result);

echo "<p>Number of books found: ".$num_result."</p>";

for ($i=0; $i <$numresults; $i++)
{
$row = mysql_fetch_array($result);
echo"<p><strong>".($i+1).". Title: ";
echo htmlspecialchars( stripslashes($row["title"]));
echo "<strong><br>Author: ";
echo htmlspecialchars (stripeslashes($row["author"]));
echo "<br>ISBN: ";
echo htmlspecialchars (stripeslashes($row["isbn"]));
echo "<br>Price: ";
echo htmlspecialchars (stripeslashes($row["price"]));
echo "</p>";
?>

Now... The problem is When i click search it comes up with an error message on results page saying:

Parse error: parse error in c:\phpdev\www\tests\results.php on line 46

the only code on Line 46 of my code </html>, the end of the document.

:? what would be causing this and how can i fix it?

Any Help would be much appreciated :)

Thankyou

Robert
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

you can almost garuntee that if you get an error like that that is outside of the php then you have forgotten either a ; or a }

low and behold, look at your for loop ;)
Robbie
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2003 6:52 am
Location: Melbourne, Australia
Contact:

Post by Robbie »

ahh..thankyou...my eyes aren't trained well enough yet to spot these things....
Robbie
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2003 6:52 am
Location: Melbourne, Australia
Contact:

Post by Robbie »

O.k... I've fixed this problem so i'm not getting an error message. But When i enter data into the text boxes and press submit it It comes up with a Message saying
You have not entered Search details. Please go back and try again
which is coming from this script

Code: Select all

trim($searchitem);
	if (!searchtype || !$searchitem)
	&#123;
		echo "You have not entered Search details. Please go back and try again";
		exit;
		&#125;
	$searchtype = addslashes($searchtype);
	$searchterm = addslashes($searchterm);
The rest of the code looks like this:

Code: Select all

@ $db = mysql_pconnect("localhost", "dbname", "dbname");
	
	if (!$db)
	&#123;
		echo "Error. Could not Connect to database. Please try again";
		exit;
		&#125;
		
	mysql_select_db("books");
	$query = "select * from books where ".$searchtype." like'%".$searchterm."%'";
	$result = mysql_num_row($query);
	$num_results = mysql_num_rows($result);
	
	echo "<p>Number of books found: ".$num_result."</p>";
	
	for ($i=0; $i <$numresults; $i++)
	&#123;
		$row = mysql_fetch_array($result);
		echo"<p><strong>".($i+1).". Title: ";
		echo htmlspecialchars( stripslashes($row&#1111;"title"]));
		echo "<strong><br>Author: ";
		echo htmlspecialchars (stripeslashes($row&#1111;"author"]));
		echo "<br>ISBN: ";
		echo htmlspecialchars (stripeslashes($row&#1111;"isbn"]));
		echo "<br>Price: ";
		echo htmlspecialchars (stripeslashes($row&#1111;"price"]));
		echo "</p>";
		&#125;
?>
I think i maybe running old code in a new version of php but would like to know if there's something else .

Please Note: I'm only in my third day of learning php and mysql so excuse me if my questions seem a little silly..

thankyou

Robert
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

That's right, old code on a new version, have a read of this:
viewtopic.php?t=511

and try this:

Code: Select all

<h1>Book-O-Rama Search Results</h1> 
<?php 
/* We can easily trim each of the values in the $_POST array, which is where all data
   POSTed from forms ends up. */
foreach ($_POST as $key => $value) {
	$_POST[$key] = trim($value);
}

/* Use empty() to determine if there is no data in a variable or if the variable is not
   set, just doing !$variable is going to cause you errors and is an old, not very good
   way of checking for data. */
if (empty($_POST['searchtype']) || empty($_POST['searchterm'])) { 
	echo 'You have not entered Search details. Please go back and try again';
} else {
	/* Using else insted of exiting the script if there was no data entered will make the script
	   easier to follow, IMHO */

	/* Now we can set the variables to easier to type variable names */
	$searchtype = addslashes($_POST['searchtype']);
	$searchterm = addslashes($_POST['searchterm']);

	@$db = mysql_connect('localhost', 'dbname', 'dbname') or die('<p>Could not connect to database.</p>'.mysql_error());
	@mysql_select_db('books') or die(mysql_error());

	/* In your SQL statement explicitely state which columns you want to select data from, using * 
	   means all data for all columns is selected whether you want it or not.  This method can
	   also make debugging easier. */
	$sql = "SELECT title, author, isbn, price FROM books WHERE ".$searchtype." LIKE '%".$searchterm."%'";

	/* You need to use mysql_query to query the database */
	$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
	$num_results = mysql_num_rows($result);

	echo '<p>Number of books found: '.$num_results.'</p>';

	/* We can make this a proper numbered list: */
	if ($num_results > 0) {
		echo '<ol>';
		/* Use a while loop instead of a for one here as it'll produce the same result but with simpler code */
		while ($row = mysql_fetch_assoc($result)) {
			echo '<li><p><b>Title:</b> '.htmlspecialchars(stripslashes($row['title'])).'<br />';
			echo '<b>Author</b>: '.htmlspecialchars(stripslashes($row['author'])).'<br />';
			echo '<b>ISBN:</b> '.htmlspecialchars(stripslashes($row['isbn'])).'<br />';
			echo '<b>Price:</b> '.htmlspecialchars(stripslashes($row['price'])).'</p></li>';
		}
		echo '</ol>';
	}

}
Your questions aren't silly, it's just that lots of books which seemed quite good a year or so ago have become very out-of-date recently.

Mac
Robbie
Forum Newbie
Posts: 4
Joined: Mon Mar 31, 2003 6:52 am
Location: Melbourne, Australia
Contact:

Post by Robbie »

Thanks for your efforts with that code but it still isnt working. If there was a problem connecting to the database it would come back with the Message "Could not connect to database." Correct? It Still says "You have not entered Search details. Please go back and try again"... hmmmm I'm starting to understand what does what I just can't see why it's doing what it's doing..... :(
Post Reply