Hi. I have some coursework which I have to do...I have pretty much finished it, just wondered if anyone can suggest any changes? I need to connect to a database and query it, and return the results. You will figure out what I need to do by the code: Can anyone suggest any improvements?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-stricy.dtd">
<html>
<head>
<title>Results</title>
<meta http-equiv="content-type"content="text/html;charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="?????????????????????.css" />
</head>
<body>
<form method="get" action="results.php">
<p>Search for a customer: </p>
<hr />
<p><input type="text" name="search" /></p>
<p><input type="submit" name="submit query" /></p>
<p><input type="reset" name="reset" /></p>
</form>
</body>
<?
/**
* Connect to the mysql database.
*/
$conn = mysql_connect("128.240.2.48", "demo", "netskills") or die(mysql_error());
mysql_select_db('northwind', $conn) or die(mysql_error());
// Get user input
$UserInput = $_GET['search'];
// SQL query statement
$SqlQuery = "SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE CustomerID LIKE '$UserInput%' ";
// Store query result, return error message if attempt fails and exit
$result = mysql_query ($SqlQuery) or die('Query execution problem: ' . mysql_error());
// Number of rowsreturned by SQL query
$number_rows = mysql_number_rows($result);
// Assign the file to a variable
$File_Name = fopen ("query_results.txt", "w");
// Close IP connection
mysql_close($conn);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-stricy.dtd">
<html>
<head>
<title>Output</title>
<meta http-equiv="content-type"content="text/html;charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="???????????????????????.css" />
</head>
<body>
<p class="???????????????">Results = </p>
<table>
<tr>
<th>Customer ID</th>
<th>Company Name</th>
<th>Contact Name</th>
<th>Country</th>
</tr>
<?
//Write the reults to the file
fwrite($RESULTSFILE, "Results of the query:\n");
fwrite($RESULTSFILE, "Customer ID, Company Name, Contact Name, Country\n"); //Write the column headings to the file
//while the table has rows left do the following
while($output_row = mysql_fetch_array($result))
{
?>
<tr>
<td> <?=htmlentities($output_row[CustomerID]) ?> </td> <!-- Return CompanyID -->
<td> <?=htmlentities($output_row[CompanyName]) ?> </td> <!-- Return CompanyName -->
<td> <?=htmlentities($output_row[ContactName]) ?> </td> <!-- Return ContactName -->
<td> <?=htmlentities($output_row[Country]) ?> </td> <!-- Return Country -->
</tr>
<!-- Write to file -->
<? fwrite($query_txt, "$output_row[CustomerID]\t") ?>
<? fwrite($query_txt, "$output_row[CompanyName]\t") ?>
<? fwrite($query_txt, "$output_row[ContactName]\t") ?>
<? fwrite($query_txt, "$output_row[Country]\n") ?>
<?
} //End of while statement
fclose($File_Name); //Close the file after all statements have been written
?>
</table>
<?
// Conditional HTML display, dependant on SQL query
if($number_rows < 1)
{
?>
<p><a href="php_input.html">No Rows: Return to form</a></p>
<?
}
else
{
?>
<p><a href="php_input.html">Return to form</a></p>
<p><a href="query_results.txt">Show Results</a></p>
<?
}
?>
</body>
</html>
</html>
and my html page:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-stricy.dtd">
<html>
<head>
<title>Database Output</title>
<meta http-equiv="content-type"content="text/html;charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="a4Style.css" />
</head>
<body>
<form method="get" action="results.php">
<p>Search for a customer: </p>
<hr />
<p><input type="text" name="search" /></p>
<p><input type="submit" name="submit query" /></p>
<p><input type="reset" name="reset" /></p>
</form>
</body>
</html>
I hope that this code will prduce an output text file too.
Thanks
Andy
|