-1

I want to be able to insert multiple input fields as it's own row in the database.

Currently it's inserting all input fields with the same class in one field in one row.

Ajax:

('#save').click(function() {
        var accNum = $('.account').map(function() { 
                        return this.value;
                      }).get();
        $.ajax({
            url: 'add-general-journal.php',
            type: 'POST',
            data: {
                accNum : accNum,   
            }
        });
    });

PHP:

$values = implode("",$_POST[accNum]);
  mysql_query("INSERT INTO generalHeader(accountNumber) VALUES('$values')") or die(mysql_error());
  echo "Success.";

The output is showing like this after submitting:

enter image description here

Is it possible to do this in Ajax and PHP?

designtocode
  • 2,215
  • 4
  • 21
  • 35
  • 3
    Just wanted to say you should not be using mysql_query and especially not with unsanitized input such as straight from a post. That is how you get mysql injection attacks. Look into mysqli or PDO – DGS Aug 23 '13 at 08:55
  • Can you elaborate this " want to be able to insert multiple input fields as it's own row in the database." statement of yours – dreamweiver Aug 23 '13 at 08:57
  • @DGS I can easily santize this with Ajax and/or PHP.. I just need to find out how I can insert each accNum as it's own row in the table... – designtocode Aug 23 '13 at 09:02
  • 1
    Can you show an example of the output that you want? That will make it ewasier form me to work towards the solution. – Jonathan Römer Aug 23 '13 at 09:03
  • @JonathanRomer This is what I am looking to do http://stackoverflow.com/questions/9796084/inserting-multiple-rows-into-mysql-with-one-insert-statement-using-php-implode-f – designtocode Aug 23 '13 at 09:08

1 Answers1

-1

$values = implode("','",$_POST['accNum']); will add a comma and quotes in between each of the array items for you so that the INSERT will contain comma separated strings

DGS
  • 6,015
  • 1
  • 21
  • 37
  • Then please explain what you want in the question with more clarity – DGS Aug 23 '13 at 09:04
  • This is something that I want.. http://stackoverflow.com/questions/9796084/inserting-multiple-rows-into-mysql-with-one-insert-statement-using-php-implode-f – designtocode Aug 23 '13 at 09:08
  • So you want for each row in the input array in php to be its own row? but to be handled in one INSERT? – DGS Aug 23 '13 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36105/discussion-between-dgs-and-msbodetti) – DGS Aug 23 '13 at 12:06