Skip to content

Instantly share code, notes, and snippets.

@gsmcwhirter
Created January 20, 2010 02:35
Show Gist options
  • Save gsmcwhirter/281537 to your computer and use it in GitHub Desktop.
Save gsmcwhirter/281537 to your computer and use it in GitHub Desktop.
CREATE TABLE IF NOT EXISTS tests(
id INT unsigned not null auto_increment,
PRIMARY KEY(id),
name VARCHAR(255) not null,
description TEXT not null
);
<?php
//whatever here, connecting to database, including stuff etc
$messages = array();
$success = null;
//Are we supposed to try and save or just display?
if(strtoupper($_SERVER["REQUEST_METHOD"]) == "POST")
{
//Check to see if the right inputs were passed
if(!isset($_POST["name"]) || trim($_POST["name"]) == "")
{
$messages["name"] = "Name cannot be empty.";
}
if(!isset($_POST["description"]) || trim($_POST["description"]) == "")
{
$messages["description"] = "Description cannot be empty.";
}
if(count($messages) == 0)
{
$sql_name = mysql_real_escape_string(trim(stripslashes($_POST["name"])));
$sql_desc = mysql_real_escape_string(trim(stripslashes($_POST["description"])));
$sql = "INSERT INTO tests (name, description) VALUES ('$sql_name','$sql_desc')";
$q = mysql_query($sql);
if($n = mysql_insert_id() && $n != 0)
{
$success = true;
}
else
{
$success = false;
}
}
else
{
$success = false;
}
}
if($success === false && array_key_exists("name", $_POST))
{
$value_name = htmlentities(stripslashes($_POST["name"]));
}
else
{
$value_name = "";
}
if($success === false && array_key_exists("description", $_POST))
{
$value_desc = htmlentities(stripslashes($_POST["description"]));
}
else
{
$value_desc = "";
}
?>
<html>
<head><title>Test Form</title></head>
<body style="text-align: center;">
<?php
if($success === false)
{
echo "<strong style='color: #f00;'>Failed to add new test.</strong>";
}
elseif($success)
{
echo "<strong style='color: #0f0;'>Added new test successfully.</strong>";
}
?>
<br />
<form method="post">
<table style="border: none; border-collapse: collapse; margin: 0 auto; width: 500px;">
<tbody>
<tr>
<th style="text-align: right; vertical-align: top;">Name:</th>
<td style="text-align: left; vertical-align: top;">
<?php echo ($success === false && array_key_exists("name", $messages)) ? "<strong style='color: #f00;'>".$messages["name"]."</strong><br />" : "" ?>
<input type="text" name="name" id="name_field" value="<?php echo $value_name ?>" />
</td>
</tr>
<tr>
<th style="text-align: right; vertical-align: top;">Description:</th>
<td style="text-align: left; vertical-align: top;">
<?php echo ($success === false && array_key_exists("description", $messages)) ? "<strong style='color: #f00;'>".$messages["description"]."</strong><br />" : "" ?>
<textarea style="width: 100%;" name="description" id="description_field"><?php echo $value_desc ?></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center;"><button type="submit">Add Test</button></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment