login about faq

Due to the large amount of spam accounts, we temporarily disabled new user sign ups. To override this, email newuser.lgqa@gmail.com and an admin will determine if you are permitted to join


I have been wondering how I would go about adding data from users in a form (firstname,lastname, email) and then putting that data into a mysql database.

Is this possible? Are there any tutorials on how to do this, if not could you show me the code?

Any help would be greatly appreciated. Thanks for your time. Dom

asked Apr 22 '11 at 09:06

DomChester's gravatar image

DomChester
391121625


Below I have written up some code for you with detailed comments, if there's anything down there that you don't understand just send me an email: @live.com">n00ne@live.com.au

<?php

//We connect to the database first so that we can insert the data.

$server = "localhost"; //This is the default.
$db_user = "root"; //Username for your database.
$db_pass = "lockerngome"; //Password that goes with the username above.
$db_table = "users"; //Your table.

//Connect to the database.
mysql_connect($server, $db_user, $db_pass);
mysql_select_db($db_table);

?>
<html>

<form action='name-of-file-this-file.php' method='post'>
Firstname: <input type='text' name='firstname' /><br/ >
Lastname: <input type='text' name='lastname' /><br/ >
Email: <input type='text' name='email' /><br />
<input type='submit' name='submit' />
</form>

<?php

//If the form has been submitted.
if($_POST['submit']){

    //Getting the data from the form. 
    $firstname = mysql_real_escape_string($_POST['firstname']); //mysql_real_escape_string() is not necessary, it just makes the form more secure and protects from SQL injection.
    $lastname = mysql_real_escape_string($_POST['lastname']);
    $email = mysql_real_escape_string($_POST['email']);

    //Checking to see if those fields have been filled out.
    if($firstname && $lastname && $email){
        //Insert into the mysql database.
        //The blank value before first name ('') is the ID which should be set to auto increment.
        //WHERE id='1' means where the users id is 1, so John's id might be 1 so his data will be put into the database.
        //Generally you would not use id='1'. You would have the id set as a session so it would look like: WHERE id='".$session_id."'
        mysql_query("INSERT INTO users VALUES('', '$firstname', '$lastname', '$email') WHERE id='1'"); 
        //The VALUES above are in order so your mysql table would be: id, firstname, lastname, email. That's why you insert following that order.
    }
    else
        //If the firstname, lastname or email inputs have not been filled out, or if none of them have been filled out, echo an error.
        echo 'You have not filled out all of the inputs.';
}
//If the form hasn't been submitted.
echo 'Please fill out the form below.';

?>
</html>

answered Apr 22 '11 at 22:00

Hugo's gravatar image

Hugo
1.7k243151

edited Apr 22 '11 at 22:01

1

If you specify which fields you're filling in data for you wouldn't have to worry about the id slot. If you have the id field in your table set with auto_increment and as the primary key you could do a query like "INSERT INTO users (firstname,lastname,email) VALUES(var1,var2,var3)"

(Apr 23 '11 at 02:14) recck recck's gravatar image

I know. I said that ID should be set to autoincrement in the comments in the code..

(Apr 24 '11 at 01:49) Hugo Hugo's gravatar image

Sorry if this double posts - it did not appear for me.

Thank you all for your links / code it has really helped me with what i was looking for. I realise that I still needed to leave a blank gap for the auto-incrementing id so wat I put as a first name value was infact going to the id section and so forth. Once again I thank you all for your help.

answered Apr 23 '11 at 05:28

DomChester's gravatar image

DomChester
391121625

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported


Join Us in the Chat Room

Tags:

×142
×96
×29
×5
×4

Asked: Apr 22 '11 at 09:06

Seen: 1,012 times

Last updated: Apr 24 '11 at 01:49