[Solved] PHP MySQL Json data saving [closed]


As ADyson said, you need to get the current state of that column, convert it to an array of objects, add the new object and then write that back to the database

// change the connection to use the MYSQLI extension, then you can freely update to PHP 8
$vtbaglan = new mysqli('localhost', 'userid', 'password', 'database name');

// get the current row from the DB
$sorgum = $vtbaglan->query($vtbaglan, "SELECT * FROM birlikler WHERE id = 1");
$sorgu  = $sorgum->fetch_assoc($sorgum);

$uyesim = json_decode($sorgu['uyeler']);

if(isset($_POST["verigir"])){
    $arr = [
        'kullanici' => $_POST["kullaniciadi"],
        'rutbe' => _POST["rutbe"],
        'karakteradi' => $_POST["kadi"]
    ];

    // add new data to the existing data
    $uyesim[] = $arr;

    // update it back to the database
    $stmt = $vtbaglan->prepare("UPDATE birlikler SET uyeler = ? WHERE id = 1");
    $stmt->bind_param('s', json_encode($uyesim));
    $stmt->execute();
    
}

// this will now show the new data, if you have added it, as the add code
// now happens before you re-present the page after the update

foreach($uyesim as $bubirtest){
    echo $bubirtest['kullanici'] . '<br>';
}
echo $uyebilgileri.'

<form method="post">
    <input type="text" name="kullaniciadi" placeholder="kullaniciadi"s>
    <input type="text" name="rutbe" placeholder="rutbe">
    <input type="text" name="kadi" placeholder="karakteradi">
    <button type="submit" name="verigir">verigir</button>
</form>';

See there is nothing frightening about using the mysqli_ OR PDO database access extension. These are also available in the old version of PHP you must be using, but now you can updgrade to newer version and this code should not need changing.

1

solved PHP MySQL Json data saving [closed]