0

What do I have wrong in the following code:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection made...";

//$payload_dump = $_POST['payload']
$payload = '{"device":"gabriel","data_type":"data","zone":1,"sample":4,"count":0,"time_stamp":"00:00"}'

$payload_array = json.decode($payload,true);

//get the data_payload details
$device = $payload_array['device'];
$type = $payload_array['data_type'];
$zone = $payload_array['zone'];
$sample = $payload_array['sample'];
$count = $payload_array['count'];
$time = $payload_array['time_stamp'];

$sql = "INSERT INTO data(device, data_type, zone, sample, count, time_stamp) VALUES('$device', '$type', '$zone', '$sample', '$count', '$time')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();
?>

I get the following error:

Parse error: syntax error, unexpected '$payload_array' (T_VARIABLE) in /Applications/XAMPP/xamppfiles/htdocs/insert_from_json.php on line 18

This PHP file receives the "payload" from a python file via JSON. The PHP file inserts that data into a table. $payload is a test variable to "simulate" the data coming in from the python code in the line above it. The payload could contain multiple rows. Maybe there is a better way to insert multiple rows than what I am attempting?

2
  • 1
    Syntax error json.decode should be json_decode Commented Aug 6, 2015 at 15:32
  • 1
    ; is missing at the end of 16th line as well Commented Aug 6, 2015 at 15:38

1 Answer 1

1

On line 18 you have the following:

$payload_array = json.decode($payload,true);

When it should be

$payload_array = json_decode($payload,true);

You used a period (.) instead of an underscore (_).

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

3 Comments

Thats what I said too.
I think we were doing it at the same time. I try to refresh the screen a few times before posting an answer to see if someone has already posted a decent one.
No sweat, was just having a laugh, not a dig

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.