-1

I'm trying to "convert" a very small project (list of data that can be re-arranged by dragging and dropping) from, well, I suppose usual PHP, mysqli and ajax to working with MeekroDB instead.

It works fine in its original form (this is the small PHP file that the Ajax uses):

<?
 require_once __DIR__.'/../ini.php'; 

$SortingOrder = isset($_POST["SortingOrder_ids"]) ? $_POST["SortingOrder_ids"] : [];

if(count($SortingOrder)>0){
    for($order_no= 0; $order_no < count($SortingOrder); $order_no++)
    {
     $query = "UPDATE SortingTable SET SortingOrder = '".($order_no+1)."' WHERE ID = '".$SortingOrder[$order_no]."'";
     mysqli_query($con, $query);
    }
    echo true; 
}else{
    echo false; 
}

?>

But does absolutely nothing now that I'm trying with MeekroDB:

<?
 require_once __DIR__.'/../ini_meekro.php'; 

$SortingOrder = isset($_POST["SortingOrder_ids"]) ? $_POST["SortingOrder_ids"] : [];


if(count($SortingOrder)>0){
  for($order_no= 0; $order_no < count($SortingOrder); $order_no++) 
  {
    
    DB::query("UPDATE SortingTable SET SortingOrder=%i, WHERE ID=%i", $order_no+1, $SortingOrder[$order_no]);
  };

    echo true; 
}else{
    echo false; 
}

?>

The javascript/jQuery script is unchanged. Of course, I have updated how the list is initially fetched from the database and displaying on the site, and that works just fine. I can also still drag things, but it never saves the new sorting order.

I have never done anything with Ajax before. I initially used the guide found here: http://learninfinity.info/drag-and-drop-table-row-sorting-ajax-php-and-mysqli/ and got it all to work nicely. But I would like it to work with MeekroDB instead, and I'm stuck.

eqr21
  • 51
  • 6
  • Didn't you encounter any error of sorts? You can inspect in browser and lookup in network tab, if there's any error or not. You may use ``error_reporting(E_ALL)`` at the top of php script file to fetch any error. – FerdousTheWebCoder Apr 03 '21 at 13:23
  • Nope, no errors. I don't know if it's because the PHP script the Ajax runs is a separate file? Using Network inspector doesn't help me much either, it just gives status 200 and shows the ajax_update.php file does run. – eqr21 Apr 03 '21 at 14:03
  • @Dharman Yes, I am aware that the original is open to SQL Injections, hence I wanted to use MeekroDB rather than what the tutorial I followed used. As far as I can read, MeekroDB is effective to avoid SQL Injections, but I'd be glad to know if there's a vulnerability in it that you know of? – eqr21 Apr 03 '21 at 14:54

1 Answers1

0

After way too long I finally found a solution.

I don't really know or understand the big difference in the two ways to update in MeekroDB, but using the other way and making variables works.

<?
 require_once __DIR__.'/../ini_meekro.php'; 

$SortingOrder = isset($_POST["SortingOrder_ids"]) ? $_POST["SortingOrder_ids"] : [];


if(count($SortingOrder)>0){
  for($order_no= 0; $order_no < count($SortingOrder); $order_no++) 
  {
    
    $NewOrderNumber = $order_no+1;
    $EntryID = $SortingOrder[$order_no];
    
    //DB::query("UPDATE SortingTable SET SortingOrder=%i, WHERE ID=%i", $order_no+1, $SortingOrder[$order_no]);
    DB::update('SortingTable', ['SortingOrder' => $NewOrderNumber], "ID=%i", $EntryID);
  };

    echo true; 
}else{
    echo false; 
}

?>

I'll leave this up here, but I'll be very happy if someone knows a better and cleaner solution! :)

eqr21
  • 51
  • 6