Something really strange is happening while trying to add in records to a table in my database. Below is the code i'm using
// empty the current table
mysql_query("TRUNCATE TABLE `topproducts`") or die("ERROR: deleting old list");
// seperate id numbers from the list
$list = explode(",",$_POST['list']);
// start SQL query
$SQL = "
INSERT INTO `topproducts` ( `ID` )";
$count = 0;
// generate SQL query
foreach($list as $id) {
if($count == 0) { $SQL .= " VALUES "; }else { $SQL .= ", "; }
$SQL .= "( '".$id."' )";
$count++;
}
// echo $SQL; //for debugging
// run query
mysql_query($SQL) or die("ERROR: adding products");
// if there are no errors so far, then the below message will display
echo "<strong>Top Products list has been edited succesfully!</strong><br>";
echo "All the products [on the main page] will now show in the order you specified
in the right list.";
my database structure is
Field Type Null Key?
order int(5) No Yes
ID int(11) No No
when using my code, it adds this to the table
order ID
1 0
when debugging the code, the SQL query that is built is
INSERT INTO `topproducts` ( `ID`) VALUES ( '3' ), ( '2' )
The query works as expected when used through phpMyAdmin, but for some reason doesn't work when using it in the code I posted at the top of the page.
Hopefully someone here can help out 🙂