Skip to content
Advertisement

How to insert values for the current session user using $stmt = $connect->prepare

This is my current code for adding some additional information to a database table named klantinfo. This information is added after the user is logged in. At the login, the given username gets inserted into the login table, but also into the klantinfo table. This way I can compare the username in the database with the username in the session, so I can display the correct data for the logged in user.

if (!$error) {
    
    
    $stmt = $connect->prepare("
        INSERT INTO klantinfo (voornaam, achternaam, telefoonnummer, straat, straatnummer, stad, postcode) 
        VALUES(?, ?, ?, ?, ?, ?, ?)");
    
    $stmt->bind_param("sssssss", $voornaam, $achternaam, $telnr, $straat, $straatnummer, $stad, $postcode);
    
    if($stmt->execute()) {
        
        $success_message = "De registratie is gelukt. U kunt nu inloggen.";
        
    } else {
        
        $error_message = "Error. Probeer het later opnieuw.";
        
    }
}

However, this just inserts the values into a new row in the database table. I want it to specifically insert the values into the already existing row of the user (the one with the username already in it) thats currently in the session.

I already tried other queries, with no luck.

For example, I tried adding this to the query:

WHERE username_info='$user'

Nothing works!

Advertisement

Answer

You should use an UPDATE statement instead of an INSERT statement. UPDATE changes the values in the database without inserting a new row whereas INSERT adds another row to the database. You can refer here: UPDATE(docs)

INSERT(docs)

INSERT

UPDATE

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement