This tutorial will walk you through using PHP to select entries from a database based on the text that is entered into a textbox on the site. This guide will also walk you through making the insert script to insert the comments into the database as well as help you make the database.
First we need to create our database that we will be using. The following code will set our table that we will be using up automatically. **NOTE** change DBNAME and tutorial to the database name and table name respectively. As shown, I called my table tutorial, so for the sake of following this tutorial, you may want to do the same.
- CREATE TABLE `DBNAME`.`tutorial` (
- `id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
- `name` VARCHAR( 30 ) NOT NULL ,
- `topic` VARCHAR( 30 ) NOT NULL ,
- `comment` VARCHAR( 255 ) NOT NULL ,
- PRIMARY KEY ( `id` )
- ) ENGINE = MYISAM
CREATE TABLE `DBNAME`.`tutorial` ( `id` INT( 4 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 30 ) NOT NULL , `topic` VARCHAR( 30 ) NOT NULL , `comment` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM
As you can see, this will create a table called tutorial on our database with 4 fields of data:
Id – Primary key and auto increment. (Will increase by one for every entry inserted)
Name – name of the user making a comment
Topic – A heading/title for the comment
Comment – The actual comment from the user.
Now that we have our table prepared, let’s insert a sample entry to it to make sure it is ok. Once you verify that, we are ready to start preparing our scripts to write and to read from the database. Let’s start with our insert script first. I called this file index.php.
- <?php
- $conn = mysql_connect ("localhost","dbname_username","password") or die ('cannot connect to database error: '.mysql_error());
- mysql_select_db ("databasename");
<?php
$conn = mysql_connect ("localhost","dbname_username","password") or die ('cannot connect to database error: '.mysql_error());
mysql_select_db ("databasename");
The first thing in our file will be the connection string. This connects to our database. Make sure you insert you DBname, username, and password into the appropriate places in the string. You can now save the file and launch it in a browser. If the screen comes up blank the connection stream worked. If it throws an error, it will tell you the error and you will need to troubleshoot it.
- $name = $_POST['Name'];
- $topic = $_POST['Topic'];
- $comment = $_POST['Comment'];
$name = $_POST['Name']; $topic = $_POST['Topic']; $comment = $_POST['Comment'];
Next we declare the variables that we are going to use. We will make a form in the php file later that will store these variables on submit. As you can see, we have a variable for the name, the topic, and the comment.
- if (isset($name) && isset($topic) && isset($comment))
- {
This line checks to see if all of the variables that we need have indeed been set. This allows the page to see whether the user has already inserted data, or if this is the first time the page has been loaded.
- if(($name=="") || ($topic=="") || ($comment=="")) {
- echo 'One or more field has been left blank. Please use your browser back button and correct this. Thank-you.';
- die ();
- }
if(($name=="") || ($topic=="") || ($comment=="")) {
echo 'One or more field has been left blank. Please use your browser back button and correct this. Thank-you.';
die ();
}
This section runs if the variables are set. Just because they are “set” does not mean they contain information. This snippet checks for there to be valid characters in all the variables. If there is not, it throws an error and ends the PHP so that erroneous data is not entered into the database.
- $sql = "INSERT INTO
- `DBNAME`.`TABLENAME` (`id`,`name`, `topic`, `comment`)
- VALUES (NULL,'".$name."','".$topic."','".$comment."');";
- mysql_query($sql);
- $commentno = mysql_insert_id();
$sql = "INSERT INTO `DBNAME`.`TABLENAME` (`id`,`name`, `topic`, `comment`) VALUES (NULL,'".$name."','".$topic."','".$comment."');"; mysql_query($sql); $commentno = mysql_insert_id();
This code inserts the values they entered into the fields into the database. Again, make sure your DBNAME and TABLENAME are properly inserted into those places. The last line returns the primary key (the one that is set to auto increment) so that we can display the comment number to the user.
- echo 'You comment has been successfully submitted with the following values: <br />';
- echo "<br /><B>Comment #:</B> $commentno";
- echo "<br /><B>Name:</B> $name";
- echo "<br /><B>Topic:</B> $topic";
- echo "<br /><B>Comment:</B> $comment";
- echo '<form method="post" action = "index.php">';
- echo '<Input Type="SUBMIT" value="Submit Another?"></form>';
- }
echo 'You comment has been successfully submitted with the following values: <br />'; echo "<br /><B>Comment #:</B> $commentno"; echo "<br /><B>Name:</B> $name"; echo "<br /><B>Topic:</B> $topic"; echo "<br /><B>Comment:</B> $comment"; echo '<form method="post" action = "index.php">'; echo '<Input Type="SUBMIT" value="Submit Another?"></form>'; }
This code simply displays the values back to the user in formatted text so they can see that there comment went through the way they intended. The submit button simply redirects to the same page so that you can insert another comment if you would like. When you click the button and reload the page it will clear the variables. If you just reload the page it will keep submitting the variables to the database. The trailing end bracket ends the If statement.
- else{
- echo '<h2>Please leave us your comments: (All Fields Are Required)</h2>';
- echo '<form method="post" action="index.php">';
- echo 'Your Name: <br /> <INPUT TYPE="TEXT" NAME="Name" size="35" maxlength = "30"><br /><br />';
- echo 'What is your comment about?: <br /> <INPUT TYPE="TEXT" NAME="Topic" size="35" maxlength = "30"><br /><br />';
- echo 'Comment: <br /> <TEXTAREA NAME = "Comment" rows="7" cols = "35"></textarea><br /><br />';
- echo '<INPUT TYPE="SUBMIT" VALUE="Submit Comment"> <br /><br />';
- echo '</form>';
- }
- ?>
else{
echo '<h2>Please leave us your comments: (All Fields Are Required)</h2>';
echo '<form method="post" action="index.php">';
echo 'Your Name: <br /> <INPUT TYPE="TEXT" NAME="Name" size="35" maxlength = "30"><br /><br />';
echo 'What is your comment about?: <br /> <INPUT TYPE="TEXT" NAME="Topic" size="35" maxlength = "30"><br /><br />';
echo 'Comment: <br /> <TEXTAREA NAME = "Comment" rows="7" cols = "35"></textarea><br /><br />';
echo '<INPUT TYPE="SUBMIT" VALUE="Submit Comment"> <br /><br />';
echo '</form>';
}
?>
This is the else statement that runs if the first IF statement if not true (the ISSET one). It simply shows the form to the user and tells it to reload this page on submit. Note how the names of the input boxes are the same of the variables that we declared in the first sections. Now you should be able to run you file and use it to submit a comment.
discuss this topic to forum
