Obviously, before you can fetch data you must add it first. To do this, we're going to use a normal HTML form. So, open your HTML editor and create a page like below. Name it whatever you want.
It looks a little scrappy, but you can style it and such later. The next step is a little trickier -- inserting information into your database. Seeing as it will be the first part of PHP coding for the whole script, we'll do it in steps, starting with the connection to the MySQL database:
The first line starts the PHP; without this the code will not work. Next comes the connection, with the server or host, username, and password. The connection is set to the variable $connect for future reference.
For the to connection to work for you, you will have to change "user" and "pass" to your MySQL username and password. On most occasions, "localhost" stays the same.
The next step is selecting your database. This is just one line of code:
Replace "databasename" with your MySQL database name, which is usually given to you by your host. Just so that you know what's going on, MySQL is told to select the database, using the connection that we set earlier (hence the $connect variable being there).
The next part is the actual submitting to the database:
if($add == "SUBMIT"){ $sql = "INSERT INTO content SET Name='$xname', Author='$xauthor', Body='$xbody', Description='$xdescription'";
You'll have to think back a bit for this. When we created the form we gave the text fields weird names. Like instead of calling one "description", we called it "xdescription". I did this for the sole reason that it would avoid confusion. Anyway, back to the point, the code is what will allow you to submit everything submitted into your directory. The IF statement that starts the code out exists so that the code is only executed when a form has been submitted.
Next comes the actual submission and error handling incase something went awry.
if(mysql_query($sql))
{
echo "The information has been added to the database.";
}
else
{
echo "There was an error submitting the information.";
}
} // closing bracket for original IF statement ?>
So if all was successful with the $sql variable, you get a message in the form of "The data has been added to the database". Otherwise (or else as PHP says) you get "There was an error submitting the information".
After both statements are closed you see ?>. This tells your server to stop processing for PHP at this point.
So there you have it, the complete code for inserting data into your database.
Something to remember is that the ID automatically gets assigned when you submit
anything into the database. This is because we set auto_increment to it.