4

I'm just getting started with backbone.js and I'm working with this tutorial on 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' : '[email protected]'
   }
})

var u = new user()

And then I used the save method:

u.save(undefined, {url : 'inserts.php'})

Inserts.php contains:

<?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'");
?>

What's wrong with my code? It seems to be inserting in the database because whenever I call the save() method it inserts something on the user table but only in the password field.

3
  • Your insert query is wrong. You are doing an update query syntax with insert. INSERT INTO table (fields) VALUES (values) Commented Apr 15, 2012 at 7:00
  • That's an alternative syntax:) Commented Apr 15, 2012 at 7:04
  • try to echo $name. what do you get? Commented Apr 15, 2012 at 7:10

2 Answers 2

7

When you call the .save() method a JSON object is sent to the url you have specified; if the model is new, the POST method will be used. When the JSON is received in the PHP script you have to decode it and save the model attributes, let us say one field per attribute. Change this in the '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
    //...
Sign up to request clarification or add additional context in comments.

1 Comment

this works, but what if i want to fetch() instead of save(). how to retrieve data which are coming from fetch() request? any help would be appreciated. Thanks
0

In your PHP code in the line before $db->query, do this:

echo $name, '<br>', $password, '<br>', $email;

Make a traditional HTML form with name password email fields, and test the PHP script, making sure that the name password and email address you entered appears on the screen.

If this succeeds, your error is probably in your database query. For starters, it looks like your code is vulnerable to SQL injections. You will need to post some information about what DBMS you are using (MySQL, MSSQL, PostgreSQL etc). At any rate, the query looks wrong. Also post your CREATE TABLE command.

2 Comments

I'm using the alternative syntax for inserts. There's nothing wrong with the query, I've already tried executing it on the console.
If you had bothered to read my answer, you would understand that I was suggesting debugging the script before the query was even being executed, in case the data was not reaching that point...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.