Hello!
Can someone tell me how to write the counter correctly to add an array to the database?
I tried dozens of options - it does not work!
Essence: inside the $ array array (this is a brand) there will be a key ['models'] - and in it, in order, starting with [1], different models of machines are entered.

if ($ target == "add_model") {

$i=1;<br> 

// different info about $ @ _ POST;

 $far=array('model_name'=>$mod_name, 'opisanie'=>$opisanie);<br> $array = array('ааа'=>$asd);<br> $array['models']['$i++']=$far; // счетчик некорректно записан!<br> $json=(json_encode($array));<br> $sql = "UPDATE marki SET json='$json' WHERE id=$marka_id";<br> $res = mysqli_query($connect,$sql);<br> echo "<script>window.location='index.php'</script>";<br> 

}
?>

  • If $ array ['models'] is an array, can it be declared then? $ array ['models'] = array ()? And where does the base, if there is a numbering in the array? - nick_n_a
  • ['$i++'] well ++ surely it will never be executed inside quotes, why are they even write just [$i++] - Mike

1 answer 1

Thanks Mike!
Proper advice!
It turned out like this:

$ asd ['models'] [$ i ++] = $ far;
$ json = normJsonStr (json_encode ($ asd));
... $ res = mysqli_query ($ connect, $ sql) or die (mysqli_error ($ connect));
$ get = mysqli_fetch_assoc ($ res);
$ sql = "UPDATE marki SET json = '$ json' WHERE id = $ marka_id";
Entering into the database (json field) turned out as it should:
{"marka_name": "INFINITI", "models": {"1": {"model_name": "Aybek", "opisanie": "It's me", "marka_id": "55", "photo": "aibek .jpg "}}}
?>