rjkdonaldson
10-30-2009, 02:12 AM
How do i perform a simple check using php on the backend to check whether or not field is empty? Here is my sample code
<?php // Script 12.5 - add_entry.php
// This script adds an entry to the database.
// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['submit'])) { // Handle the form.
// Connect and select.
if ($dbc = @mysql_connect ('localhost', '', '')) {
if (!@mysql_select_db ('msgboard')) {
die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
}
} else {
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}
// Define the query.
$query = "INSERT INTO entry (name, message, date) VALUES ('{$_POST['from']}', '{$_POST['message']}', NOW())";
// Execute the query.
if (@mysql_query ($query)) {
print '<p>The message has been added.</p>';
} else {
print "<p>Could not add the message because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}
} else { // If no session value is present, redirect the user.
ob_end_clean(); // Delete the buffer.
// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/message_board.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}
mysql_close();
ob_end_flush();
}
// Display the form.
?>
<?php // Script 12.5 - add_entry.php
// This script adds an entry to the database.
// Address error handing.
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
if (isset ($_POST['submit'])) { // Handle the form.
// Connect and select.
if ($dbc = @mysql_connect ('localhost', '', '')) {
if (!@mysql_select_db ('msgboard')) {
die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
}
} else {
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}
// Define the query.
$query = "INSERT INTO entry (name, message, date) VALUES ('{$_POST['from']}', '{$_POST['message']}', NOW())";
// Execute the query.
if (@mysql_query ($query)) {
print '<p>The message has been added.</p>';
} else {
print "<p>Could not add the message because: <b>" . mysql_error() . "</b>. The query was $query.</p>";
}
} else { // If no session value is present, redirect the user.
ob_end_clean(); // Delete the buffer.
// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
$url .= '/message_board.php'; // Add the page.
header("Location: $url");
exit(); // Quit the script.
}
mysql_close();
ob_end_flush();
}
// Display the form.
?>