1

A new Datatype Introduced in mysql which is JSON.

Text

now when I get Raw data from header:

$_data = json_decode(file_get_contents("php://input"),true);

Result of $data

Array
(
[lines] => Array
    (
        [0] => Array
            (
                [id] => 8
                [name] => Panadol Extra
                [qty] => 1
                [price] => 11.00
                [total] => 11.00
            )

        [1] => Array
            (
                [id] => 6
                [name] => Panadol Simple
                [qty] => 1
                [price] => 5.00
                [total] => 5.00
            )

        [2] => Array
            (
                [id] => 2
                [name] => Panadol
                [qty] => 1
                [price] => 1.00
                [total] => 1.00
            )

        [3] => Array
            (
                [id] => 1
                [name] => Panadol Extra
                [qty] => 1
                [price] => 1.00
                [total] => 1.00
            )

    )

[discount] => 0
[date] => 2019-11-22T16:31:21+05:00
[paid] => 18
[user_id] => 1
[serial] => 35

)

am getting this error when i store data in mysql

Invalid Query!Invalid JSON text: "Invalid value." at position 0 in value for column 'sales.Invoice'.

here is my query:

require_once 'config/config.php';
$_json = json_decode(file_get_contents("php://input"),true);

echo "<pre>";
print_r($_json);
exit();

$query = "INSERT INTO `sales` 
(`UserId`,`invoice`,`Discount`,`Paid`,`Serial`) 
Values('','','','','');";

$result = $db->query($query);
if ($result)
echo "Data Inserted Successfully!";
else
echo "Invalid Query!".$db->error;

i need to store discount,paid,user_id,serial and lines(json format) into table.help me to store that data into sales. how do i convert lines into json format. i can store other object discount paid serial using extract function but unbale to convert lines data into json format

4
  • it's not clear what you are asking. the screenshot is not useful other than to orient us that you are using phpmyadmin and looking at a fieldtype of json. The $_data array is useful; however, what happens between php://input) and the error is required to understand what is going awry. Please at least edit the question and show the code that is trying to submit to the Invoice table. Commented Nov 22, 2019 at 11:00
  • can you help me remotely Commented Nov 22, 2019 at 11:16
  • edit your post. take down the image, replace it with one that shows your column names. post the section of the code that takes $_data and creates an insert query. Commented Nov 22, 2019 at 11:19
  • I have updated question more specifically Commented Nov 22, 2019 at 11:40

2 Answers 2

3

You are decoding the JSON so it's creating a problem. Don't decode it directly store the JSON variable

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

Comments

0

Here is the Solution i found.

$_json = json_decode(file_get_contents("php://input"),true);
extract($_json);

$invoice = json_encode($lines);

$query = "INSERT INTO `sales` (`UserId`,`invoice`,`Discount`,`Paid`,`Serial`) 
Values('$user_id','$invoice','$discount','$paid','$serial');";

$result = $db->query($query);

Comments

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.