Page 1 of 1
Inserting data to a database using a text box in a form
Posted: Sun Apr 10, 2005 11:40 am
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

Posted: Sun Apr 10, 2005 11:59 am
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
How would I be able to insert data to a database using a tex
Posted: Sun Apr 10, 2005 12:16 pm
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
Posted: Sun Apr 10, 2005 1:24 pm
by anjanesh
Yes.
Code: Select all
<form action="e;whereever.php"e; method="e;post"e;>
First Name : <input type="e;textbox"e; name="e;FirstName"e; /><br>
Surname : <input type="e;textbox"e; name="e;Surname"e; /><br>
User Name : <input type="e;textbox"e; name="e;Username"e; /><br>
Password : <input type="e;password"e; name="e;Password"e; /><br>
</form>
Posted: Sun Apr 10, 2005 3:04 pm
by John Cartwright
Moved to PHP-Code.
Posted: Sun Apr 10, 2005 10:33 pm
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.
Posted: Mon Apr 11, 2005 5:55 am
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");
Posted: Mon Apr 11, 2005 11:07 am
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