Re: db insert question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux