0

I am using the Gridstack library to create a dashboard. The widgets x,y,width and height can be put into a json, I am trying to save this json into MySQL.

I got the json array to enter a table in MySQL by pressing a button to work, when I was at my unviversity. When I went home, it stopped working. I am using PHP and MySQL.

The first issue was that the line below stopped working (it was fine before I went home, didn't touch code).

  $data = $_GET['name'];

Had to be changed to this:

   $data = isset($_GET['name']);

No idea why. Also the rest of the PHP stopped working. No errors, just does nothing. It isn't the script I have a problem with. All the Javascript work just fine.

Rest of the code:

 $('#save').click(function(){
      var res = _.map($('.grid-stack .grid-stack-item:visible'), function (el) {
el = $(el);
var node = el.data('_gridstack_node');
return {
    id: el.attr('data-custom-id'),
    x: node.x,
    y: node.y,
    width: node.width,
    height: node.height
};
window.location.href = "index.php?string=" + JSON.stringify(res);
});
<?php
$connect = mysqli_connect("localhost", "root", "", "widgetCollection");


 $data = isset($_GET['string']);
 //$data = $_POST['variable'];

  $array = json_decode($data, true);

  foreach((array)$array as $row) {
  $sql = "INSERT INTO grids(x, y, width, height) VALUES('".$row["x"]."', '".$row["y"]."', '".$row["width"]."', '".$row["height"]."');";

  mysqli_query($connect, $sql);
  }
  ?>
  alert(JSON.stringify(res));

  });
Ranj Salih
  • 53
  • 6

1 Answers1

0

Isset allows you to test the existence of a key in an array, and will return a boolean.

You should not use isset like that.

You can write instead :

if (isset($_GET['name'])) {
    $data = $_GET['name'] ;
} 

That will test the existence of the key 'name' in the array $_GET and, if found, will enter the 'if' condition and set the variable.

johnkork
  • 659
  • 5
  • 19
  • I now get: Notice: Undefined index: name in C:\xampp\htdocs\widgets\index.php on line 98. Line 98 being: $array = json_decode($data, true); – Ranj Salih Mar 11 '17 at 16:59