• <bdo id='en4od'></bdo><ul id='en4od'></ul>
  1. <tfoot id='en4od'></tfoot>
    1. <small id='en4od'></small><noframes id='en4od'>

    2. <i id='en4od'><tr id='en4od'><dt id='en4od'><q id='en4od'><span id='en4od'><b id='en4od'><form id='en4od'><ins id='en4od'></ins><ul id='en4od'></ul><sub id='en4od'></sub></form><legend id='en4od'></legend><bdo id='en4od'><pre id='en4od'><center id='en4od'></center></pre></bdo></b><th id='en4od'></th></span></q></dt></tr></i><div id='en4od'><tfoot id='en4od'></tfoot><dl id='en4od'><fieldset id='en4od'></fieldset></dl></div>

      <legend id='en4od'><style id='en4od'><dir id='en4od'><q id='en4od'></q></dir></style></legend>

      将 Backbone.js 模型插入 MySQL 数据库

      时间:2023-10-02

        • <i id='48xp7'><tr id='48xp7'><dt id='48xp7'><q id='48xp7'><span id='48xp7'><b id='48xp7'><form id='48xp7'><ins id='48xp7'></ins><ul id='48xp7'></ul><sub id='48xp7'></sub></form><legend id='48xp7'></legend><bdo id='48xp7'><pre id='48xp7'><center id='48xp7'></center></pre></bdo></b><th id='48xp7'></th></span></q></dt></tr></i><div id='48xp7'><tfoot id='48xp7'></tfoot><dl id='48xp7'><fieldset id='48xp7'></fieldset></dl></div>

              <bdo id='48xp7'></bdo><ul id='48xp7'></ul>

                <tfoot id='48xp7'></tfoot>
              • <small id='48xp7'></small><noframes id='48xp7'>

                <legend id='48xp7'><style id='48xp7'><dir id='48xp7'><q id='48xp7'></q></dir></style></legend>
                  <tbody id='48xp7'></tbody>
              • 本文介绍了将 Backbone.js 模型插入 MySQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个带有一些默认值和一个 url 的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!";
                

                我试过阅读很多教程,但我无法将这个简单的模型保存起来.为什么我的代码不起作用?关于如何解决这个问题的任何想法?

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

                这样一来,就会在boxes"表中插入一行,其中包含主干模型Box的每个属性的信息.本例中的服务器请求方式为POST,表boxes"中的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.

                推荐答案

                Backbone 基于 REST API:当将模型保存/更新到服务器时,Backbone 会将其序列化为 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,并在内容类型为 application/json 的 HTTP 正文中发送.

                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

                • 确定请求的类型
                • 解码序列化的 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 实现 http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

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

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

                上一篇:一个 RESTful 持久性解决方案,可用于 Backbone.js. 下一篇:Backbone.js 如何与 PHP 一起使用

                相关文章

                最新文章

                • <bdo id='ZAGnz'></bdo><ul id='ZAGnz'></ul>

                <small id='ZAGnz'></small><noframes id='ZAGnz'>

                <legend id='ZAGnz'><style id='ZAGnz'><dir id='ZAGnz'><q id='ZAGnz'></q></dir></style></legend>
                  <i id='ZAGnz'><tr id='ZAGnz'><dt id='ZAGnz'><q id='ZAGnz'><span id='ZAGnz'><b id='ZAGnz'><form id='ZAGnz'><ins id='ZAGnz'></ins><ul id='ZAGnz'></ul><sub id='ZAGnz'></sub></form><legend id='ZAGnz'></legend><bdo id='ZAGnz'><pre id='ZAGnz'><center id='ZAGnz'></center></pre></bdo></b><th id='ZAGnz'></th></span></q></dt></tr></i><div id='ZAGnz'><tfoot id='ZAGnz'></tfoot><dl id='ZAGnz'><fieldset id='ZAGnz'></fieldset></dl></div>

                  1. <tfoot id='ZAGnz'></tfoot>