a simple search, got error,Anyone please?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jojo
Forum Newbie
Posts: 9
Joined: Wed Sep 11, 2002 2:50 am
Location: Canada

a simple search, got error,Anyone please?

Post by jojo »

8) Hi;
I have made a search page using one of the table of my db( there are two tables in my db), and gradually learn some about php/mysql searching.

now I get an error which i can't figure it out what is wrong, I appreciate if you take a look at my code, and see what is wrong with it.
If you think I could use a better code for search my db, please feel free to post, your effort is appreciated.

i get this error:
Parse error: parse error, unexpected T_ELSE in c:\inetpub\wwwroot\test.php on line 48

and this is on line 48: else {
and this the last "else" on the code page:

Code: Select all

<html>
<body>
<?php
 if($submit);{
# Type="MYSQL"
# HTTP="true"
$hostname_conn_amasys = "localhost";
$database_conn_amasys = "mytestMarch15";
$username_conn_amasys = "mytestpage";
$password_conn_amasys = "789456";
$conn_amasys = mysql_pconnect($hostname_conn_amasys, $username_conn_amasys, $password_conn_amasys) or die(mysql_error());
?>
<?php
$query="SELECT*FORM company WHERE LOWER (company_name) LIKE LOWER ( '%$keyword%') OR LOWER(job_title) LIKE LOWER('%$keyword%') ORDER BY id DESC";

$result=mysql_query($query);
if($myrow=mysql_fetch_array($result)){
	echo"<br><b> Search results for: </b><u>$keyword</u><br>";
	echo"<ol>";
	do{
		echo "<li>".$myrow["company_name"]."";
	}while ($myrow=mysql_fetch_array($result));
	echo "</ol><br>";
	?>
	<div align="center">
	<form method="psot" action="submit" value="search">
<table calss = "columns" align="center">
    <tr><td>
    <b> Search The Archive:</b><input type="text" name="keyword" size="15">
    <input type="submit" name="submit" value="g0>
    </td></tr></table></div></form>
<?php
}
else{
	echo "<br><b> NO Records Found for:</b><u>$keyword</u><br>";
	?>
	<div align ="center">
	<form method="post" action="search123.php">
	<input type="hidden" name="submit" value="search">
<table calss="columns" align="center">
<tr><td>
<b> Search:</b><input type="text" name="keyword" size="15">
<input type="submit" name="submit" value="g0>
</td></tr></table></div></form>
<?
}
}
else {  // this is line 48
	?>
	<div align="center">
	<form method="post" action="search123.php">
	<input type="hidden" name="submit" value="search">

<table calss="columns" align="center">
<tr><td>
<b> Search The Archive:</b>
<input type="text" name="keyword" size="15">
<input type="submit" name="submit" value="g0>
</td></tr></table></div></form>
<?php }
?>
</body>
</html>

thanks a lot.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<html><body>
<?php
if($submit)/** no ; here */ {
	# Type="MYSQL"
	# HTTP="true"
	$hostname_conn_amasys = "localhost";
	$database_conn_amasys = "mytestMarch15";
	$username_conn_amasys = "mytestpage";
	$password_conn_amasys = "789456";
	$conn_amasys = mysql_pconnect($hostname_conn_amasys, $username_conn_amasys, $password_conn_amasys) or die(mysql_error());
// removed end/start-tag
	/** avoid malicious user-input, see http://php.net/mysql_escape_string and http://www.php.net/manual/en/ref.info.p ... quotes-gpc */
	$keyword = mysql_escape_string($keyword);
	// LIKE is caseinsesitive (unless fields are binary)
	$query = "SELECT * FORM company WHERE company_name LIKE '%$keyword%' OR job_title LIKE '%$keyword%' ORDER BY id DESC";

	$result=mysql_query($query) or die(mysql_error());
	if($myrow=mysql_fetch_array($result)) {
		echo '<br /><b> Search results for: </b><u>', $keyword, '</u><br />';
		echo"<ol>";
		do {
			echo '<li>', $myrow["company_name"], '</li>';
   }while ($myrow=mysql_fetch_array($result));
   echo '</ol><br />';
?>
	<div align="center">
		<form method="post" action="submit"> <!-- action="submit" is correct? -->
			<table class="columns" align="center">
	    	<tr>
	    		<td>
	    			<b>Search The Archive:</b>
	    			<input type="text" name="keyword" size="15" />
	    			<input type="submit" name="submit" value="go" />
	    		</td>
	    	</tr>
	    </table>
		</form><!-- wrong element order -->
	</div>
<?php
}
	else { // if($myrow=mysql_fetch_array($result))
		echo '<br /><b>NO Records Found for:</b><u>', $keyword, '</u><br />';
?>
	<div align ="center"><!-- isn't that the same form? -->
		<form method="post" action="search123.php">
   		<input type="hidden" name="submit" value="search" />
			<table class="columns" align="center">
				<tr>
					<td>
						<b>Search:</b>
						<input type="text" name="keyword" size="15" />
						<input type="submit" name="submit" value="go" />
					</td>
				</tr>
			</table>
		</form>
	</div>
<?php
	}
}
else {  // this is line 48
?>
	<!-- again the same form, why not print it unconditionally at the end of the script? -->
	<div align="center">
		<form method="post" action="search123.php">
			<input type="hidden" name="submit" value="search" />
			<table class="columns" align="center">
				<tr>
					<td>
						<b> Search The Archive:</b>
						<input type="text" name="keyword" size="15" />
						<input type="submit" name="submit" value="go" />
					</td>
				</tr>
			</table>
		</form>
	</div>
<?php
}
?>
</body></html>
Post Reply