Solved

Getting my form info to my web Database

Posted on 2006-06-21
6
484 Views
Last Modified: 2008-02-26
Hi all,  I have created a webpage with a form for users to fill out.  I am also using MySQL on ipower to gather the information from the form.  I have zero knowledge of forms and web based DB's.  Any advise on getting the info from the form to the DB?   Thanks so much in advance for the advice.
0
Comment
Question by:danielspcs
6 Comments
 
LVL 21

Expert Comment

by:Julian Matz
ID: 16956618

First you need to create the variables for the form input...

$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : '';  // assuming form field name labeled "firstname"
$lastname = isset($_POST['lastname']) ? $_POST['lastname'] : '';

Then check if the user has filled out the data correctly...

if ( empty($firstname) || empty($lastname) ) {
 $message = '<b>Please enter all required details...</b>';
} else {
 $connection = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ( 'Error: could not connect to database!' ); // this is the MySQL connection
 mysql_select_db(DB_NAME,$connection); // This selects the database
 $query = "INSERT INTO table_name SET firstname='$firstname', lastname='$lastname'"; // This is for the DB query
 $result = mysql_query($query,$connection) or die ( mysql_error() ); // This enters $firstname and $lastname in the DB
}


Hope this example points you in the right direction...
0
 
LVL 21

Expert Comment

by:Julian Matz
ID: 16956644
MySQL login details need to be defined first of course...


define ('DB_HOST', 'localhost'); // usually "localhost" is a good bet
define ('DB_USER', 'username');
define ('DB_PASS', 'password');
define ('DB_NAME', 'db_name');


To close a MySQL connection, you can use the code below at the end of your page:

<? mysql_close($connection); ?>


This is the manual for PHP MySQL functions:
http://www.php.net/manual/en/ref.mysql.php
0
 
LVL 4

Expert Comment

by:VRGultom
ID: 16956968
create form in html page
 <form action=x.php method=post>
     
     //field of form that user have to fill
    ....
     ....
     ....

    //submit form
    <input type = submit value = 'Submit'>
</form>


after user press submit button, the value that user enter will send to x.php

x.php must get the parameter sent to him

<?
          //get the value of parameter
          $var1 = ...
          $var2 = ...
         
         //open database connection
         .....
         //sql statement to write to database
         .....
        //write to database using sql statemtn
       ....
?>

that's the logic.
For syntax you can browse more in php web site

Regards
Veronika

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:bpmurray
ID: 16957855
These answers all assume php. Are you using php?
0
 
LVL 2

Accepted Solution

by:
Mr_Lenehan earned 250 total points
ID: 16958626
Whilst I am quite sure that all of the above code snippets are correct, perhaps you'd be better off having a read through a tutorial: http://www.freewebmasterhelp.com/tutorials/phpmysql/1

If you don't know a great deal abuot a topic then its always best to have a read up on it first... that way you'll know how to use the code snippets above. This tutorial guides you through using MySQL with PHP and includes stuff on inserting and viewing inserted data.

Hope this helps you.
0
 

Author Comment

by:danielspcs
ID: 16960275
Thank you so much for the link, as I didn't have a clue where to begin.   The website tutorial was very clear on how to set the database up from beginning to end.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now