1

I want send a JSON string over to a PHP file with some user info and then insert that info into a MySQL DB.

My HTML:

<!DOCTYPE html>
<head>
    <script src="script.js"></script>
</head>
<body>
    <form name="form1" action="" onSubmit="sub()">
        <input type="text" name="username" id="usrnm" />
        <input type="text" name="password" id="pswrd" />
        <input type="text" name="firstname" id="frstnm" />
        <input type="text" name="surname" id="surnm" />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

My Javascript in script.js:

function sub() {
        var un = document.getElementById("usrnm").value;
        var pw = document.getElementById("pswrd").value;
        var fn = document.getElementById("frstnm").value;
        var sn = document.getElementById("surnm").value;
        var jsonObj = {username:un, password:pw, firstName:fn, surName:sn};
        var xmlhttp = new XMLHttpRequest();     
        xmlhttp.open("POST","server.php" ,true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("data="+encodeURIComponent(JSON.stringify(jsonObj)));
    };

My PHP:

<?php 
    $con=mysqli_connect("localhost","root","", "somedatabase");
    $data = $_POST["data"];
    $res = json_decode($data, true);
    mysqli_query($con,"INSERT INTO sometable (username,password,firstname,surname) VALUES ($res[username],$res[password],$res[firstname],$res[surname])");
    mysqli_close($con);
?>

My PHP inserts a new row into the DB when I replace the values in the insert statement with normal string values but nothing gets inserted when I try to use the values from my PHP object.

3
  • try debbuging with print_r($_POST) or var_dump($_POST), check it with firefox/firebug or chrome/developer tools Commented Nov 19, 2013 at 22:04
  • Thanks, I will read up a bit on how to debug in PHP. Commented Nov 19, 2013 at 22:34
  • Using a form submission is not using AJAX Commented Nov 19, 2013 at 22:49

2 Answers 2

1

Make it like

VALUES (" . $res['username'] . "," . $res['password'] . "," . $res['firstname'] . "," . $res['surname']. ")"

Instead of directly in the string. PHP will not pick that up.

Sign up to request clarification or add additional context in comments.

2 Comments

Or: "VALUES ('{$res['username']}', '{$res['password']}', '{$res['firstname']}', '{$res['surname']}')" Also it's always a good idea to remember escaping data before persisting it to database.
Thanks guys, my problem was my syntax in the insert statement.
0

Arrays using text as keys should always be quoted. e.g. $array_name['key']

therefore try changing your query line to

mysqli_query($con,"INSERT INTO sometable (username,password,firstname,surname) VALUES ($res['username'],$res['password'],$res['firstname'],$res['surname'])");

If you still have issues try testing the result of $res

print_r($res);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.