Bonjour,

J'ai un petit code pour insérer des données en type array dans un champs type json.

Structure de la table, le champs concerné est restaurant_order:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
describe restaurant_list;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int unsigned | NO   | PRI | NULL    | auto_increment |
| restaurant_name  | varchar(50)  | NO   |     | NULL    |                |
| timing           | json         | NO   |     | NULL    |                |
| meals            | json         | NO   |     | NULL    |                |
| restaurant_order | json         | NO   |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
5 rows in set (0.10 sec)

Pour insérer ces données, j'utilise une class PDO, voici un petit morceau de la class:
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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
 
public function jsonArray($table, $item, $values, $where = null)
{
    // i.e of updating data which works
 
    //UPDATE restaurant_list SET restaurant_order = JSON_ARRAY('1', '2') WHERE `id` = 1;
 
    $values   = ':'.$values;
 
    $sql = 'UPDATE '.$table.' SET '.$item.' = JSON_ARRAY('.$values.')';
 
    if ($where != null) {
        $sql .= ' WHERE ' . $where;
    }
 
    $this->sql    = $sql;
    return $this->sql;
}
 
/*** function to prepare query Start ***/
public function prepare()
{
    return $this->stmt = $this->dbh->prepare($this->sql);
}
/*** function to prepare query EnD ***/
 
/*** function to bind query Start ***/
public function bind($param, $value, $type = null)
{
    if (is_null($type)) :
        switch (true):
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
 
            case is_bool($value):
                $type = PDO::PARAM_BOOL;    
                break;
 
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
 
            case is_string($value):
                $type = PDO::PARAM_STR;
                break;
 
            default: // array
                $type = null;
                //echo "string 44444<br>";
 
        endswitch; // end switch (true):
    endif; // end if (is_null($type)):
 
    if(is_array($value)):
 
 
        $value  = $value;
    else:
        $value = $this->strSafe($value);
    endif;
 
    $this->stmt->bindParam($param, $value, $type);
}
 
/*** function to bind query EnD ***/
 
/*** function to execute query Start ***/
public function execute()
{
    return $this->stmt->execute();
}
/*** function to execute query EnD ***/
 
/*** function to execute and fetch Start ***/
public function resultset()
{
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
/*** function to execute and fetch EnD ***/
 
public function strSafe($string)
{
    $string = trim(strip_tags(($string)));
    $string = preg_replace('/\s+/', ' ', $string); // remove more than one space
    $this->string    = $string;
 
    return $this->string;
}
Le code d'insertion est:
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
 
require_once 'crud.php';
// initialize the connection class
$dbh    = new Database();
 
$p_cat      = [1,5];
 
$upOn   = $dbh->jsonArray('restaurant_list', 'restaurant_order', 'value', 'id = :id');
 
    $dbh->prepare($upOn);
 
    foreach($p_cat as $pc):
    $dbh->bind('value', implode('","', $p_cat));
    endforeach;
 
 
    $dbh->bind('id', 1);
 
    $dbh->execute();
Quand j'insère les données, je reçois ce résultat:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
SELECT restaurant_order FROM restaurant_list;
+----------------------+
| restaurant_order     |
+----------------------+
| ["1\",\"5"]          | ===> Le nouveau champs
| ["1", "2", "3", "4"] |
| ["1", "2", "4"]      |
+----------------------+
3 rows in set (0.00 sec)
* Comment puis-je mettre à jour les données pour avoir un résultat similaire à ["1","5"]?
* Est ce que cette méthode est sécurisée? Sinon, y a-t il un autre moyen pour effectuer cette opération?

Merci à vous