Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have multiple fields with names that look like name_$i and im trying to figure out a way to on submit send them all to the database the thing is that the form is looped and the insert has to be able to adapt to the amount of fields in the form is there a way to do this???

<?php

$fldcnt = $_POST['fldcnt'];
$i = $_POST['i'];

for ($i = 0; $i < $fldcnt; $i++){
$NAME = $_POST['name_$i'];
$AGE = $_POST['age_$i'];
$ADDRESS = $_POST['address_$i'];
$TELEPHONE = $_POST['telephone_$i'];
$EMAIL = $_POST['email_$i'];



$q_register_new_users = "insert into registration set
        NAME = '$NAME',
        AGE = '$AGE',
        ADDRESS = '$ADDRESS',
        TELEPHONE = '$TELEPHONE',
        EMAIL = '$EMAIL'";

    mysql_query($q_new_products,$connection) or die(mysql_error());


};
?>"
share|improve this question
1  
Use arrays for the name field instead of name_$1 then you can loop through them easily –  Ben Carey Dec 11 '12 at 13:37
 
The variables you're building, aren't those the field names? And are you getting any errors? –  Michael Perrenoud Dec 11 '12 at 13:38
 
Using array is best way to do this. But if you still want to go head with a counter then you could use for($i = 0;isset($_POST["name_{$i}"]);$i++){...} –  shawndreck Dec 11 '12 at 13:39
 
this would be easier to follow with some html –  Adelphia Dec 11 '12 at 13:45
1  
@shawndreck don't post answers as comments. Post answers as answers... –  Cylindric Dec 11 '12 at 14:41
show 3 more comments

4 Answers

HTML and PHP

You can enter input fields into an array by simply calling the field name[]. Like so:

<input name="name[]" />

You can then use PHP to loop through the fields like so:

foreach($_POST['name'] as $key=>$value){
    // Insert the value of the form field into a string or query
    // i.e. build the query
    $query .= $value;
}

// Then execute the query for each set of fields

The logic above is actually incorrect, but it should give you an idea of what I mean.

MySQL

Your SQL syntax is incorrect, the correct syntax for inserting into a MySQL database is:

INSERT INTO `table` (`field_1`, `field_2`)
VALUES ('value_1', 'value_2')

PLEASE NOTE

The use of the mysql_ functions is hugely discouraged due to there impending deprecation. Instead, most PHP programmers are now using the PDO / SQLite Classes. Whilst these might seem complex, they are actually pretty simple and offer a much more secure way of executing SQL statements.

  1. PDO
  2. SQLite
share|improve this answer
 
+1 for recommending PDO. Going to take a long time for mysql_* to die :( –  Cylindric Dec 11 '12 at 14:35
 
@Cylindric That is true, am still trying to encourage as many people as possible to migrate to it. Once the migration is done, it is so much better and so much easier. :-) –  Ben Carey Dec 11 '12 at 14:38
add comment

The syntax for INSERT statement should be like this,

 INSERT INTO registration (NAME , AGE , ADDRESS, TELEPHONE, EMAIL)
 VALUES ('$NAME', '$AGE', '$ADDRESS','$TELEPHONE', '$EMAIL')

but hte query above is vulnerable with SQL INJECTION, please read the article below to learn how to protect from it,

share|improve this answer
add comment

If you are going to keep structure of your code, you need to use double quotes instead of apostrophes

$NAME = $_POST["name_$i"];

or put the variable out

$NAME = $_POST['name_'.$i];
share|improve this answer
 
What difference is this going to make!!!! –  Ben Carey Dec 11 '12 at 13:50
 
$i=1; echo 'name_$i'; echo "name_$i"; >> name_$i name_1 –  jcjr Dec 11 '12 at 13:54
 
That was not very clear in your answer, it seemed that you were stating that he shouldn't do the above, he should do the second. Have removed the downvote as it is now clearer what you intended –  Ben Carey Dec 11 '12 at 13:55
 
I tried to improve the post, hoping it is more clear now. Anyway, I agree that using arrays is better aproach, but my idea seems like a good quick fix –  jcjr Dec 11 '12 at 14:03
add comment

Using array is best way to do this. But if you still want to go head with a counter then you could use

for($i = 0;isset($_POST["name_{$i}"]);$i++)
{
 // name found
}

Please note that this code may not be optimal if the name_xx fields are coming from checkboxes, where a user selected items and skipped some in between.

PS. I posted this a comment but it is more suitable as an answer.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.