[SOLVED] Help with HTML form to database (mysql/php)!

This is a discussion on "[SOLVED] Help with HTML form to database (mysql/php)!" within the PHP Forum section. This forum, and the thread "[SOLVED] Help with HTML form to database (mysql/php)! are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 19th, 2008, 20:37
Junior Member
Join Date: Jan 2008
Location: Reykjavik
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Help with HTML form to database (mysql/php)!

Hi, I'm having a little trouble with my first run at website where users can fill in a HTML form, then that data should be stored in a mysql database. I've read through the sticky on this Forum with no luck.
I've been trying to read tutorials but with no luck I was wondering if someone could read through my code and give me some pointers...

A) I've created a HTML for the user FORM input.
B) The FORM information is then sent to a PHP file.
C) The PHP file retrieves that informations and (should) inserts it to the database.

B) Here is the PHP code for "form_dyr.php"
PHP: Select all

<html>
<head></head>
<body>

<?php
// collect data sent from form
$form_len $_POST['form_len'];
$form_riki $_POST['form_riki'];
$form_fylking $_POST['form_fylking'];
$form_undirfylking $_POST['form_undirfylking'];
$form_flokkur $_POST['form_flokkur'];
$form_undirflokkur $_POST['form_undirflokkur'];
$form_aettbalkur $_POST['form_aettbalkur'];
$form_undiraettbalkur $_POST['form_undiraettbalkur'];
$form_aett $_POST['form_aett'];
$form_undiraett $_POST['form_undiraett'];
$form_aettkvisl $_POST['form_aettkvisl'];
$form_tegund $_POST['form_tegund'];

// connection variables
$host "localhost";
$db_name "NAME";
$db_user "USER";
$db_pass "PASS";
$db_table "Flokkun";

// connect to host and select db
mysql_connect($host$db_user$db_pass);
mysql_select_db($db_name);

// create and execute the query to insert data
$query "INSERT INTO $db_table (Len,Riki,Fylking,Undirfylking,Flokkur,Undirflokkur,Aettbalkur,Undiraettbalkur,Aett,Undiraett,Aettkvisl,Tegund) VALUES ('$form_len','$form_riki','$form_fylking','$form_undirfylking','$form_flokkur','$form_undirflokkur','$form_aettbalkur','$form_undiraettbalkur','$form_aett','$form_undiraett','$form_aettkvisl','$form_tegund')";
$result mysql_query($query);
?>

<?
echo $form_len;
echo 
$form_riki;
echo 
$form_fylking;
echo 
$form_undirfylking;
echo 
$form_flokkur;
echo 
$form_undirflokkur;
echo 
$form_aettbalkur;
echo 
$form_undiraettbalkur;
echo 
$form_aett;
echo 
$form_undiraett;
echo 
$form_aettkvisl;
echo 
$form_tegund;
?> 

</body>
</html>
The database name, table name, host, user name, password have all been double checked for spelling mistakes (copy/paste from hosting provider).


Right now, when I type in some data and hit the submit button the page jumps to the PHP page which is blank (no SQL errors appear). But in phpMyAdmin, the data has not been inserted into the table.

Any help would be great

Last edited by skuliaxe; Jan 19th, 2008 at 21:59. Reason: SOLVED
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 19th, 2008, 21:05
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,405
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
Re: Help with HTML form to database (mysql/php)!

Hmm.
PHP: Select all

query "INSERT INTO $db_table (id,Len,Riki,Fylking,Undirfylking,Flokkur,Undirflokkur,Aettbalkur,Undiraettbalkur,Aett,Undiraett,Aettkvisl,Tegund,Heiti) VALUES ('','$form_len','$form_riki','$form_fylking','$form_undirfylking','$form_flokkur','$form_undirflokkur','$form_aettbalkur','$form_undiraettbalkur','$form_aett','$form_undiraett','$form_aettkvisl','$form_tegund')";
$result mysql_query($query); 
The thing I would suggest is, you should not be inserting a value of null into the id field, I presume that is your Primary Key, and in your screen it says it cannot be null.

So try:
PHP: Select all

