Created
January 20, 2010 02:35
-
-
Save gsmcwhirter/281537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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