How can i do this insert


I want to insert a number of records in a database table but i know i will need a for loop or something.

If you look on this page:

You will see that when you click on “Add author”, it adds another text box. So now all thats left is to do a insert statement where it adds this to a table no matter how many textboxes there are. So i think some sort of for loop is needed.

I currently would use this method to add one instance of a record:

    public function insertAuthor(){

        $sql = "INSERT INTO Author
                    (name, date_added, deleted) VALUES
        $result = mysql_query($sql);
        return "Successfully added author";

But how can i add something to this that loops through each text box and inserts it?



Thanks for your replies. I am trying to understand your suggestions. So if i did something like this:

class People {

    public function insertAuthor(){

        foreach($_POST['author'] as $author):
            $sql = "INSERT INTO People
                        (Pname, Pdate_added, Pdeleted) VALUES
            $result = mysql_query($sql);
        return "Successfully added author";

And then on the front end page i had my input like this:

<input type="text" name="author[]" id="author[]" />

Calling the function like so:

if(isset($_POST['author[]']) && $_POST['author[]'] != ''):

Would you expect it to work? It does not give me any errors but it doesn’t INSERT.

Any ideas what i am doing wrong?

Thanks again

if you change your function a little bit this should work:

$postarray = $_POST['yourvalue'];
$message = array();

foreach ($postarray  as $value) {

if (trim($value)) {

$message[] = insertAuthor($value);



echo implode($message, '<br/>');

your input fields should look something like this:

<input type="text" name="authors[]" />

if you name all your input fields authors, then you can do a foreach($_POST[‘authors’] as $author)

just make sure you check that $author has something in it…right after the foreach, do

if (!trim($author)) continue;

Nope, wouldn’t expect that to work considering $_POST[‘authors’] is an array.

Above that code place:

echo '<pre>',print_r($_POST),'</pre>';

You should than see that the authors key is an array.

In regards to the way your doing it I would not use separate insert statements but a single one. You can insert multiple authors at once following the below syntax. You would just need to to use a loop to build out the SQL query.

INSERT INTO authors (author_name) VALUES ('foo'),('bar'),('blah'),('whatever')

That is what I would do to eliminate unnecessary trips to the database.

The other thing I would recommend although you can consider this at your won discretion is to use a key for the author name input. That way it is very easy in the future to extend upon the data entered for a author or break up fields such as first and last but keep the data together programmatically.

<input type="text" name="authors[0][first_name]">
<input type="text" name="authors[0][last_name]">

<input type="text" name="authors[1][first_name]">
<input type="text" name="authors[1][last_name]">

<input type="text" name="authors[2][first_name]">
<input type="text" name="authors[2][last_name]">