query "INSERT INTO $db_table (Len,Riki,Fylking,Undirfylking,Flokkur,Undirflokkur,Aettbalkur,Undiraettbalkur,Aett,Undiraett,Aettkvisl,Tegund,Heiti) VALUES ('$form_len','$form_riki','$form_fylking','$form_undirfylking','$form_flokkur','$form_undirflokkur','$form_aettbalkur','$form_undiraettbalkur','$form_aett','$form_undiraett','$form_aettkvisl','$form_tegund')";
$result mysql_query($query); 
(Bear in mind I am only capable of simple PHP - some of the more advanced people here might see something I cant.)

HTH, Jack
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 19th, 2008, 21:10
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with HTML form to database (mysql/php)!

First off, DO NOT include your DB Connection details, edit & remove them for security!

Only thing i can think of is $_GET ? instead of post, but then again im no PHP wiz.

~Dean
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 19th, 2008, 21:20
Junior Member
Join Date: Jan 2008
Location: Reykjavik
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with HTML form to database (mysql/php)!

As you can all see, I´m new to all this. So thanks for all the help.

Quote:
Originally Posted by jackfranklin
The thing I would suggest is, you should not be inserting a value of null into the id field, I presume that is your Primary Key, and in your screen it says it cannot be null....
So try:
I fixed that, thanks. The 'id' is my primary key and is set to auto increment.

Quote:
Originally Posted by Bravo81
First off, DO NOT include your DB Connection details, edit & remove them for security!

Only thing i can think of is $_GET ? instead of post, but then again im no PHP wiz.
I edited that out, thanks. I didn't mean to have that info there. But after using copy/paste a few times I got it in there somehow This is although just a test database right now.

I´ll read up on $_GET and see if that will help me. Thank you.

The PHP is echoing the right information that is entered in the Forms and there doesn't seem to be a database connection error.
So shouldn't the error lie in the Insert to table part?

PHP: Select all

// create and execute the query to insert data
$query "INSERT INTO $db_table (Len,Riki,Fylking,Undirfylking,Flokkur,Undirflokkur,Aettbalkur,Undiraettbalkur,Aett,Undiraett,Aettkvisl,Tegund,Heiti) VALUES ('$form_len','$form_riki','$form_fylking','$form_undirfylking','$form_flokkur','$form_undirflokkur','$form_aettbalkur','$form_undiraettbalkur','$form_aett','$form_undiraett','$form_aettkvisl','$form_tegund')";
$result mysql_query($query); 

Last edited by skuliaxe; Jan 19th, 2008 at 21:30.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 19th, 2008, 21:46
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with HTML form to database (mysql/php)!

I dont see why you used $db_table in the INSERT, I would of just put the DB name in text..but again, im no PHP wiz and i dont belive that would stop your information going in.

I dont know if this will work, but try:
PHP: Select all

mysql_query($query); 


instead of $result=


Thats all i could find after Googling.


Sorry im not much help,

~Dean
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 19th, 2008, 21:56
Junior Member
Join Date: Jan 2008
Location: Reykjavik
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with HTML form to database (mysql/php)!

I just solved my issue.
I had to add $link...

OLD CODE
Code: Select all
mysql_connect($host, $db_user, $db_pass);
NEW CODE
Code: Select all
$link = mysql_connect($host, $db_user, $db_pass);
Thanks for all the help

Quote:
Originally Posted by Bravo81
Sorry im not much help,
~Dean
Any advice was greatly appreciated. Thank you.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jan 19th, 2008, 22:05
Up'n'Coming Member
Join Date: Jan 2008
Location: London
Age: 18
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with HTML form to database (mysql/php)!

Congrats on solving it yourself, that was my next suggestion. Honest! lol


[SOLVED]
(Always wanted to do that)

~Dean
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jan 20th, 2008, 00:41
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Help with HTML form to database (mysql/php)!

Quote:
Originally Posted by Bravo81 View Post
Congrats on solving it yourself, that was my next suggestion. Honest! lol


[SOLVED]
(Always wanted to do that)

~Dean
Always wanted to say "solved"? Ha.
Marked as solved.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Creating multiple dynamic list boxes from mysql database davibugi PHP Forum 11 Nov 20th, 2007 06:12
[SOLVED] Linking php code to a form &amp; mysql database longstand PHP Forum 21 Oct 9th, 2007 16:40
php/mysql database fields into html list/menu csun PHP Forum 4 Jul 27th, 2007 16:04
How to connect flash form with Mysql database shenbagarajan Flash & Multimedia Forum 6 Jan 20th, 2006 19:59


All times are GMT. The time now is 01:54.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42