Hey all,
I need to put an array into my mysql database. I've already figured out that you can't actually insert an array into the database and that it needs to be broken up somehow. The question is, how?
This is the checkbox which is repeated for the number of articles there are in the selection:
<input type="checkbox" name="artikelid[]" value="<?php echo $row_all_records['artikelid']; ?>" />
It's passed to the next page, where the following code works with it:
$artikelid=$_POST['artikelid'];
$count=count($artikelid);
for ($i=0; $i<$count; $i++) {
echo $artikelid[i];
}
When I try and do the following, it goes wrong. I've got it so far that it's only giving me an SQL error:
<?php
if (isset($_POST['orderid'])) {
$orderid=$_POST['orderid'];
$artikelid[]=$_POST['artikelid'];
$count=count($artikelid);
mysql_select_db($database_DB_tests, $DB_tests);
for ($i=0; $i<$count; $i++) {
$insertSQL = 'INSERT INTO ink_order_artikel (orderid, artikelid) VALUES
($orderid, $artikelid[i])';
Result = mysql_query($insertSQL, $DB_tests) or die(mysql_error());
} //end for
header("Location: index.php");
} //end if
else { echo "No orderid, this can't be happening!"; }
?>
The SQL error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[i])' at line 1
The thing is, with the code shown above, I'm not trying to insert an array - I'm trying to insert the element of an array which is really just an int.
I don't really want to use the serialize function because I want each element of the array to be a separate row in the table so I can treat it as a separate entity.
I have a feeling that the answer is right in front of my nose and I just can't quite figure it out. So any help that any one can offer would be much appreciated. 🙂
Thanks!