Page 2 of 2

Posted: Wed Jan 03, 2007 9:37 pm
by nickman013
Once again,

I attempted, and failed..

The code

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); 
$ip = mysql_real_escape_string($_GET['ip'], $connection2); 
$sql4 = "SELECT * FROM `sn_lookup` WHERE IP ='$ip' ORDER BY `ID` DESC";
$result4 = mysql_query($sql4) or die(mysql_error());
$num_rows = mysql_num_rows($result4); 
if ($num_rows = 0){echo "No SN found for that IP";}
?>

<style type=text/css>
	table tr.odd{
	background-color:gray;}
	table tr.odd:hover{
	background-color:#CCFFCC;}
	a{
	color:white
	}
	</style>
<?
if ($num_rows > 0){echo '
<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>
<?
};
?>
Neither of the IF statements for num_rows work, how can I make them.

I really appreciate you guys telling me what to do.

Thanks!

Posted: Wed Jan 03, 2007 9:41 pm
by volka
Code's still not indented.

Posted: Wed Jan 03, 2007 9:44 pm
by nickman013
How do I indent? With tab, if so, how many times do I indent?

My program doesnt do it automatically. I am using Taco for mac.

Is there a better script editor for mac?

Thanks!

Posted: Wed Jan 03, 2007 9:53 pm
by aaronhall
With tab. Indent inside bracketed blocks (like 'if' and 'while' blocks).

There's a problem with your if statement: http://us2.php.net/operators.comparison

Posted: Wed Jan 03, 2007 9:54 pm
by volka
I e.g. use one tab per level.

Please invest more time to understand your code - what it does, what's necessary and what is not.
e.g.
$ip= $_GET['ip'];
$connection2 = mysql_connect('localhost',$username2,$password2);
mysql_select_db($database2);
$ip = mysql_real_escape_string($_GET['ip'], $connection2);
first assigning $ip a value, then connecting to the database (without using the variable $ip) and then assigning another value to $ip => there's no need for $ip= $_GET['ip']; and that increases the "developer does not know what he/she is doing"-probability-counter for someone reviewing the code ;)

Code: Select all

<?php
if ( isset($_GET['ip']) ) {
	$username2= "muot_report"; 
	$password2= "report"; 
	$database2= "muot_report";

	$connection2 = mysql_connect('localhost',$username2,$password2) or die(mysql_error()); 
	mysql_select_db($database2, $connection2) or die(mysql_error()); 

	$ip = mysql_real_escape_string($_GET['ip'], $connection2);
	$sql = "SELECT * FROM `sn_lookup` WHERE IP ='$ip' ORDER BY `ID` DESC";
	$result = mysql_query($sql) or die(mysql_error());
	
	$num_rows = mysql_num_rows($result);
	if ( 0==$num_rows ) {
		echo "No SN found for that IP";
	}
	else {
?>
		<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>
				<th>ID/IP</th>
				<th>SN</td>
			</tr>
<?php
		while($row4 = mysql_fetch_array($result4)) {
?>
			<tr class="odd">
				<td align="left" width="100" valign="top">
					<?php echo $row4['ID']; ?>
					<br />
					<?php echo $row4['IP']; ?>
				</td>
				<td align="left">
					<?php echo $row4['SN']; ?>
				</td>
			</tr>
<?php
		}
	}
}
?>
<form action="/snlookup.php" method="get">
	Enter the IP address - <input type=text name=ip>
	<br />
	<input type="submit" value="Look Up">
</form>

Posted: Wed Jan 03, 2007 10:02 pm
by nickman013
Thanks for helping me, and writing/fixing up the code for me.

Right now only some parts work..

If a SN is not found for the IP

it displays the message correctly..

If I type in a IP that I know is in the DB, it outputs this

Image

any idea, i am using the code above.

Thanks ALOT!!!

Posted: Wed Jan 03, 2007 10:06 pm
by aaronhall
$result4 isn't being set.

Posted: Wed Jan 03, 2007 10:10 pm
by nickman013
Thanks It works just how I want it to..


THANKS ALOT!!!

Posted: Wed Jan 03, 2007 10:11 pm
by aaronhall
We're happy to help. Hope you learned something from it all.