I have a MYSQL database query nested in a foreach statement like this:
foreach ($_POST['articles'] as $article => $number) {
$sql_update2 = "INSERT INTO testmerrill.Device_Article_Connection (device_id, article_id)
VALUES ((MAX (device_id) FROM testmerrill.Devices),'{$_POST['articles'][$article]}))'";
query_db1($sql_update2);
}
The above query runs after this one:
$sql_update1 = "INSERT INTO testmerrill.Devices (device_name, device_manufacturer)
VALUES ('{$device_name}', '{$device_manufacturer}')";
query_db1($sql_update1);
This page is the create page of a CRUD program I am writing. I have two tables I am working with here: Devices and Device_Article_Connection. What I am trying to do is insert into my Device_Article_Connection table multiple select statement options the user posted on submit, calling a query for as many options the user chose to include as associated with a new 'device'. I know my current solution is probably not a very elegent way to do it, but thats where I'm at at the moment. The difficulty is that I do not know the id of the new device the user just created, which I need in order to associate it with the articles the user chose to associate with it. I am trying to find the device id using the MAX function (because the last id the user just added should be the largest), but I can't seem to get that to work, there is some syntax error that I have not been able to pinpoint.
I am thankful for any suggestions.