22, November 2008

Writing to database via a web form - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Writing to database via a web form
« previous next »
Pages: [1] 2 Print

Author Topic: Writing to database via a web form  (Read 1568 times)
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« on: Aug 29, 2006, 02:14:21 PM »

Hi Guys

I am a newbie so...but I can not find a decent tutorial on how to use a form to submit data to a MySQL table using PHP5. I tried several ways and got either duplicate records or records when the page was loaded or no data at all. Does the PHP go before or after the form data...I am using Olaf's access class as a basis and have connection to the DB but for some reason I can not get it to work.

For each user that has access to the webpages, do they need to have an account setup within MySQL? I know there are alot of questions here and have been reading quite alot, but none of the tutorials I found, seemed to work for me. Any suggestions on where I can find something that will work?

Thanks
gman
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #1 on: Aug 29, 2006, 02:23:25 PM »

As I am not sure what Olaf's class do exactly, for start I will try to answer your general  questions :

Quote
For each user that has access to the webpages, do they need to have an account setup within MySQL?

The user that connects to the database is php. So no matter how many users your site has, your users connect to the site, and the site connects to mysql with one account.

In general to react with the database you need a connection, and then everything is done with queries (SELECT, INSERT, UPDATE, etc.)

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6439
39458 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Aug 29, 2006, 02:36:23 PM »

as addition to Nick's his post, maybe you should describe what you try to do...


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #3 on: Aug 29, 2006, 02:44:11 PM »

BTW you inspired me to write a small tutorial here :

http://www.webdigity.com/index.php?action=tutorial;code=31 Visit through proxy

Wink

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6439
39458 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Aug 29, 2006, 02:53:59 PM »

BTW you inspired me to write a small tutorial here :

http://www.webdigity.com/index.php?action=tutorial;code=31 Visit through proxy

Wink
Nicely done Wink

but I think that gman has some problems with database table design and keys


Last blog : Just a better Internet portal provided by Google
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #5 on: Aug 29, 2006, 02:57:47 PM »

Yeah propably...

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #6 on: Aug 29, 2006, 03:15:51 PM »

Thanks guys...have not looked at any of your responses but there are no issues with the database structure..that seems to be fine. What I would like is to just have a user form and add to the table what has been entered when the submit button is clicked. But I am writing to fields 2, 3 and 7 and not the others as they are auto incremented, so I am not sure if that has to be accounted for when doing the INSERT statement.

So let me read what you fine fellows have written and I am glad that I inspired you all  Grin

gman
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6439
39458 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Aug 29, 2006, 03:31:51 PM »

try a statement like this:

INSERT INTO table SET col1 = 'value 1', col7 = 'value7',....

this way you don't need to add values for all columns


Last blog : Just a better Internet portal provided by Google
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #8 on: Aug 29, 2006, 04:29:20 PM »

Nikolas

I tried your example and assigned the variables for the $db_server..etc, but I am getting an error with the $query on the line starting with $res...also, O do not understand the $fields within your INSERT statement...what is this reading? The form names? Do they need to be defined outside the form? ie. $fields = 'field1', 'field2', 'field3'?

Thanks again
gman
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #9 on: Aug 29, 2006, 04:59:27 PM »

To get this snippet work, you must make a post form that will send the data there.

For each <input> you must declare as name the name of the field in the table that you are about to insert the data.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #10 on: Aug 29, 2006, 05:27:57 PM »

Thanks Nikolas...I am still getting an error with the $query. Here is my code

Code:
<form id="form1" name="form1"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label></label>
  <p>
    <label>Study_name
    <input type="text" name="study_name" />
    </label>
  </p>
  <p>
    <label>Study_Description
    <input type="text" name="study_description" />
    </label>
  </p>
  <p>
    <label>Study_Status
    <input type="text" name="study_status" />
    </label>
  </p>
  <p>
    <label>Create Study
<input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
  </form>
<?php
 
//Replace the values with the correct :
 
$db_server='localhost';
 
$db_user='user';
 
$db_password='password';
 
$db_name='clinical';
 
$db_connection = @mysql_connect($db_server$db_user$db_passwd);
 @
