|
||||
| Register--Login--Top 20 Posters--Search Topics |
Forum Main>>Tutorials>>Entering and displaying form data | ||
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 Wed Oct 01, 2008 04:31:27 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:
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:
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
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:
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 | |||||
| Stubborn Rank:acorn Group: members Posts: 22 IP Logged PM ID and RPS ID: 1653 [PM Stubborn] RPS score: -1 RPS challenge | Posted at Wed May 24, 2006 21:39:55 Edit post|Quote I appreciate your great effort to help us II followed ur steps and created all the files needed and the database tables as well, but i faced a couple of problems that I tried to solve but I couldn't please help me if you can ![]() when I want to view the display.php, the page where the comments should appear .. I get a message which says "Parse error: syntax error, unexpected '<' in c:\wamp\www\MyGB\display.php on line 12" and the other problem is a message that appears in action.php and it says "Parse error: syntax error, unexpected T_STRING in c:\wamp\www\MyGB\action.php on line 19" and that's all, looking forward to hearing from you ![]() Best Regards ----------------------------- .:: Don't judge a book by its cover ::. | |||||
| Stubborn Rank:acorn Group: members Posts: 22 IP Logged PM ID and RPS ID: 1653 [PM Stubborn] RPS score: -1 RPS challenge | Posted at Thu May 25, 2006 20:05:25 Edit post|Quote It is ok .. I sorted it out myself .. ![]() ----------------------------- .:: Don't judge a book by its cover ::. | |||||
| keith678 Rank:acorn Group: members Posts: 4 IP Logged PM ID and RPS ID: 8701 [PM keith678] RPS score: 0 RPS challenge | Posted at Wed Oct 04, 2006 17:02:42 Edit post|Quote i got the same problem. "Parse error: syntax error, unexpected T_STRING in c:\wamp\www\MyGB\action.php on line 19" but i dont know how to fix it? - thanks | |||||
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 | |||||
| keith678 Rank:acorn Group: members Posts: 4 IP Logged PM ID and RPS ID: 8701 [PM keith678] RPS score: 0 RPS challenge | Posted at Wed Oct 04, 2006 17:32:40 Edit post|Quote i just put a double quote at the end of (die"), and now this is the problem i get: Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /var/www/html/action.php on line 18 - thank you | |||||
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 | |||||
| keith678 Rank:acorn Group: members Posts: 4 IP Logged PM ID and RPS ID: 8701 [PM keith678] RPS score: 0 RPS challenge | Posted at Wed Oct 04, 2006 19:18:29 Edit post|Quote Thank you for the help so far. i think that was able to fix it, but yet i am still running into problems. now these are the error's that it says: Warning: main(connect.php): failed to open stream: No such file or directory in /var/www/html/action.php on line 2 Warning: main(): Failed opening 'connect.php' for inclusion (include_path='.:/php/includes:/usr/share/php') in /var/www/html/action.php on line 2 Warning: mysql_query(): Access denied for user: 'kmr678@localhost' (Using password: NO) in /var/www/html/action.php on line 18 Warning: mysql_query(): A link to the server could not be established in /var/www/html/action.php on line 18 Could not insert comment hers the link http://www.ride-li.com/form.php I belive it has to do with the SQL Table. I know you have directions, but i am lost on how i should set it up? - thanks again | |||||
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 | |||||
| keith678 Rank:acorn Group: members Posts: 4 IP Logged PM ID and RPS ID: 8701 [PM keith678] RPS score: 0 RPS challenge | Posted at Wed Oct 04, 2006 19:59:44 Edit post|Quote I did create a "connect.php" I have just started to learn PHP and MySQL just the other day, so i am still very new at this. So i just need to know what i have to do in order to make this work? as far as setting up a php.mysql and making a link between them? - thanks for your time | |||||
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 | |||||
| Bigjoe11a Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 11425 [PM Bigjoe11a] RPS score: 0 RPS challenge | Posted at Sat Nov 25, 2006 12:02:45 Edit post|Quote
I tried your samples. and I keep getting a Fatal Error: call to undefinded function mysql_connect() in c:\path\test.php in line 2 can you please tell me how to fix this Thanks | |||||
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 | |||||
| Bigjoe11a Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 11425 [PM Bigjoe11a] RPS score: 0 RPS challenge | Posted at Sat Nov 25, 2006 18:30:39 Edit post|Quote
I fix that. It turned out to be that I didn't have mysql enabled in php. thats what the problem was. any way your script is working just not all of if. When I run the html page and click submit. it says no database selected. how can I fix that. | |||||
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 | |||||
| Loska Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 13377 [PM Loska] RPS score: 0 RPS challenge | Posted at Tue Aug 14, 2007 13:08:26 Edit post|Quote I got a guestbook like this on http://hihkutus.freehostia.com/.How could I get some space after each comment? And maybe some table of box after each one? Or how about some image after every comment?Thanks! | |||||
| monstrevelu Rank:acorn Group: members Posts: 2 IP Logged PM ID and RPS ID: 13606 [PM monstrevelu] RPS score: 0 RPS challenge | Posted at Wed Sep 19, 2007 13:11:20 Edit post|Quote Hi...please help...I am a newbie... I have installed the Guestbook script on my website http://www.thislifenextlife.co.uk and when I click on the 'submit' button on the guestbook, I can view the message "Comment added. Click here to see all comments". However, when I click on the this link, it takes me to a blank web page. Do I need to make any changes to the 'display.php' file or is there something else I need to do? I can see the entries in the SQL database on my web host admin panel, I just cannot get the results displayed. Many thanks.
| |||||
| genie19 Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 15614 [PM genie19] RPS score: 0 RPS challenge | Posted at Sun Mar 30, 2008 21:34:05 Edit post|Quote hi i am just starting to learn php and mysql co i can make my current webpage better http://www.bouncybeatsonline.co.uk (live internet radio station) i am practising tutorials like this one. i am also getting the error message Parse error: syntax error, unexpected '<' in www/zxq.net/b/o/u/bouncyshouts/htdocs/display.php on line 19 i have read your comments but i do not know what double quotes are or how to fix and wud be grateful for your help the submit for is working fine but i am unable to see the results displayed i also have a blank display.php page. thanks here is the code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body><?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> } ?> </body> </html> | |||||
| WOLF123 Rank:acorn Group: members Posts: 3 IP Logged PM ID and RPS ID: 16090 [PM WOLF123] RPS score: 0 RPS challenge | Posted at Tue May 13, 2008 15:33:26 Edit post|Quote Im using the CmsCore script, And followed your instructions here to use this form as a way for people to comment on Articles. Online Problem tho is the Comments show up on all Articles.Like USER 1 comments on Article 1 it also shows up on Article 2.How can I make this work where it doesnt do that.I need it to work for each Article seperate. Is this possible and If so Could you explain it to me.Thanks | |||||
| inzhimomi Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 16412 [PM inzhimomi] RPS score: 0 RPS challenge | Posted at Mon Jun 16, 2008 12:06:39 Edit post|Quote Click here to see all comments."; } } ?>I get the above error... after I click the submit button. Lately i have been able to retrive data but unable to update the database from a web page using php + mysql...i can update it in the command prompt or xampp page.. I guess the case is more or less the same here ... please suggest ----------------------------- inzhimomi | |||||
| Blinky981 Rank:acorn Group: members Posts: 1 IP Logged PM ID and RPS ID: 16614 [PM Blinky981] RPS score: 0 RPS challenge | Posted at Sun Jul 06, 2008 20:35:20 Edit post|Quote
Hi, I had the same problem. I'm relatively new at PHP and MySQL, although I have a somewhat extensive knowledge of HTML so with that I was able to figure out what was causing it quite quickly. In "display.php", the code at the end is as follows:
You need to change the lone <hr> tag to print "<hr>"; like this:
Hope this helps xD | |||||