Search Database Script - not working - simple mistake [56kw]

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

User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Search Database Script - not working - simple mistake [56kw]

Post by nickman013 »

Hello,

I have created a script that looks up information in a database, that is inputted on a form. If there is nothing inputted, it will show a form instead of information from the database. I cant figure out where the error is, I know its really simple but I just cant figure it out, or see it.

Dont get angry becasue I know this is such a stupid mistake

snlookup.php

Code: Select all

<?
if ($_GET['ip']) { 
$username2= "muot_report";  
$password2= "pass";  
$database2= "muot_report";
$ip= $_GET['ip'];
$connection2 = mysql_connect('localhost',$username2,$password2);  
mysql_select_db($database2); 
$sql4 = "SELECT * FROM `sn_lookup` WHERE IP =".$_GET['ip']." ORDER BY `ID` DESC"; 
$result4 = mysql_query($sql4) or die(mysql_error());


?>
<style type=text/css>
	table tr.odd{
	background-color:gray;}
	table tr.odd:hover{
	background-color:#CCFFCC;}
	a{
	color:white
	}
	</style>
<table border=0 width=500>
	<tr><td><b>ID/IP</b></td><td><b>SN</b></td></tr>
<?
};
while($row4 = mysql_fetch_array($result4)) {
echo "<tr class=odd>"; 
echo "<td align=left width=100 valign=top><font size=2>".$row4['ID']."</font><br><font size=1>".$row4['IP']."</font></td>"; 
echo "<td align=left><font size=2>".$row4['SN']."</font></td>";
} mysql_close($connection2); 
else { 
?>
<form action=/snlookup.php method=get>
Enter the IP address - <input type=text name=ip><br><input type=submit value="Look Up">
</form>
<?
};
?>
The error I get is

Parse error: parse error, unexpected T_ELSE in /home/muot/public_html/snlookup.php on line 32
Told ya it was easy!!

thanks alot!!!
Last edited by nickman013 on Wed Jan 03, 2007 10:03 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have a closing brace before the while() that terminates the if.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please indent your code. It will help you finding the error.
Let's assume for a moment there is a function foo() that prints
<style type=text/css>
table tr.odd{
background-color:gray;}
table tr.odd:hover{
background-color:#CCFFCC;}
a{
color:white
}
</style>
<table border=0 width=500>
<tr><td><b>ID/IP</b></td><td><b>SN</b></td></tr>
and bar() printing
<form action=/snlookup.php method=get>
Enter the IP address - <input type=text name=ip><br><input type=submit value="Look Up">
</form>
then your current code might look like

Code: Select all

<?
if ($_GET['ip']) {
$username2= "muot_report"; 
$password2= "pass"; 
$database2= "muot_report";
$ip= $_GET['ip'];
$connection2 = mysql_connect('localhost',$username2,$password2); 
mysql_select_db($database2);
$sql4 = "SELECT * FROM `sn_lookup` WHERE IP =".$_GET['ip']." ORDER BY `ID` DESC";
$result4 = mysql_query($sql4) or die(mysql_error());

foo()
};
while($row4 = mysql_fetch_array($result4)) {
echo "<tr class=odd>";
echo "<td align=left width=100 valign=top><font size=2>".$row4['ID']."</font><br><font size=1>".$row4['IP']."</font></td>";
echo "<td align=left><font size=2>".$row4['SN']."</font></td>";
} mysql_close($connection2);
else {
bar();
};
?>
Now, if you indent that code thoroughly you will see there's something strange/wrong.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Feyd.. I took it out, and still got the same problem

Volka, I see what your saying, I like the idea, I will start doing it in the future, thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I didn't say to remove it. :P
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Now Im confused !!!
:roll:

LOL,

Hmmm What should I do?
I know what a closing brace is, but I dont understand what I got to do to it, if I dont got to remove it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Indent all the code as it stands now. You should see what needs to happen with it after you've done so.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks. I got that error worked out.

Now,

The script I made dont even work, as usual :D .

When I search for a IP,

I get this error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.158.89 ORDER BY `ID` DESC' at line 1
if I typed in just a simple word or letter

I get this error
Unknown column 'k' in 'where clause'
uhhh, please help me!!
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Always use single quotes around values in a SQL query.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks for helping, but I changed it to single quotes, and get the same problem..

My php script now

Code: Select all

<?
if ($_GET['ip']) { 
$username2= "muot_report";  
$password2= "pass";  
$database2= "muot_report";
$ip= $_GET['ip'];
$connection2 = mysql_connect('localhost',$username2,$password2);  
mysql_select_db($database2); 
$sql4 = 'SELECT * FROM `sn_lookup` WHERE IP ='.$_GET['ip'].' ORDER BY `ID` DESC'; 
$result4 = mysql_query($sql4) or die(mysql_error());
?>
<style type=text/css>
	table tr.odd{
	background-color:gray;}
	table tr.odd:hover{
	background-color:#CCFFCC;}
	a{
	color:white
	}
	</style>
<table border=0 width=500>
	<tr><td><b>ID/IP</b></td><td><b>SN</b></td></tr>
<?
while($row4 = mysql_fetch_array($result4)) {
echo "<tr class=odd>"; 
echo "<td align=left width=100 valign=top><font size=2>".$row4['ID']."</font><br><font size=1>".$row4['IP']."</font></td>"; 
echo "<td align=left><font size=2>".$row4['SN']."</font></td>";
};
mysql_close($connection2); 
}
else { 
?>
<form action=/snlookup.php method=get>
Enter the IP address - <input type=text name=ip><br><input type=submit value="Look Up">
</form>
<?
};
?>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

The query would need to look like this when it's passed to MySQL:

Code: Select all

SELECT * FROM `sn_lookup` WHERE `IP` = '192.168.1.1' ORDER BY `ID` DESC
You don't need quotes around table and column names, though.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Would this do?

Code: Select all

SELECT * FROM `sn_lookup` WHERE `IP` = '.$ip.' ORDER BY `ID` DESC
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No. Try

Code: Select all

$ip = mysql_real_escape_string($_GET['ip'], $connection2);
$sql4 = "SELECT * FROM `sn_lookup` WHERE IP ='$ip' ORDER BY `ID` DESC";
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks Alot!.. That worked...

Now I got one more question.

How would I echo "No SN found for that IP" if there are no results...

mysql_num_rows(); ?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

You got it
Post Reply