mysql_select_db($db_name$db_connection);
 
//This is the table that we are about to insert data in the database :
 
$dbTable 'global_studies';
 
//Some security tasks(SQL injection)
 
foreach ( $_POST as $f => $v )
 
$_POST[$f] = addslashes $v );
 
$fields array_keys($_POST);
 
//Prepare the query
 
$sql 'INSERT INTO '$dbTable '(`' implode ('`, `'$fields) . '`) VALUES (\'' implode ('\', \''$_POST) . '\');';
 
//So let's execute the query
 
$res mysql_db_query($db_name,$query,$db_connection);
?>
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #11 on: Aug 29, 2006, 05:30:12 PM »

in the end of the file add this, and let us know the result :

Code:
<?php
if (!$res) {
   echo 
"DB Error, could not query the database\n";
   echo 
'MySQL Error: ' mysql_error();
   exit;
}

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #12 on: Aug 29, 2006, 05:46:21 PM »

It states that "query" is undefined and

mysql_db_query(): supplied argument is not a valid MySQL-Link resource...in file name
DB Error, could not query the database MySQL Error:
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #13 on: Aug 29, 2006, 05:59:22 PM »

Then the error is in the connection.

Something is wrong there.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #14 on: Aug 29, 2006, 11:33:06 PM »

Thanks Nikolas

The connection now seems to be fine but I am getting an error stating that the submit button is unknown

Could you look at my form to see if that is setup correctly? I hate to be a bother with this

Thanks
gman
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #15 on: Aug 29, 2006, 11:34:42 PM »

Does this works ?

<input type="submit" value="Submit" />

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #16 on: Aug 30, 2006, 12:32:11 AM »

Same results...this is what I was getting

MySQL Error: Unknown column '' in 'field list'

Any idea? Don't you love challenges?

Thanks
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #17 on: Aug 30, 2006, 12:35:14 AM »

ok can you do this?

Code:
<?php
echo '<pre>';
var_dump$_POST );

and post the result here (after you hit the submit)

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
OMG!I am geek
**
Posts: 55
366 credits
Members referred : 0


« Reply #18 on: Aug 30, 2006, 12:47:45 AM »

I am now getting the first field within my table showing up even though I do not specifically tell it to be there

That code produced this
array(0) {
}
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #19 on: Aug 30, 2006, 11:22:46 AM »

Wait a minute. You get that error when you first load the page right?

If you press the submit button everything will be fine.

Here is how you should do the above code you posted :

Code:
<form id="form1" name="form1"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <label></label>
  <p>
    <label>Study_name
    <input type="text" name="study_name" />
    </label>
  </p>
  <p>
    <label>Study_Description
    <input type="text" name="study_description" />
    </label>
  </p>
  <p>
    <label>Study_Status
    <input type="text" name="study_status" />
    </label>
  </p>
  <p>
    <label>Create Study
<input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
  </form>
<?php
if ( isset ( $_POST['study_status'] ) )
{
//Replace the values with the correct :
 
$db_server='localhost';
 
$db_user='user';
 
$db_password='password';
 
$db_name='clinical';
 
$db_connection = @mysql_connect($db_server$db_user$db_passwd);
 @
mysql_select_db($db_name$db_connection);
 
//This is the table that we are about to insert data in the database :
 
$dbTable 'global_studies';
 
//Some security tasks(SQL injection)
 
foreach ( $_POST as $f => $v )
  
$_POST[$f] = addslashes $v );
 
$fields array_keys($_POST);
 
//Prepare the query
 
$sql 'INSERT INTO '$dbTable '(`' implode ('`, `'$fields) . '`) VALUES (\'' implode ('\', \''$_POST) . '\');';
 
//So let's execute the query
 
$res mysql_db_query($db_name,$query,$db_connection);
}
?>


Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3875
Tags : design html tutorials mysql databases Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] 2 Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Writing to database via a web form
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 22, 2008, 05:24:36 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 37.733
Total Topics: 7.649
Total Members: 4.394
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: rdgoetz

41 Guests, 4 Users online :

16 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.