Solved

Getting my form info to my web Database

Posted on 2006-06-21
6
486 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

840 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