Insert Backbone.js model into MySQL database

I have a backbone.js model with some default values ​​and a url:

var Box = Backbone.Model.extend({ url: "./save.php", defaults: { x: 0, y: 0, w: 1, h: 1 } }); 

Then I have an instance of this model, and I continue to save it:

 var box = new Box({ x:10, y:10, w:200, h:200 }); box.save(); 

Now I want to save this model in the MySQL database using the PHP script "save.php", it will look like this:

 <?php include('connection.php'); $id = $_POST['cid']; $x = $_POST['x']; $y = $_POST['y']; $w = $_POST['w']; $h = $_POST['h']; mysql_query("INSERT INTO boxes (id, x, y, w, h) VALUES('$id', '$x', '$y', '$w', '$h') ") or die(mysql_error()); ?> echo "Data Inserted!"; 

I tried to read many textbooks, but I can not get this simple model to save on work. Why is my code not working? Any ideas on how this can be resolved?

thanks

EDIT: QUICK SOLUTION

In a php script, the correct way to get information from a sent JSON object is as follows:

 $box_data = json_decode(file_get_contents('php://input')); $x = $box_data->{'x'}; $y = $box_data->{'y'}; $w = $box_data->{'w'}; $h = $box_data->{'h'}; 

And save to the database:

 mysql_query("INSERT INTO boxes(id, x, y, w, h) VALUES('', '$x', '$y', '$w', '$h') ") or die(mysql_error()); 

Thus, one row will be inserted into the "Boxes" table with the information of each of the attributes of the basic Box model. The server request method in this case is POST, and the identifier in the "Boxes" table is set to automatically increase.

+7
source share
2 answers

The backbone is based on the REST API: when saving / updating the model on the Backbone server, it will send it as JSON to the request body with the POST our PUT request. From the Backbone.sync Documentation

In the default implementation, when Backbone.sync sends a request to save the model, its attributes will be transferred, serialized as JSON, and sent to the HTTP body using an application of the content type / json.

This means that on the server side you must

  • determine the type of request
  • decodes serialized JSON

Something like this should start

 $request_method = strtolower($_SERVER['REQUEST_METHOD']); $data = null; switch ($request_method) { case 'post': case 'put': $data = json_decode(file_get_contents('php://input')); break; } // print_r($data); // note that mysql_* functions are deprecated // http://php.net/manual/en/function.mysql-query.php // inserting with a PDO object, assuming an auto incremented id $sql = "INSERT INTO boxes (x, y, w, h) VALUES(?, ?, ?, ?)"; $sth = $dbh->prepare($sql); $sth->execute(array( $data->x, $data->y, $data->w, $data->h )); $id = $dbh->lastInsertId(); 

Check this page for a more complete implementation of the REST API in PHP http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

+18
source

You forgot to send the identifier.

// $ id = $ _POST ['cid'];

enter image description here

Make an Id in AUTO_INCREMENT and remove from code:

$ id = $ _POST ['cid'];

 mysql_query("INSERT INTO boxes (x, y, w, h) VALUES('$x', '$y', '$w', '$h') ") or die(mysql_error()); 
0
source

All Articles