Hello,
I'm new to PHP so forgive me if this question is silly to you. Basically I have created an HTML form that a user inputs a location i.e. Los Angeles. When the user clicks the submit button the input is passed to a PHP script. The script then runs a SQL query against the the database table. The script works when a location is in the database and I can see the results of the query. However, if the location is not in the database the if statement to handle that condition does not seem to work (I just get a blank page instead of the error I want).
Any help would be appreciated.
Thanks,
Bill
Here is my code:
<?
//BEGIN PHP SCRIPT
if (!$_POST[location_choice]) {
echo "<font color=red><b>You must enter a location</b></font>";
} else {
$location = "$_POST[location_choice]"; //var to hold user input
$db_name = "ip"; //create var to hold database
$table_name = "cidr_block"; //create var to hold table from database
$connection = @mysql_connect("localhost", "wdugger", "superfine") //connect to mysql
or die(mysql_error()); //if there is a problem display error
$db = @mysql_select_db($db_name, $connection) //select dtatabse
or die(mysal_error()); //if there is a problem display error
//create sql statement to select all fields in the table
$sql = "SELECT * FROM $table_name where cidr_location = '$location'";
//create var to hold result of sql query
$result = mysql_query($sql,$connection)
or die(mysql_error());
//start while loop and create array $row for each record in the $result set
while ($row = mysql_fetch_array($result)) {
$cidr_location = $row['cidr_location']; //get individuale elements of reocords and
$date_modified = $row['date_modified']; //and remove slashes. Put into these vars.
$cidr_status = $row['cidr_status'];
$cidr_oct1 = $row['cidr_oct1'];
$cidr_oct2 = $row['cidr_oct2'];
$cidr_oct3 = $row['cidr_oct3'];
$cidr_oct4 = $row['cidr_oct4'];
$cidr_bits = $row['cidr_bits'];
$cidr_notes = stripslashes($row['cidr_notes']); //end of var creation for individuale elements.
if ($cidr_location =="") {
echo "Location Not in Database";
} else {
//create var to hold formatted records
$display_block .= "<P><strong>$cidr_location</strong><br>CIDR $cidr_oct1.$cidr_oct2.$cidr_oct3.$cidr_oct4 /$cidr_bits<br> Site Notes:
$cidr_notes<br>Last modified on: $date_modified<br>Block Staus:<font color=\"red\"><b>$cidr_status</b></font></p> ";
}
} //end while loop
}
//END PHP SCRIPT
?>
<HTML>
<HEAD>
<TITLE>Locations Query</TITLE>
</HEAD>
<BODY>
<H1>CIDR Blocks: Ordered by Location</H1>
<!--echo result of PHP script-->
<? echo "$display_block";?>
<? echo "$cidr_location";?> <!--this is for troubleshooting-->
<P><a href="../html/querydatabase.html">Run another query</a></P>
</BODY>
</HTML>