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
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 8:19 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jan 03, 2007 8:32 pm
you have a closing brace before the while() that terminates the if.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Jan 03, 2007 8:34 pm
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.
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 8:45 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jan 03, 2007 8:47 pm
I didn't say to remove it.
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 8:50 pm
Now Im confused !!!
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jan 03, 2007 8:52 pm
Indent all the code as it stands now. You should see what needs to happen with it after you've done so.
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 9:05 pm
Thanks. I got that error worked out.
Now,
The script I made dont even work, as usual
.
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!!
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Wed Jan 03, 2007 9:08 pm
Always use single quotes around values in a SQL query.
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 9:11 pm
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>
<?
};
?>
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Wed Jan 03, 2007 9:14 pm
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.
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 9:18 pm
Would this do?
Code: Select all
SELECT * FROM `sn_lookup` WHERE `IP` = '.$ip.' ORDER BY `ID` DESC
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Jan 03, 2007 9:19 pm
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";
nickman013
Forum Regular
Posts: 764 Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York
Post
by nickman013 » Wed Jan 03, 2007 9:24 pm
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(); ?
aaronhall
DevNet Resident
Posts: 1040 Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:
Post
by aaronhall » Wed Jan 03, 2007 9:26 pm
You got it