Adding rows to a mySQL database
Jeff Andersen (PHP / Moderate)This article will walk you through adding rows to a mySQL database for further usage, such as displaying in a page on your website. There are a few things you might need to know before continuing, such as how to make a basic HTML form (can be read in the HTML section of Siteguts.com).
In this example, we are going to create a database for saving articles in. For the sake of saving time, the following is the SQL query we used, use it to create your table.
CREATE TABLE `articles` ( `id` int(11) NOT NULL auto_increment,
`title` text NOT NULL,
`author` text NOT NULL,
`article` longtext NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;
This query creates a table named articles, with columns for the ID, article title, author and the article itself. Once you have created your table (explained further in Creating a mySQL database & table) we shall move onto setting the HTML form (explained further in How to make an HTML form) and the form processing code (to follow). This is the fun part, create a new file called add.php. We will be putting the form and form processing code within this.
`title` text NOT NULL,
`author` text NOT NULL,
`article` longtext NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;
We start out by opening our PHP document: <? This allows us to start writing PHP code and assures that it will execute when the file is called upon.
Now on a new line we are going to write an IF statement. This IF statement will tell the server to procede with the code providing a variable is executed or correct. Here is the code broke down into parts:
if($_POST['submit']) {
Which says if the form is posted by hitting submit it will continue with the code. This is where we now connect to our mySQL database
mysql_connect("localhost","username","password");
We now select a database we will be editing, in our case it is generically called database As you see we have example_ before the database name, this is because when/if you create a database through CPanel it will put your site's username and then _databasename, Strictly for this article we are using the username example.
mysql_select_db("example_database");
Now we have to tell the server that the information posted (or posts) should be put into variables for easier manipulation. Here is how this is done:
$title = $_POST['title'];
$author = $_POST['author'];
$article = $_POST['article'];
The part in single quotes is the name of the field you'd like to set as a variable. As you can see, in our form (below) we have named or fields title, author and article (ex name="title")
$author = $_POST['author'];
$article = $_POST['article'];
This next step is to tell the server that we need to take the variables and insert them into the table (the one we created earlier named articles). This is done via the following:
$result=MYSQL_QUERY("INSERT INTO news (id,title,author,article)".
"VALUES ('NULL', '$title', '$author', '$article')");
With id,title,author,article being the field names we used separated by commasand then the variables listed in single quotes, separated by commas.
"VALUES ('NULL', '$title', '$author', '$article')");
('$title', '$message', '$who', '$date', '$time')
Now we have the server show a finishing statement that shows up telling you it has been completed:
echo 'We have submitted your article';
Now we have to set an ELSE statement telling the server that if the IF statement comes back false, the following happens. So we close the IF statement and open the ELSE statement as shown below:
}
else
{
Now we close the usage of PHP so that we can enter HTML elements (the form to accept the data).
else
{
?>
For the form we will need two text boxes and a text area. This is where the form you may have created in HTML form for adding to a database will come in. If you did not complete that form, to save time this is the form we will be using:
<form action="add.php" method="post">
<input name="title" type="text" size="30">
<input name="author" type="text" size="30">
<textarea name="article" rows="15" cols="100">>/textarea>
<input name="submit" type="submit" value="Submit">
</form>
We set the action to Add.php because we want the form to point there and execute the code within this file and the method is post because we want the form to post the data to the server for further usage (such as what we did by applying the text into variables).
All that is left now is for us to close the else statement. For this we need to re-initialize the usage of PHP and then close the else statement as shown:
<input name="title" type="text" size="30">
<input name="author" type="text" size="30">
<textarea name="article" rows="15" cols="100">>/textarea>
<input name="submit" type="submit" value="Submit">
</form>
<?
}
?>
Now all that is left is to upload your file, and give it a whirl. If you encounter a problem you can have a look at the full document's code below to see where you might have gone wrong.
Add.php includes the following code:
}
?>
<?
if($_POST['submit'])
{
mysql_connect("localhost","username","password");
mysql_select_db("example_database");
$title = $_POST['title'];
$author = $_POST['author'];
$article = $_POST['article'];
$result=MYSQL_QUERY("INSERT INTO news (id,title,author,article)".
"VALUES ('NULL', '$title', '$author', '$article')");
echo 'We have submitted your article';
}
else
{
?>
<form action="add.php" method="post">
<input name="title" type="text" size="30">
<input name="author" type="text" size="30">
<textarea name="article" rows="15" cols="100">>/textarea>
<input name="submit" type="submit" value="Submit">
</form>
<?
}
?>
if($_POST['submit'])
{
mysql_connect("localhost","username","password");
mysql_select_db("example_database");
$title = $_POST['title'];
$author = $_POST['author'];
$article = $_POST['article'];
$result=MYSQL_QUERY("INSERT INTO news (id,title,author,article)".
"VALUES ('NULL', '$title', '$author', '$article')");
echo 'We have submitted your article';
}
else
{
?>
<form action="add.php" method="post">
<input name="title" type="text" size="30">
<input name="author" type="text" size="30">
<textarea name="article" rows="15" cols="100">>/textarea>
<input name="submit" type="submit" value="Submit">
</form>
<?
}
?>
© Copyright 2006 - All Rights Reserved - SiteGuts is a part of the Enthonia Content Network