|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Entering multiple users in DB
Hello Again....
I'm trying to enter multiple users into the database, but what I am trying to achieve is to enter all the contact details in one go then the next set of contact details...I'll post the code here to get more of an understanding... Database structure... PHP Code:
PHP Code:
PHP Code:
So basically I've got the contact form repeating 12 times, so they could enter upto 12 contacts for instance. When submit the form then they should go into the database. I've placed the values into arrays, but I'm stuck on how to implment the way you insert into the database. Any help greatly received. Ozzie |
|
#2
|
|||
|
|||
|
Hey Ozzie,
Well personally I would use a for loop to get the values from the $_POST associative array and add them, something like this: for($i = 0; $i < 12; $i++) { $query = "insert into myTable(name, age, sex) values("; $query .= "'" . $_POST["name_$i"] . "', "; $query .= $_POST["age_$i"] . ", "'" . $_POST["sex_$i"] . "')"; } etc... you would call the form values name_1, age_1, sex_1, name_n, age_n, etc.... so each one has a number from 1-12 at the end... |
|
#3
|
|||
|
|||
|
Thanks
Hey Mytch...
Big thanks for that, never come across $_POST before, what does that mean? Thanks Ozzie |
|
#4
|
|||
|
|||
|
I was wondering this will insert into the database 12 times, but what happens if the user enters one contact for instance can this be achieved??
THanks Ozzie |
|
#5
|
|||
|
|||
|
Ozzie, $_POST is the same as $HTTP_POST_VARS from older version of PHP. It contains name/value pairs for the details entered into your HTML form.
You can easily just add one, just use a check to see whether or not they are empty, something like this: for($i = 0; $i < 12; $i++) { if($_POST["name_$i"] != "") { $query = "insert into myTable(name, age, sex) values("; $query .= "'" . $_POST["name_$i"] . "', "; $query .= $_POST["age_$i"] . ", "'" . $_POST["sex_$i"] . "')"; } } ![]() |
|
#6
|
|||
|
|||
|
Thanks
Mytch....
Thanks for the code....but I done this, this way so to speak...see what you think... PHP Code:
Works well indeed when I echo the query out, it displays the number of times the user has enter n contacts. Thanks Mytch Ozzie. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Entering multiple users in DB |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|