Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Demonlord's Db Topic
demonlord
post Mar 19 2008, 01:07 AM
Post #1


Member [Level 3]
******

Group: [HOSTED]
Posts: 92
Joined: 1-January 08
Member No.: 55,554



Hey guys it's me again.
ok this time i need a way to write the following stuff to a database:
name
date
message

and 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
Go to the top of the page
 
+Quote Post
jlhaslip
post Mar 19 2008, 03:09 AM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,076
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
leiaah
post Mar 19 2008, 08:48 AM
Post #3


Super Member
*********

Group: Members
Posts: 436
Joined: 21-January 05
From: Koronadal City, Philippines
Member No.: 3,358



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)
CODE
<?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
CODE
<?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! smile.gif
Go to the top of the page
 
+Quote Post
demonlord
post Mar 19 2008, 09:47 PM
Post #4


Member [Level 3]
******

Group: [HOSTED]
Posts: 92
Joined: 1-January 08
Member No.: 55,554



what data base structer would i need to create with that leiaah?
Go to the top of the page
 
+Quote Post
leiaah
post Mar 21 2008, 07:37 AM
Post #5


Super Member
*********

Group: Members
Posts: 436
Joined: 21-January 05
From: Koronadal City, Philippines
Member No.: 3,358



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).
CODE
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;
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

 



- Lo-Fi Version Time is now: 6th October 2008 - 11:02 PM