In JavaScript, I'm using an Ajax call and I'm sending an array of shops.
Except for the last element in the array all the shops already exist in the DB.
The last element is a new shop.
The PHP code should get the array and update all the shops that are already in the DB and insert the new one after the last row of the table.
The PHP code uses $wpdb->insert() to insert the last shop but for some reason it doesn't always insert it after the table's last row.
The insert parameters include values for all the fields except for the index. This field is marked as auto_increment in the DB and indeed the inserted row get the expected index number.
But it's not inserted after the last row as expected.
What can be the cause?
Here's the php code:
function updateshops($shopsArray){
global $wpdb;
$sArray = $_POST["shopsArray"];
$aSize = sizeof($sArray);
for ($i=0; $i<$aSize; $i++)
{
if ($sArray[$i]['shopId']!=-1) // update existing records
$wpdb->update( 'wp_shops', array( 'shopName'=>$sArray[$i]['shopName'], 'shopAddress'=>$sArray[$i]['shopAddress'] ), array('shopId'=>$sArray[$i]['shopId']));
else // add new records
$wpdb->insert( 'wp_shops', array( 'userId'=>$sArray[0]['userId'], 'shopName'=>$sArray[$i]['shopName'], 'shopAddress'=>$sArray[$i]['shopAddress'], 'shopLat'=>45, 'shopLng'=>20 ));
}
}
SHOW CREATE TABLE wp_shops
. Plus you should be quoting your array keys, they're not constants!CREATE TABLE 'wp_shops' ( 'shopId' bigi...
.CREATE TABLE...
. So what do you mean 'last row'? I think you're just after anORDER BY shopID ASC
clause :)ORDER BY shopID ASC
will do the trick but I'd rather have it working the right way in the first place.