In previous articles I’ve covered the creation of a MySQL database, and how to select information from it. In this article, I’ll cover insertion of user data into a table.

Insertion of data is usually done via a web form. There are many reasons we might want to do this: the obvious cause is to record input from a user, such as a comment in a blog, or an order for a product. Here, we’ll create a simple version of the former.

First, we’ll create a simple HTML5 form on a page:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post" role="form">
	<fieldset>
		<legend>Please enter your comment</legend>
		<label for="name" accesskey="n">
			Your name
		</label>
		<input type="text" size="38" maxlength="36" name="name" id="name">
		<label for="email" accesskey="e">
			Your eMail address
		</label>
		<input type="email" size="68" maxlength="66" name="email" id="email">
		<label for="comment" accesskey="c">
			Comment
		</label>
		<textarea name="comment" id="comment" rows="5" columns="66">
		</textarea>
		<input type="submit" value="Post" name="submit" id="submit">
	</fieldset>
</form>

This form submits to itself; unlike previous examples, we’ll make every action take place on this page, rather than splitting it across several pages (a form.html and formhandler.php, for example).

Note that we have provided our submit input with a name. This is one way of testing that the form has been submitted: as a named input, the submit button will be converted into a variable, one that we can test. (Obviously, the variable will not exist if the form has not been submitted).

At the top of the form page we’ll write a connection to a database, and connect to an existing comments table within it.

<?php $mysql_connection = mysql_connect("server", "user", "password") or 	
	trigger_error(mysql_error(),E_USER_ERROR);
	mysql_select_db("comments", $mysql_connection);?>

The comments table has fields that reflect those of the form (name and email as VARCHAR fields with the same character limits as the matching form fields, plus two more: an INT field named commentno that will automatically record the comment number via auto increment, and act as a primary key for the table, and a commentdate field set as TIMESTAMP that will record the moment that the comment is made, named joindate.)

We’ll test that the form has been submitted, convert the variables within it, help make them safe with mysql_real_escape_string, and insert the data:

<?php if (isset($_POST['submit'])) {
	$name = mysql_real_escape_string($_POST['name']);
	$comment = mysql_real_escape_string($_POST['comment']);
	$email = mysql_real_escape_string($_POST['email']);
	$insert = "INSERT INTO comments (name, email, comment)
VALUES ( '$name', '$email', '$comment') ";
	mysql_query($insert);
}

Note that we match the data we are going to insert to its appropriate database table field. (For simplicity’s sake I am ignoring any validation that should take place.)

We’ll show the comments recorded in the database next; because of the order in which we execute this page, it should also show the comment that has just been inserted:

<? $query = "SELECT * FROM comments ORDER BY commentate";
	$comments = mysql_query($query, $mysql_connection) or die(mysql_error());
	$comment = mysql_fetch_assoc($comments);
	do { ?>
		<p>Comment by <?=$comment['name']?> on 
		<?=$comment['date']?> </p>
		<p><?=$comment['comment']?></p>
		<?php } while ($comment = mysql_fetch_assoc($comments)); ?>

The form will go just below this, so that the user can see the most recent comments and reply to them.

Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.