PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Entering and displaying form data

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Fri Jan 13, 2012 01:21:17
Edit Post|Quote
Ok, this is a tutorial on how to write a form, insert the data into a mysql database and then display the data. This can be the start of a simple guestbook.

You need 3 files, form.php for the html form, action.php to post the data to the database and display.php to display the data. You also need a connect.php file to connect to the database.

First lets create the sql table in the database.
You need:
1 table called visitordata with:

1 field called entryid of type bigint, make it the primary field of the table and make it auto-increment

1 field called name of type varchar, length 255

1 field called comment of type mediumtext

Next we have the form.php code:
Code:

<form action='action.php' method='post'>
Name: <input type='text' name='name' size='20'><br>
Comment:<br>
<textarea name='comment' rows='5' cols='40'></textarea><br>
<input type='submit' name='submit' value='submit'></form>


This is a pretty basic html form. It tells the form to act on action.php when submitted and accepts name and comments as the data passed to action.php.

Before we interact with the database, we have to make a connect.php file to connect to the database:
Code:

<?php
$db = mysql_connect("localhost", "username", "password") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("mysql_name",$db))
die("No database selected.");
if(!get_magic_quotes_gpc())
{
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string', $_POST);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string', $_POST);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>

This is pretty easy to understand, the first few lines are necessary as they tell you to connect to the mysql and select a database.

The lines after:
if(!get_magic_quotes_gpc())

Are not necessary and may not work on older mysql extensions, but they are there to parse out mysql injections.

Now we go to action.php
Code:

<?php
include "connect.php";
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$comment=$_POST['comment'];
if(strlen($name)<1)
{
print "You did not enter a name.";
}
else if(strlen($comment)<1)
{
print "You did not enter a comment.";
}
else
{
$insert="Insert into visitordata (name,comment) values('$name','$comment')";
mysql_query($insert) or die("Could not insert comment);
print "Comment added. <A href='display.php'>Click here</a> to see all comments.";
}
}
?>

This is fairly simple. Includeing connect.php includes our connector file and lets our file access the database. The next line says simply states that if it detects that a Post variable called "submit" was passed. then follow through with the rest of the file, if not, it will just display a blank page.

The next few lines after that are simply data validators. They name if the name data is smaller than 1 character(its not there), then display the message that a name was not entered. The next line after that does the same validation for the comment data.

Now the final step, display the data that was inserted into the database in display.php:

Code:

<?php
include "connect.php";
$getdata="SELECT * from visitordata order by entryID desc";
$getdata2=mysql_query($getdata) or die("Could not get data");
while($getdata3=mysql_fetch_array($getdata2))
{
$getdata3[name]=strip_tags($getdata3[name]);
$getdata3[comment]=strip_tags($getdata3[comment]);
print "Name: $getdata3[name]<br>";
print "Comment:<br>";
print "$getdata3[comment]<br>";
<hr>
}
?>


Ok, this is a basic mysql select query, which tells it to pull all the fields from the table using SELECT * and order the data by entryID descending. Since your entryID auto increments, this means it will ordered them by the comments that were submitted the latest. The while loops says that as long as there are more elements in the array, keep looping. So it will loop from the beginning to the end of the data table.

Then for each time it loops, we simply print, the name and datafields of that element of the array.
The strip_tags function parses out html injections.

Thats a simple guestbook!
-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Wed Oct 04, 2006 17:06:12
Edit post|Quote
There's a missing double quote at the end of the die statement. Just put one in.
-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Wed Oct 04, 2006 18:01:07
Edit post|Quote
Don't put it after die. There is a doouble quote at the beginning of "Could not insert comment". The double quote after comment is missing. Put that double quote there.
-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Wed Oct 04, 2006 19:36:58
Edit post|Quote
Did you actually create a connect.php?

This tutorial assumes you already have php.mysql set up and you know how to establish a basic link between PHP and MySQL.

A connect.php is generally just a mysql_connect and mysql_select_db function with your database parameters.
-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Wed Oct 04, 2006 23:19:37
Edit post|Quote
You can use the connect.php in any of my script as a generic template.
-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Sat Nov 25, 2006 13:33:01
Edit post|Quote
You can't access mysql at all if that function doesn't work. Make sure your mySQL extensions are functioning right.
-----------------------------
Chipmunk,
Supreme Administrator

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Sun Nov 26, 2006 13:19:43
Edit post|Quote
Enter the correct database name in the mysql_select_db() function.
-----------------------------
Chipmunk,
Supreme Administrator

Page: 1



Powered by Chipmunk Board

Flash games Ninja games-Web Design New York