Assuming a database structure as per this SQL:
SQL
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`name` varchar(20) collate latin1_general_ci NOT NULL,
`date` datetime NOT NULL,
`message` varchar(100) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
*edit* changed the datatype of the date to better reflect the needs of the program. It now saves date and timestamp.
I assume that you will be able to adjust the Database (or this code) to suit.
Here is a page that will request the user's name and their message, then INSERT the information into the 'users' table of the 'test' database.
You will need to change the Database information to suit your particular set-up.
CODE
<?php
// Send NOTHING to the Web browser prior to the header() line!
// Check if the form has been submitted.
if (isset($_POST['submitted'])) {
// require_once ('mysql_connect_test.php'); // Connect to the db.
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
error_reporting (E_ALL); // set error reporting to all
// Make the connection.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
// Select the database.
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
// Create a function for escaping the data.
function escape_data ($data) {
// Address Magic Quotes.
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
// Check for mysql_real_escape_string() support.
if (function_exists('mysql_real_escape_string')) {
global $dbc; // Need the connection.
$data = mysql_real_escape_string (trim($data), $dbc);
} else {
$data = mysql_escape_string (trim($data));
}
// Return the escaped value.
return $data;
} // End of function.
$errors = array(); // Initialize error array.
// Check for a first name.
if (empty($_POST['name'])) {
$errors[] = 'You forgot to enter your name.';
} else {
$n = escape_data($_POST['name']);
}
// Check for a message.
if (empty($_POST['message'])) {
$errors[] = 'You forgot to enter your message.';
} else {
$m = escape_data($_POST['message']);
}
if (empty($errors)) { // If everything's OK.
// Register the user in the database.
// Check for previous registration.
// Make the query.
$query = "INSERT INTO users (
name,
date,
message
)
VALUES (
'$n',
NOW(),
'$m'
)";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.
// Send an email, if desired.
// Redirect the user to the thanks.php page.
// 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.
}
// Add the page.
$url .= '/thanks.php';
header("Location: $url");
exit();
} else { // If it did not run OK.
$errors[] = 'You could not be registered due to a system error. We apologize for any inconvenience.'; // Public message.
$errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.
}
} // End of if (empty($errors)) IF.
mysql_close(); // Close the database connection.
}
// End of the main Submit conditional.
// Begin the page now.
$page_title = 'Register';
// include ('./includes/header.html');
if (!empty($errors)) { // Print any error messages.
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
// Create the form.
?>
<h1>Input Area</h1>
<p>This is where you'll put the main page content. This content will differ for each page. This is where you'll put the main page content. This content will differ for each page. This is where you'll put the main page content. </p>
<form action="insert.php" method="post">
<p>Name: <input type="text" name="name" size="15" maxlength="15" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
<p>Message: <input type="text" name="message" size="15" maxlength="30" value="<?php if (isset($_POST['message'])) echo $_POST['message']; ?>" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
// include ('./includes/footer.html');
?>
You will need a thanks.php page to go to if there are no errors during the INSERT.
I'll leave that file up to you to write.
Or it could go back to the Index.php, even. Adjust that to suit your situation.
Hope this helps.
Reply