-1
I have a problem. I am mounting a dynamic update, but I am not able to mount in the query the part of parameters to be updated. Follow the code below to be clear what I need:
function UpdateCommand($table, $params, $id){
$database = new Database();
$database->database_connect();
$select = "SELECT * FROM {$table}";
$resultselect = mysqli_query($database->database_connect(), $select);
// Aqui não consigo dar sequência
$query = "UPDATE {$table} SET {$params} WHERE id_{$table} = $id";
echo $query;
//$result = mysqli_query($database->database_connect(), $query);
$database->database_close_connection();
}
This update would be for any table, which would be informed in the parameter. The same would happen with the id. The problem was in the part of the fields to be updated. And two things made me difficult in this.
Each table can have a number of different columns, so I thought I’d mount a select to take these columns and then use them with the variables informed by the form, but I couldn’t do that.
2nd - Even if I correctly extract the columns of the table, how would I give a SET
in them for each column? It got a little confusing for me.
I forgot to put that I would need to do a check of the $_POST variables that were set. But this item here is less relevant, what matters most is the update.
Daniel, is there any other better way to do this for a function?
– DiChrist
There is no native function to do all these processes. Create your own as you already are. I just demonstrated how to do it. Simply adapt to the function you are creating.
– Daniel Omine