Solved

Getting my form info to my web Database

Posted on 2006-06-21
6
483 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

746 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

13 Experts available now in Live!

Get 1:1 Help Now