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());


};
?>"
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41

4 Answers4

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
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
  • +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
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,

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
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];
jcjr
  • 1,503
  • 24
  • 40
  • $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
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.

shawndreck
  • 2,039
  • 1
  • 24
  • 30