1

I have multiple fields with names that look like name_$i and I am 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 number 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());


};
?>"
7
  • 1
    Use arrays for the name field instead of name_$1 then you can loop through them easily Commented Dec 11, 2012 at 13:37
  • The variables you're building, aren't those the field names? And are you getting any errors? Commented Dec 11, 2012 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++){...} Commented Dec 11, 2012 at 13:39
  • this would be easier to follow with some html Commented Dec 11, 2012 at 13:45
  • 1
    @shawndreck don't post answers as comments. Post answers as answers... Commented Dec 11, 2012 at 14:41

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for recommending PDO. Going to take a long time for mysql_* to die :(
@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. :-)
0

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,

Comments

0

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];

3 Comments

$i=1; echo 'name_$i'; echo "name_$i"; >> name_$i name_1
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
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
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.