0

I have a problem and I don't know how to solve it. I want to transfer a json to another table and I get a syntax error.

This is my output

INSERT INTO saved_cmd ('id_user','value','store','totalPrice','hour','type_payement') VALUES ('11','"{\"lenght\":0,\"produits\":[{\"id\":29,\"name\":\"Tarte au fraise\",\"count\":1,\"price\":2,\"totalPrice\":2},{\"id\":28,\"name\":\"rose des sables\",\"count\":0,\"price\":2,\"totalPrice\":0}]}"','6','2.00','13:00','caisse')


===================================================


Error: INSERT INTO saved_cmd ('id_user','value','store','totalPrice','hour','type_payement') VALUES ('11','"{\"lenght\":0,\"produits\":[{\"id\":29,\"name\":\"Tarte au fraise\",\"count\":1,\"price\":2,\"totalPrice\":2},{\"id\":28,\"name\":\"rose des sables\",\"count\":0,\"price\":2,\"totalPrice\":0}]}"','6','2.00','13:00','caisse')<br>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id_user','value','store','totalPrice','hour','type_payement') VALUES ('11','"{\' at line 1

I tried to encode my json but i have the same problem

and this is my php

 $state = $row['states'];
                $id_user = $row['id_user'];
                $value = ($row['value']);
                $panier = json_encode($value);
                $store = $row['store'];
                $totalPrice = $row['totalPrice'];
                $hour = $row['hour'];
                $type_payement = $row['type'];
                if ($row['states'] != 4) {
                    $states = $state + 1;
                    $sql = "UPDATE cmd SET states = $states WHERE id = '$id'";
                    if ($conn->query($sql)) {
                        echo "good:up";
                    }
                } else {
                    $sql = "INSERT INTO saved_cmd ('id_user','value','store','totalPrice','hour','type_payement') VALUES ('$id_user','$panier','$store','$totalPrice','$hour','$type_payement')";
                    echo $sql . "\n\n\n===================================================\n\n\n";
                    if ($conn->query($sql) === true) {
                        echo "good:save";
                    } else {
                        echo "Error: " . $sql . "<br>" . $conn->error;
                    }
                }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Does this answer your question? [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – sticky bit Mar 05 '21 at 12:04
  • Did you check id_user type? Also, there is no need to add quotation before { and after } – iazaran Mar 19 '21 at 21:08

0 Answers0