将Backbone.js的模型导入MySQL数据库 [英] Insert Backbone.js model into MySQL database

查看:135
本文介绍了将Backbone.js的模型导入MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些违约率Backbone.js的模型和一个网址:

I have a backbone.js model with some defaults and an 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 proceed to save it:

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

现在我想用PHP脚本save.php这个模型保存到一个MySQL数据库,它是这样的:

Now I want to save this model into a MySQL database using a PHP script "save.php", it goes 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!";

我已经尝试了许多阅读教程,但我不能让这个简单的模型保存到工作。为什么我的code不工作?关于这可怎么解决的任何想法?

I have tried reading many tutorials but I cannot get this simple model save to work. Why is my code not working? Any ideas on how can this be solved?

感谢

编辑:快速的解决方案

在PHP脚本,以获得来自发送JSON对象信息的正确方法是如下:

In the php script, the correct way to obtain the information from the 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'};

和以在数据库中存储

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

在这种方式的一行将在表盒子与骨干模型箱的属性中的每一个的信息插入。
在这种情况下,服务器请求方法是POST,并在表的盒子设置为自动递增的ID。

In this way one row will be inserted in the table "boxes" with the information of each one of the attributes of the backbone model Box. The server request method in this case is POST and the id in the table "boxes" is set to auto-increment.

推荐答案

骨干是基于REST API:保存时/更新模型到服务器,骨干网将其发送的请求体序列化为JSON以 POST 我们的 PUT 请求。从 Backbone.sync文档

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

使用默认的实现,当Backbone.sync发送一个请求
  保存模式,其属性将被传递,序列化为JSON,
  而在HTTP正文中发送与内容类型的应用程序/ JSON。

With the default implementation, when Backbone.sync sends up a request to save a model, its attributes will be passed, serialized as JSON, and sent in the HTTP body with content-type application/json.

这意味着服务器端,你必须

This means that server-side you have to


  • 确定请求的类型

  • 德code序列化的JSON

像这样的东西应该让你开始

Something like this should get you started

$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();

查看此网页在PHP中进行更彻底的执行REST API的<一个href=\"http://www.gen-x-design.com/archives/create-a-rest-api-with-php/\">http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

这篇关于将Backbone.js的模型导入MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