Jump to content
xisto Community
demonlord

Demonlord's Db Topic

Recommended Posts

Hey guys it's me again.ok this time i need a way to write the following stuff to a database:namedatemessageand then i will need to be able to dispay the content of that database in a file.if some one knows how to do that that would be great.Thanks

Share this post


Link to post
Share on other sites

Assuming a database structure as per this 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.
<?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 = '/' . $_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.

Share this post


Link to post
Share on other sites

I made templates so it would be easier for me to create PHP pages with database connectivity

 

To insert data into a table (assuming you've already created a database with a table called table1)

<?php	$name=$_POST['name'];	  $msg=$_POST['msg'];  	  $date = date('Y-m-d , h:i:s',time()); 	  $name = addslashes($name);	  $msg = addslashes($msg);	  @ $db = mysql_pconnect('localhost', 'username', 'password');	if (!$db)	  {	 		echo 'Error: Could not connect to database.  Please try again later.';	 		exit;	  }	  mysql_select_db('databasename');  	  $query = "insert into table1(name,date,message) values ('".$name."', '".$date."','".$msg."')"; 	  $result = mysql_query($query);	  	if ($result)			  echo  'Entry has been Posted.'; ?>

To view data from a database table

<?php	@ $db = mysql_pconnect('localhost', 'username', 'password');	if (!$db)	  {	 		echo 'Error: Could not connect to database.  Please try again later.';	 		exit;	  }	  mysql_select_db('databasename');  	  $query=mysql_query("SELECT name,date,message FROM table1");	while($row=mysql_fetch_row($query)){		echo "Name: ". $row[0]."<br>";		echo "Date: ". $row[1]."<br>";		echo "Message: ". $row[2]."<br>";	}?>

Hope this helped! <_<

Share this post


Link to post
Share on other sites

You can use the SQL statement for creating a table. Jlhaslip posted the code below. You simply need to change the tablename to the name you want (users to table1).

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;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.