Re: PHP, MYSQL & multiple records

From: Date: Mon, 02 Apr 2001 23:47:24 +0000
Subject: Re: PHP, MYSQL & multiple records
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-46710@lists.php.net to get a copy of this message
Need to see your code for inserting data. But I think first, you should be naming your fields with empty[]s Ie. <input type="text" name="photo_no[]"> Etc That way, php will build an array out of the variables, and it will ONLY contain values, if values were submitted.. Then you build your insert query by looping through the arrays by index# <? // with arrays:$photo_no[], $datetak[],$photodesc[] built by PHP when form // values were parsed $query = "INSERT INTO photo (photono,photoid,photodesc,photodate) VALUES "; for($I=0;$i < count($photo_no); $I++) { if(!($I == count($photo_no) -1)) { //check to see if this is the last data set to insert if it is NOT the last, do this $query .= "('$photo_no[$I]',NULL,'$photodesc[$I]','$datetak[$I]'),"; //note the comma, to link the insert together. } else { //do this if it IS the last dataset to enter $query .= "('$photo_no[$I]',NULL,'$photodesc[$I]','$datetak[$I]')"; } } $result = mysql_query($query,$link); ?> Why the if-else? Because, we don't want that trailing comma if it is the last data set to be insert. We would get an error. This piece of code will produce a query like: INSERT INTO photo (photoid,photono,photodesc,photodate) VALUES ('photo_number_info1',NULL,'photo_description1','photo_date1'), ('photo_number_info2',NULL,'photo_description2','photo_date2'), ('photo_number_info3',NULL,'photo_description3','photo_date3') If 3 sets of data were passed in. I am making the assumption here that photoid is a unique auto_incrementing field in your database. If so, you could also leave out the null field in the query. On 4/2/01 4:14 PM, "Curtis" <curtis_c@bellsouth.net> wrote: > Hello, > I don't relly get this... can sonmeone try and explain. > I have a mysql database and phtml form. > > I can insert information intodatabase from the form, no problem. > UNTIL > I have a few items I want to insert multiples of ie DATE, DESCRIPTION > > My table is... > photoid > photono > photodesc > photodate > > My form reads... > > PHOTO NO : > DATE TAKEN: > DESCRIPTION : > PHOTO NO : > DATE TAKEN: > DESCRIPTION : > PHOTO NO : > DATE TAKEN: > DESCRIPTION : > PHOTO NO : > DATE TAKEN: > DESCRIPTION : > PHOTO NO : > DATE TAKEN: > DESCRIPTION : > > I am using an array in my phtml, that sorta works. > It will insert 5 records into my database, but if I only want 1 it still > inserts 5 > > Is there an example anyone knows of I can look at. > > Thanks > Curtis > >

« previous php.general (#46710) next »