A little project for anyone with time on their hands? 😉 I want to add server side validation to this form but don't know where to start - or finish :bemused:
There are just two input fields in this form. device and x10_channel fields from my devices table. Both are NOT NULL required fields. Both are alphanumeric data.
x10_channel MUST be 3 characters long and preferably with an input mask of A00 i.e. a letter A-Z followed by a number 01 - 99.
The device must be between 1 and 6 characters long.
The script minus validation is below.
If server side validation is too tricky - perhaps client side validation would be okay but then not everyone has Javascript enabled.... I thought PHP would be better.
<html><head><title>Add a Device</title></head>
<body>
<?php
session_start();
if (!isset($_SESSION['user'])
|| $_SESSION['user'] !== true)
{
header('Location: login.php');
exit;
}
$query = "LOCK TABLES Class WRITE, Student WRITE";
mysql_query($query);
$self = $_SERVER['PHP_SELF'];
$x10_channel = $_POST['x10_channel'];
$device = $_POST['device'];
if( (!$x10_channel) or (!$device) )
{
$form ="<table width=\"247\" border=\"1\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\" bordercolor=\"#000000\">";
$form.="<tr bgcolor=\"#CCCCCC\"><td colspan=\"2\"><font size=\"1\" face=\"Verdana\"><center>";
$form.="Please enter details of new Device";
$form.="</td></font>";
$form.="</tr><tr>";
$form.="<td><font size=\"2\" face=\"Verdana\"><form action=\"$self\"";
$form.=" method=\"post\">Device:</td>";
$form.="<td><input type=\"text\" name=\"device\"";
$form.=" value=\"$device\"></td></tr><tr><td><font size=\"2\" face=\"Verdana\">X10 Channel:</td>";
$form.="<td><input type=\"text\" name=\"x10_channel\"";
$form.=" value=\"$x10_channel\">";
$form.="<tr><td colspan=\"2\"><center><input type=\"submit\" value=\"Add Device\">";
$form.="</tr></table></form>";
echo($form);
}
else
{
$conn = @mysql_connect("localhost","alexm","rugby")
or die("Could not connect to MySQL");
$db = @mysql_select_db("rugby_project",$conn)
or die("Could not select Database");
$sql = "insert into devices (x10_channel,device)
values (\"$x10_channel\",\"$device\" )";
$result = @mysql_query($sql,$conn)
or die("Could not execute query - username or phone number already exists");
if($result)
{
echo( "<table width=\"30%\" border=\"1\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bordercolor=\"#000000\">
<tr bgcolor=\"#CCCCCC\">
<td colspan=\"2\"><font size=\"1\" face=\"Verdana\">
<center>Your $device has now been added to the Database</center></font></td></tr></table><p><font size=\"1\" face=\"Verdana\"><center><a href=\"addcommand.php\">Add More</a><p>" );
}
}
$query = "UNLOCK TABLES";
mysql_query($query);
?>
<p>
<font size="1" face="Verdana"><center>
<a href="javascript:history.go(-1)">Return to Control Panel</a> |
<a href="logout.php">Log out</a>
</body></html>