If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Can someone help explain multiple insert?

I've got a problem sorting out an insert that cycles through multiple rows of a form, and then constructs and executes the insert. I've Googled 'mysql php insert multiple rows form', and while there's a quite a few articles, none of them seem particularly well commented or explained, so I'm hoping someone can shed light on my confusion.

I have a database table called mailinglist which holds data about which people get invited to specific events for a client of mine. It has five columns - mailinglistID (the primary key) mailnglistnameID, locationID and clientID (foreign keys) and isCard (a boolean).

I found some sample code at Object Mix, which I used as the basis of my attempt, after cleaning up the HTML.

//Assign each array to a variableforeach($_POST['mailinglistnameID'] as $row=>$mailinglistnameID){$mailinglistnameID=$mailinglistnameID;$locationID=$_POST['locationID'][$row];$clientID=$_POST['clientID'][$row];$isCard=$_POST['isCard'][$row];}

//enter rows into databaseforeach($_POST['mailinglistnameID'] as $row=>$mailinglistnameID){$mailinglistnameID=mysql_real_escape_string($mailinglistnameID);$locationID=mysql_real_escape_string($_POST['locationID'][$row]);$clientID=mysql_real_escape_string($_POST['clientID'][$row]);$isCard=($_POST['isCard'][$row]);}

However, running it only results in the last row of the form being inserted into the table, and the isCard boolean isn't inserted if the checkbox is ticked. Also, I'm a little concerned that, while I've read that giving the name of the field a '[]' suffix prepares PHP for inserting the contents into an array, that having multiple HTML elements with the same id invalidates the HTML, so I'm not sure that I'm doing it right…

I've been looking at this since first thing yesterday, and I'm just going round in circles now. If anyone could point out where I'm going wrong, I'd be grateful.

Okay - thought I'd try answering my own question about the repeated [] making the ids invalid and put numbers in there, which I can insert on my final from using a loop at run-time. So now my test form looks like this:

Users who have thanked XterM for this post:

Thanks - and thanks for the commenting in the code. I guess I've been spoiled having Dreamweaver write all the update and insert behaviours for me - I think it's probably time for me to buy a book and dig into the PHP properly!