How to do a proper UPDATE of the data on php website?

There are two rows of ""name,"name1" if you want to update only name and 1 name is left blank , then the database updates the column name1 is empty. How to update only necessary input if it is not blank ?
068480a3a56f440a8779415dfcb25b74.PNG41995fde84b24719abb93930e88ef1aa.PNG
July 4th 19 at 23:09
3 answers
July 4th 19 at 23:11
Solution
  1. You got a SQL injection is scattered many
  2. Something like this:
    $updatingFields = [];
    
    foreach (['name', 'familia'] as $field) {
     if (isset($_POST[$field]) && $fieldValue = trim($_POST[$field])) {
     $updatingFields[] = sprintf("%s = '%s'", $field, $link->real_escape_string($fieldValue));
    }
    }
    
    $result = null;
    if (count($updatingFields)) {
     $query = sprintf(
    "
     UPDATE users
     SET %s
     WHERE id = '%s'
    ",
     implode(', ', $updatingFields),
    $link->real_escape_string($id)
    );
    
     $result = $link->query($query);
    }

  3. Find out what are prepared statements and PDO or any other Database abstraction layer
  4. Find out what is query builder
Thank you! - Pearline92 commented on July 4th 19 at 23:14
July 4th 19 at 23:13
Solution
get an array of field names and then go out and post foreach'em and check whether there is a key in the array of fields and whether the empty value in the post on the logic fills in
Thank you! - Pearline92 commented on July 4th 19 at 23:16
July 4th 19 at 23:15
The question is unclear. In provedennom your screen even no variable name1
Please specify what you mean?
I apologize if realized . - Pearline92 commented on July 4th 19 at 23:18

Find more questions by tags PHP