|
Posted by Duncan Hill on 07/21/05 17:30
On Thursday 21 July 2005 15:23, Jim Moseby typed:
> > Yes, MySQL supports an extended insert syntax of insert into
> > foo (...) values
> > (...), (...), (...).
>
> Interesting! Consulting the manual, I see that you are correct. So the OP
> would do something like:
>
> $sql="insert into foo values ";
> foreach($formdata as $thisdata){
> $sql.="($thisdata,...,...,...),";
> }
> $result=mysql_query($sql);
>
> (of course the above code is broken, because it would leave an extra comma
> at the end, but this is the general idea, no?)
Vaguely, yes. The trick here is that any single failure in one of the columns
can leave you with inconsistent data. Thus, use this with InnoDB table and
transactions enabled so you can rollback on failure :)
For sake of correctness, the data should be checked for validity first. Then,
assuming that all of the form data is going into the same table as the same
fields, you could push "($variable1, $variable2)" into an array, and
join(',', $that_array)
Presto, no extra comma. Should use the mysql escape stuff too if not using an
abstraction layer that deals with it for you.
--
My mind not only wanders, it sometimes leaves completely.
Navigation:
[Reply to this message]
|