Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting my form info to my web Database

Posted on 2006-06-21
6
Medium Priority
?
498 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

971 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