INSERTING INFORM ATION INTO THE DATABASE
INSERTING INFORM ATION INTO THE DATABASE
Because you have yet to put any users in the database, we’ll start by reviewing the script that will allow that. But first, we need to tell you a little bit more about PHP variables. A bit earlier, we showed that you can create variables within a PHP script, but as this is a client/server environment, you’re going to need to get variable data from the client (the Web browser) to PHP. You’ll usually do this with HTML forms.
There’s a basic rundown of HTML forms in Appendix A. Check that if you need to. For now we will just point out that every form element has a name, and when a form is submitted the names of those form elements become available as variables in the PHP script the form was submitted to. With the following form, as soon as the form is submitted, the variables $surname and $submit will become available in the PHP script myscript.php. The value of $surname will be whatever the user enters into the text field. The value of $submit will be the text string “submit.”
<form action=”myscript.php”>
Introduction xxxv
<input type=”submit” name=”submit” value=”submit”> </form>
Before we show the script itself, now is a good time to note that Web program- ming is slightly different from other types of programming in one important respect: It is stateless. To display a page, a Web server must first receive a request from a browser. The language they speak is called HTTP, the Hypertext Transfer Protocol. The request will include several things —the page the browser wishes to see, the form data, the type of browser being used, and the IP address the browser is using. Based on this information, the Web server will decide what to serve.
Once it has served this page, the server maintains no connection to the browser. It has absolutely no memory of what it served to whom. Each HTTP request is dealt with individually with no regard to what came before it. For this reason, in Web programming you need to come up with some way of maintaining state. That is, if you are progressing through an application, you will need some way of letting the server know what happened. Essentially, you will need ways of passing variables from page to page. This will come up in our applications. The applications will solve this problem in one of three ways: by passing hidden form elements, by using cookies, or by using sessions.
Now back to our script. <form action=”myscript.php”>
<input type=”text” name=”surnmae”> <input type=”submit” name=”submit” value=”submit”>
</form> You can decide what you will display on a page based on the variable informa-
tion that comes from HTML forms. For instance, you could check if the preceding form had been submitted by checking if the variable name $submit had a value of “submit.” This very technique will come into play when it comes to creating the page for inserting information into the database.
There is one page in our application, called sign.php, that has an HTML form. The action of the form in this page is create_entry.php. Here’s the page in all its glory:
<h2>Sign my Guest Book!!!</h2> <form method=post action=”create_entry.php”> <b>Name:</b>
<input type=text size=40 name=name> <br> <b>Location:</b> <input type=text size=40 name=location> <br> <input type=text size=40 name=name> <br> <b>Location:</b> <input type=text size=40 name=location> <br>
<b>Email:</b> <input type=text size=40 name=email> <br> <b>Home Page URL:</b> <input type=text size=40 name=url> <br> <b>Comments:</b> <textarea name=comments cols=40 rows=4 wrap=virtual></textarea> <br>
<input type=submit name=submit value=”Sign!”> <input type=reset name=reset value=”Start Over”>
</form> When the user fills out this form and submits it, the information will be sent to
create_entry.php. The first thing to do on this page is to check that the form has been submitted. If it has, take the values entered into the form and use them to cre- ate a query that you will send to MySQL. Don’t worry about the specifics of the query just yet. Just know that it will insert a row into the database table you cre- ated earlier.
<?php include(“dbconnect.php”);
if ($submit == “Sign!”) {
$query = “insert into guestbook
(name,location,email,url,comments) values (‘$name’, ‘$location’, ‘$email’, ‘$url’, ‘$comments’)”
; mysql_query($query) or
die (mysql_error());
?> <h2>Thanks!!</h2> <h2><a href=”view.php”>View My Guest Book!!!</a></h2> <?php } else {
include(“sign.php”); } ?>
If the form, which is in sign.php, hasn’t been submitted, it is included and there-
Introduction xxxvii
The first time the create_entry.php page is called, the form in sign.php will be dis- played. The next time, though, the data will be inserted into the database.
Figures I-2 and I-3 show the pages that this script will create.
Figure I- 2 : create_entry.php the first time through Figure I- 2 : create_entry.php the first time through