Bonjour,

J'ai une class pour les opérations insert, update, delete, quand j'utilise la méthode JSON_INSERT, la requête génère une erreur:

Fatal error: Uncaught PDOException: SQLSTATE[22032]: <>: 3140 Invalid JSON text: "Missing a name for object member."
at position 1 in value for column 'products.p_description'. in /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php:182 Stack trace:
#0 /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php(182): PDOStatement->execute()
#1 /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/json_update.php(30): Database->execute()
#2 {main} thrown in /Library/WebServer/Documents/Tests/PHP/MySQL-JSON/CRUD.php on line 182
Le code du fichier CRUD.php:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public function update($table, $fields = [], $where = null)
{
    $args    = [];
    foreach ($fields as $fk => $fv) :
        $fk = $this->strSafe($fk);
        $args[] .= $fk . ' = :' . $fk;
    endforeach;
 
    $sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $args);
 
    if ($where != null) {
        $sql .= ' WHERE ' . $where;
    }
 
    $this->sql    = $sql;
}
 
public function execute()
{
    return $this->stmt->execute(); // LIGNE 182
}
Pour insérer un nouvel élément "chipset" dans le champs "p_description" avec la valeur "Qualcomm" dans le fichier json_update.php:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
require_once 'CRUD.php';
 
$upMemList = [
    'p_description' => "JSON_INSERT(`p_description` , '$.chipset' , 'Qualcomm')"
];
$upOn = $dbh->update('products', $upMemList, 'id = :id');
$dbh->prepare($upOn);
 
foreach ($upMemList as $fk => $fv) :
    $dbh->bind($fk, $fv);
endforeach;
$dbh->bind('id', 2);
$dbh->execute(); // LIGNE 30
Structure de la table:

Code SQL : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--
-- Table structure for table `products`
--
 
CREATE TABLE `products` (
  `id` int UNSIGNED NOT NULL,
  `p_name` varchar(50) DEFAULT NULL,
  `p_description` json DEFAULT NULL,
  `p_cat` int NOT NULL,
  `p_price` int NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
 
--
-- Dumping data for table `products`
--
 
INSERT INTO `products` (`id`, `p_name`, `p_description`, `p_cat`, `p_price`) VALUES
(1, 'TV1', '{\"p_usb\": \"2\", \"p_hdmi\": \"3\", \"chipset\": \"Qualcomm\", \"p_screen\": \"21\", \"resolution\": null}', 1, 3400),
(2, 'TV2', '{\"p_usb\": \"3\", \"p_hdmi\": \"3\", \"p_screen\": \"43\", \"resolution\": null}', 1, 4500),
(3, 'Computer1', '{\"p_usb\": \"2\", \"p_hdmi\": \"0\", \"chipset\": \"Qualcomm\", \"p_screen\": \"15\", \"resolution\": null}', 4, 7350),
(4, 'TV3', '{\"ports\": {\"p_usb\": \"3\", \"p_hdmi\": \"2\"}, \"p_screen\": \"65\", \"resolution\": null}', 1, 9900),
(5, 'TV4', '{\"ports\": {\"p_usb\": \"2\", \"p_hdmi\": \"2\"}, \"p_screen\": \"42\", \"resolution\": null}', 1, 3300);