The Cutting Edge Tech Blog


Creating a basic blog using php and mysql Part 3 by FSherratt

Creating a basic blog using php and mysql Part 3

View part 1 here

View part 2 here

Creating the create.php page

In the last tutorial we created a dynamic page to display our blog entries but we couldn’t test to see if it was working or not as we didn’t have any entries, so in this part we’re going to build a form so we can submit our entries.

So as usual create a blank php document but again don’t delete anything. then insert the php code below at the top of the document.

<?php

Again you will need to connect to the database so the include_once function will be used again.

include_once”scripts/connect.php”;

You will now need another if function. This time it will be to detect when the form has been submitted. Parse_var will be a hidden field and new will be the value of this. You can use the system to allow multiple forms to be placed on one page.

if ($_POST['parse_var'] == “new”){

The next three lines of code collected the data from the form and turn them into PHP values that can be added to our database.

$title = $_POST['title'];
$contents = $_POST['contents'];
$author = $_POST['author'];

This bit places the PHP variables into the correct columns in your database. The now() function generates the date it is at the moment.

$sqlcreate = mysql_query(“INSERT INTO entries (date, title, contents, author)
VALUES(now(),’$title’,'$contents’,'$author’)”);

The next section of code produces a message to let you know if the data has been successfully submitted or not. This can be edited with any php you want you just need to start it with ‘ and end it with ‘;

if ($sqlcreate){
$msg = ‘<font color=”#009900″>A new article has been created.</font>’;
} else {
$msg = ‘<font color=”#FF0000″>Problems connecting to server, please try again later.</font>’;
}
}
?>

There are only a few more bits of PHP now. Once again we need to include the header at the top of the page just incase you forget what your doing and add the bit of php code which will display any messages.

<?php include_once”header.php”; ?>
<?php print”$msg”;?>

Were done with php now so lets move onto creating our form.

So the first bit of code we need is the opening html form tag. The form will submit to the same page so the action is to submit to create.php. were also posting the data directly into the database so we use the post function.

<form action=”create.php” method=”post”>

Next we’ll create 3 input fields so we can create our database we only need 3 even though there are 4 fields being displayed on our article page as if you remember earlier we automatically generated the date. You also need to remember to enter the name values correctly so they correspond with the PHP we write earlier.

<input type=”text” value=”Title”/>
<br />
<br />
<textarea rows=”8″>Contents</textarea>
<br />
<input type=”text” value=”Author”/>
<br />
<br />

The next line is for the hidden value that the if function is looking for.

<input type=”hidden” value=”new” /></pre>

Next we need to add a submit button as we the form would be pretty rubbish without one. So paste the code below into your document.

<input name=”button3″ value=”Submit” />
</form>

We’ve now finished the form page. All you have top do is upload the files you’ve created into you online hosting space and test it, all being well it should work straight off.

Conclusion

And finally the conclusion of what we’ve learnt in this section of the tutorial.

–       We’ve turned html form values into PHP

–       We’ve created a hidden value and then got PHP to tell us whether we’ve done everything right (using a similar method you can also get it to validate fields, this may come in a later tutorial)

–       We’ve created a HTML form

–       We’ve made a submit button that works!!!

Thank you for following this tutorial,

I will periodically release updates and more tutorials to help you improve your blog further.

Advertisements

4 Comments so far
Leave a comment

It is easy to understand tutorial.Thanks.

Comment by Purva Yadav

Looks good

Comment by Heloic

its good tutorial for the fresher!!!

Comment by Bhushan

[…] Creating a basic blog using php and mysql Part 3 […]

Pingback by Creating a basic blog using php and mysql Part 2 « The Cutting Edge Tech Blog




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: