RE: [PHP] Multi Files insert...

From: Date: Sat, 14 Sep 2002 16:27:03 +0000
Subject: RE: [PHP] Multi Files insert...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-116383@lists.php.net to get a copy of this message
> So if I understand right I can do this... > > INSERT INTO actormovie (fname, lname, title) VALUES > ($fname1,$lname1,$title1),($fname2, $fname, > $title2),($fname3,$lname3,$name) > Cool so I could have a form with mutli values to input. Yes, that's the correct syntax. > How would do with an if or while statement. > > Would this be a way? > > $sql = "INSERT INTO actormovie (fname, lname, title) VALUES (\"$fname\", > \"$lname\", \"$title\")"; > > $result = mysql_query($sql); > > while ($row = mysql_fetch_array($result)) { > $sql = $row[$fname], $row[$lname], $row[$title]; > > } Actually, I would name your form elements as arrays, and deal with them that way. It makes things easier. Create your form like this: <? //number of actors to input $num_actors = 4; echo "<table>\n"; echo "<tr><td>fname</td><td>lname</td><td>movie</td></tr>\n"; //create a row of text entry boxes for each actor for($x=0;$x<$num_actors;$x++) { echo "<tr><td><input type='text' name='fname[]'></td>\n"; echo "<td><input type='text' name='lname[]'></td>\n"; echo "<td><input type='text' name='move[]'></td></tr>\n"; } echo "<tr><td colspan='3'><input type='submit' name='submit' value='submit'></td></tr></table>\n"; ?> Wrap a form around that, too. Then process it with something like this (assuming you use POST for your form): <? $num_actors = count($_POST['fname']); $sql = "INSERT INTO table (fname,lname,movie) VALUES "; for($x=0;$x<$num_actors;$x++) { $sql .= "('{$_POST['fname'][$x]}', '{$_POST['lname'][$x]}', '{$_POST['movie'][$x]}'),"; } //strip off last comma $sql = substr($sql,0,-1); $result = MySQL_query($sql); ?> Check for syntax errors, but that should give you an idea. It makes it adaptable to any number of entries, too. ---John Holmes...

« previous php.general (#116383) next »