mysqli更新数据库

I want user to be able to update his info eg if he had entered at the begining phone1, phone2, phone3 to be able to change them but if he leaves phone1 empty I want to keep his 1st entry.

until now I was using x3 times (one for each entry) this code:

if(empty($_POST['phone1'])){
    $phone1='';
}else if(
    $stmt = $mysqli->prepare('UPDATE register SET Phone_No=? WHERE E_Mail=?'){
    $stmt->bind_param("ss", $phone1, $email); 
    $stmt->execute();
    $stmt->close();
}
//same for phone 2
//same for phone 3

Is there a way to compine these codes so I only write once the mysqli->prepare('update') and just update only the fields that the user wanted?

Thank you

Well it depends on what you mean by entry, the first of the three post values or the previous record on the database?!

lets say the user provides only two values

$_POST['phone1'] = 34234034234;
$_POST['phone2'] = 32423423423;

you could try something like this

$values = array_intersect_key( $_POST, array_flip( array('phone1','phone2','phone3') ) );

$query = 'update register set';

if ( isset($values['phone1']) ) $query.= ' Phone1_No = ?,';
if ( isset($values['phone2']) ) $query.= ' Phone2_No = ?,';
if ( isset($values['phone3']) ) $query.= ' Phone3_No = ?,';

$query = rtrim($query, ',') . ' where E_Mail = ?';

$stmt = $mysqli->prepare($query);

array_unshift($values, str_repeat('s', count($values) ) );

array_push($values, $_POST['email']); 

call_user_func_array( array($stmt, 'bind_param'), $values ); 

if ( !$stmt->execute() )
{
    // some error
}

$stmt->close();

or stick with your old solution and just make it tidier while mantaining the same logic and adding simple loop from 1 to 3:

for ($i=1; $i < 3; ++$i)
{ 
    if ( !empty($_POST["phone{$i}"]) )
    {
        $stmt = $mysqli->prepare("update register set Phone{$i}_No = ? where E_Mail = ?");

        $stmt->bind_param('s', $_POST["phone{$i}"]); 

        $stmt->execute();

        $stmt->close();
    }
}