What i want is for the user to be able to enter a staff no & date
to retrieve other data from a table. the only thing is my date is structured in three columns so i dont know if i can do this. Thankyou in advance.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
$dbhost = "localhost"; // variable holding host information
$dbname = "sportscentre"; // variable holding the name of the database
$dbuser = "*****"; // variable holding the username of the person using database account
$dbpass = "****"; // variable holding password of the person using database account
$connection = mysql_connect($dbhost, $dbuser, $dbpass)or die("Cannot connect");
// The mysql_connect() function is what is used to make the connection to the database it takes three parameters as listed above the hostname, the database users username and the database users password.
$connection = mysql_select_db($dbname)or die("cannot select DB");
// The mysql_select_db() funtion is used to connect to the database that we are going to work with. The parameter for this is the database name and a message to the user in case the connection fails.
// A list of variable to hold all the information entered in the form so that
// it can then referred to in the insert query.
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$staffno = $_POST['staff_no'];
if (isset($_POST['submitted'])) { // To check if the form has been submitted.
$errors = array(); // Initialize error array.
if(empty($_POST['staff_no'])) // If the drop down box for staff no is empty, the display the message on the line below.
{
$errors[]= 'Please select a staff no';
}
if(empty($_POST['day'])) // If the drop down box for day is empty, the display the message on the line below.
{
$errors[]= 'Please select a day';
}
if(empty($_POST['month'])) // If the drop down box for month is empty, the display the message on the line below.
{
$errors[]= 'Please select a month';
}
if(empty($_POST['year'])) // If the drop down box for year is empty, the display the message on the line below.
{
$errors[]= 'Please select a year';
}
if (count($errors)==0) //If there are no errors in the error array then do the following insert query.
{
$query = "select staff_no, booking_ref_no, membership_no, class, day, month, year, time, teacher from classbooking where staff_no = '$staffno' and day = '$day' and month = '$month' and year = '$year'";
$result = mysql_query($query)or die ("Query failed: " . mysql_error());
echo "<table border = '1'>"; // This is basically echoing the table and header columns.
echo "<tr>";
echo "<th width = 25>Staff No</th><th>Booking Ref No</th><th>Membership No</th><th>Class</th><th>Date</th><th>Time</th><th>Teacher</th>";
echo "</tr>";
while ($row = mysql_fetch_array($result)) // while the function mysql_fetch_array() is fetching the result display to the page each row there is in the database.
{
echo "<tr>";
echo "<td>", $row['staff_no'], "</td><td>", $row['booking_ref_no'],"</td><td>", $row['membership_no'], $row['class'],"</td><td>", $row['day'], - $row['month'], - $row['year'], "</td><td>", $row['time'],"</td><td>", $row['teacher'],"</td>";
echo "</tr>";
}
echo "</table>";
}
else
{ // Advise of the errors.
echo '<h3>Error!</h3>
<p>The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p><p><br /></p>';
}
}
mysql_close(); // close connection
?>