Sending Web Forms to a Database

Hello Experts. I'm trying to find a good site or just examples of creating web forms and sending them to a database. I am familair with setting up an apache server and mySQL server. I have some coding experience (HTML/CSS/JS) but I am looking to expand my knowledge. I can create the forms but I am not sure hoe to send the info to a database.
LVL 2
msidnamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Accessing a database requires a server side programming language.  PHP is the most common one used with Apache and MySQL.
0
Chris StanyonWebDevCommented:
There are a couple of moving parts to this. The <form> is presented to a user for them to fill in some details. This <form> generally has a 'method' and an 'action'.

The 'method' tells the browser how to send the data to the server. For forms, it's generally considered best-practice to set the method to POST. This is where the term POSTing your form comes from.

The 'action' tells it where to send it. This will usually be the location of a server-side script that has been written to handle the data it receives. This could be written in one of several languages such as PHP, C#, Perl etc.

With that in mind, when a user clicks on a Submit button, the content of the form is sent via a POST request to your server-side script. This is where you would insert the data into your DB.

Simple example of a form:

<form method="POST" action="myScript.php">
    <input type="text" name="firstName">
    <input type="text" name="lastName">
    <input type="submit" value="Send">
</form>

Open in new window


Very simple example of myScript.php

<?php 
$hostname = 'localhost';
$username = 'yourUser';
$password = 'yourPass';
$database = "yourDb";

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOException $e) {
    echo $e->getMessage();
}

$stmt = $dbh->prepare("INSERT INTO yourTable (firstname, lastname) VALUES (:first, :last)");
$stmt->execute( array( 'first' => $_POST['firstName'], 'last'  => $_POST['lastName'] ));

echo "Done!";

Open in new window

You'll see from the script that the values from your form are passed into your scripts as the $_POST array. The keys of the array match the name properties from your form - firstName and lastName.

In the real world, you'd need much more to make this robust such as solid error checking, but that's the general principle of POSTing your form to a Db
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
msidnamAuthor Commented:
Chris Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.