Wer*_*eta 4 php mysql backbone.js
我刚刚开始使用backbone.js,我正在使用Nettuts上的这个教程.http://net.tutsplus.com/tutorials/javascript-ajax/getting-started-with-backbone-js/
var user = Backbone.Model.extend({
initialize: function(){
console.log('user was initialized!');
},
defaults:{
'name' : 'wern',
'full_name' : 'rem falkner',
'password' : 'secret',
'email' : 'secret@gmail.com'
}
})
var u = new user()
Run Code Online (Sandbox Code Playgroud)
然后我使用了save方法:
u.save(undefined, {url : 'inserts.php'})
Run Code Online (Sandbox Code Playgroud)
Inserts.php包含:
<?php
include('conn.php');
$name = $_POST['name'];
$password = md5($_POST['password']);
$email = $_POST['email'];
$db->query("INSERT INTO tbl_users SET user_id='$name', pword_hash='$password', full_name='$name', email='$email'");
?>
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?它似乎是插入数据库中,因为无论何时调用save()方法,它都会在用户表上插入一些内容,但只能在密码字段中插入.
当您调用.save()方法时,JSON对象将被发送到您指定的URL; 如果模型是新的,将使用POST方法.当在PHP脚本中收到JSON时,您必须对其进行解码并保存模型属性,让我们说出每个属性一个字段.在'inserts.php'中更改此内容:
<?php
include('conn.php');
$data = json_decode(file_get_contents('php://input'));
$name = $data->{'name'};
$password = md5($data->{'password'});
$email = $data->{'email'};
//proceed to add every variable to a field in the database
//...
Run Code Online (Sandbox Code Playgroud)