Inserting data to a database using a text box in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Arsenal Rule
Forum Commoner
Posts: 26
Joined: Fri Apr 08, 2005 4:13 am

Inserting data to a database using a text box in a form

Post by Arsenal Rule »

Basically I have created a form with several text boxes First Name, Surname, Username and Password. The form also contains a Submit Button.

And a database the contains the fields user_id (Primary key) (Auto-Increment), firstname,surname, username, password.

Ok I what need to know is how I would be able to insert the data typed into the text boxes into my database using PHP, with the data going into the correct fields when the submit button is clicked. And the user_id being able to Auto-Increment itself.

Please any help would be greatly appreciated thankyou so much for you time :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

If the form's method is post then

Code: Select all

$res = mysql_query("INSERT INTO `tableName` VALUES (
'',
'".$_POST['FirstName']."',
'".$_POST['Surname']."',
'".$_POST['Username']."',
'".$_POST['Password']."'
)");
if (!$res) die(mysql_error());
Edit : Should be in PHP code section
Arsenal Rule
Forum Commoner
Posts: 26
Joined: Fri Apr 08, 2005 4:13 am

How would I be able to insert data to a database using a tex

Post by Arsenal Rule »

hey thanx alot can I ask something, is the $_Post is the name for variable names given to the textboxes in the form? And where does it refer to the submit button being pressed I just want to make it clear to me so i can understand it better please. Thanx for yor help mate
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Yes.

Code: Select all

<form action=&quote;whereever.php&quote; method=&quote;post&quote;>
First Name : <input type=&quote;textbox&quote;  name=&quote;FirstName&quote; /><br>
Surname    : <input type=&quote;textbox&quote;  name=&quote;Surname&quote; /><br>
User Name  : <input type=&quote;textbox&quote;  name=&quote;Username&quote; /><br>
Password   : <input type=&quote;password&quote; name=&quote;Password&quote; /><br>
</form>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-Code.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

$_POST['input name']; is anything that comes from a form basically.

You should probably MD5 your passwords aswell. To do that simply change the code to this:

Code: Select all

// to change a password to a md5 hash use md5('$var');
$Password = md5($_POST['Password']);

$res = mysql_query("INSERT INTO `tableName` VALUES (
'',
'".$_POST['FirstName']."',
'".$_POST['Surname']."',
'".$_POST['Username']."',
'".$Password."'
)");
if (!$res) die(mysql_error());
This will add a lot more security.

When you compare passwords to log a user in simply md5 the value they enter for the username and select the previously hashed password from the database.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

The way I do this is simpler I think - you could seperate the actual process and insert from eachother, i.e connect to your database and create a form with method = "post" and action = "processwhatever.php"

in your form file just create your text boxes in the standard way but give them the input name = "yourfieldnames"

in the "processwhatever.php" file, connect to the database and "INSERT INTO tblName ( field1, field2, field3)
VALUES ('$field1','$field2','$field3')";
//and then just link back to your form where you will be presented with an empty form ready to load another record.
header ("location: http://www.yourwebsite/yourform.php");
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

that's a step to far (imho).

but i prefer to have one script that does insert. (and other scripts for update/delete/read/list)

so, all my insert script has to do is the following:

- test if all required values are there
- test if all values are valid

--> if both conditions are met:
insert them (and then tell the user insertion succeeded (and/or) redirect to other page)

--> else display the fields i want to recieve
- eventually insert the values the user already submitted
- eventually insert message if the user submitted an invalid value
Post Reply