Inserting information into SQL database

Jun 4, 2005
19,723
1
0
So say I've got this form that I can input the title and body of a news post, how would I go about submitting it and having it inserted in the code at the bottom.

If I'm missing some information you need in order to assist me, just say the word.

Thanks for the help!
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
I wouldn't put all of that HTML wrapper into the database. Just put the bits that change in there and have the script that pulls it out add the html. Much smaller database, and if you want to change the presentation of the data later all you have to do is change the script, not every entry in the database.
 
Jun 4, 2005
19,723
1
0
Originally posted by: Armitage
I wouldn't put all of that HTML wrapper into the database. Just put the bits that change in there and have the script that pulls it out add the html. Much smaller database, and if you want to change the presentation of the data later all you have to do is change the script, not every entry in the database.

That sounds like a much better idea!

As I am a MySQL newb, would you be able to explain how I would do so? Or point me to some tutorials?

My google skills are lacking lately.

Thanks for the help!
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: Armitage
I wouldn't put all of that HTML wrapper into the database. Just put the bits that change in there and have the script that pulls it out add the html. Much smaller database, and if you want to change the presentation of the data later all you have to do is change the script, not every entry in the database.

Agreed. Your formatting should remain separate from your content.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: LoKe
Originally posted by: Armitage
I wouldn't put all of that HTML wrapper into the database. Just put the bits that change in there and have the script that pulls it out add the html. Much smaller database, and if you want to change the presentation of the data later all you have to do is change the script, not every entry in the database.

That sounds like a much better idea!

As I am a MySQL newb, would you be able to explain how I would do so? Or point me to some tutorials?

My google skills are lacking lately.

Thanks for the help!

Where to start? How much do you know about databases? Do you have mysql setup? Can you connect to it via a terminal? Via your programming language of choice?
 

Shuxclams

Diamond Member
Oct 10, 1999
9,286
15
81
You connect to the database using PHP.

Start with a creating a connector like this, call it db.php;
$dbType = "mysql";

$dbUser = "UNAME";

$dbPass = "PWORD";

$dbServer = "SERVERNAME";

$dbName = "DBNAME";

$db_object = DB::connect("$dbType://$dbUser:$dbPass@$dbServer/$dbName");

if(DB::isError($db__object))

{

die($db__object->getMessage());

}

$db_object->setFetchMode(DB_FETCHMODE_ASSOC);

Now at the top of each page you create that needs to connect to the db you will need to include a line like this;

<php

require('db.php')

So in your code you would have whatever input method you choose (I prefer FCKeditorFKeditor for news and content) and then the output displayed on another page.

news.php
[/quote]
require('../db.php')

$query = ("
SELECT news, date, username, story_id
FROM news
WHERE active='Y'
ORDER BY story_id DESC");

$result = $db_object->query($query);

if (DB::isError($result)) {
die($result->getMessage());
}

$num_results = $result->numRows();


echo '<h1> Latest Headlines </h1>';
for ($i=0; $i <$num_results; $i++) {
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
echo '<table cellpadding="0" cellspacing="0" border="1" style="text-align: left; width: 99%; background-color: rgb(204, 204, 204);">';
echo '<tbody>';
echo '<tr>';
echo '<td style="vertical-align: center; background-color: rgb(102, 51, 102);" rowspan="1" colspan="13"><font color="#ffffff"><b><div style="text-align: left;">';
echo '<span style="font-weight: bold;">Story posted by '.$row[username].' on '.$row[date].'</div></span></b></font>';
echo '</td>';
echo '</tr>';

echo '<tr>';
echo '<td style="vertical-align: top;"><small>';
echo $row[news];
echo '</small><br></td>';
echo '</tr>';
echo '<br>';
}
echo '</tbody>';
echo '</table>';
echo '<br>';


?>

<p align="center">
<button type="button" onclick="window.location='http://www.mydomain.com/logout.php'">Logout</button><button
type="button"
onclick="window.location='http://www.mydomain.com/home.php'">Back</button></p>
</body>
</html>[/quote]

There are many howto's on this out there. Also I recommend a couple of books;

PHP and MySQl Web Development Second Edition / Welling Thompson
MySQL/PHP Database Applications Greenspan & Bulger


SHUX
 
Jun 4, 2005
19,723
1
0
Wow, thanks! Lots of stuff for me to read now, I'd like to seriously get into PHP and MySQL. I know the basics of MySQL, how to create/manage databases and whatnot, but nothing past beginner.

Thanks again!