Domanda

I have found answers all over the place on how to INSERT or UPDATE if record already exists, a good approach is using the on duplicate key update or REPLACE, but I can't seem to find anywhere how to do this using a prepared statement. Is this possible?

EDIT:

Ok let me be more specific with my question:

If I have a mysqli prepared statement like this example:

if ($stmt = $mysqli->prepare("UPDATE test SET name= ?, age= ? WHERE iduser= ?")) {    
    $stmt->bind_param('ssi', $name, $age, $id);
    $stmt->execute();
}

In this case how can I build a on duplicate key update This is were Im stuck, I dont know how to use the "?" :

if ($stmt = $mysqli->prepare("INSERT INTO test (iduser, name, age) VALUES (?,?,?))){
ON DUPLICATE KEY UPDATE name=?,age=?")) {    
    $stmt->bind_param('iss', $id, $name, $age);
    $stmt->execute();
}
È stato utile?

Soluzione

I may be missing something, but can you not just update the second bindParam to: $stmt->bind_param('issss', $id, $name, $age, $name, $age)?

If you don't know how the question marks work and are just copy/pasting code examples, I suggest you read up on mysqli or look into PDO.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top