Solved

Getting my form info to my web Database

Posted on 2006-06-21
6
485 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

823 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