|
Posted by Jerry Stuckle on 04/16/06 05:53
NC wrote:
> GJPeacock@gmail.com wrote:
>
>>Maybe i should re-phrase my question. I'm using php with a MySQL
>>database, and i would like to know how to insert a new record into a
>>table for each checked item on one form.
>
>
> OK, let's say you have a form:
>
> <form method="POST" action="insert.php">
> <input type="checkbox" name="id[]" value="32">Article #32<br>
> <input type="checkbox" name="id[]" value="38">Article #38<br>
> <input type="checkbox" name="id[]" value="45">Article #45<br>
> <input type="checkbox" name="id[]" value="59">Article #59<br>
> <input type="Submit">
> </form>
>
> Now, let's assume the user checked articles #32 and #59. These values
> will be available to insert.php as fields in $_POST['id'], which in
> this case will be an array. So in insert.php you can write:
>
> $query = 'INSERT INTO related_articles (id) VALUES (' .
> implode('), (', $_POST['id']) . ')';
> $result = mysql_query($query)
> or die('Could not execute INSERT query');
>
> That's it, really...
>
> Cheers,
> NC
>
It also violates first normal form for relational databases.
Rather, something like (for simplicity, no error checking shown):
foreach ($_POST['id']) as $id) {
mysql_query("INSERT INTO related_table (article_id, related_id) " .
"VALUES ($origId, $id)");
$origId is the original article id, $_POST['id'] is the id(s) of the related
article(s).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|