我在php中创建了一个个人资料页面。该页面包括地址和电话字段,并提示用户插入其数据。然后将数据保存在我的名为“profile”的表中。 一切都很正常,但问题是只有当表已经包含数据时才更新表格。如何修改它(可能是我在函数中的mysql查询语句),以便即使为空也会输入数据到表中。是否有类似于UPDATE OR INSERT INTO语法的东西可以使用? 谢谢
<?php
if ( isset($_GET['success']) === true && empty($_GET['success'])===true ){
echo'profile updated sucessfuly';
}else{
if( empty($_POST) === false && empty($errors) === true ){
$update_data_profile = array(
'address' => $_POST['address'],
'telephone' => $_POST['telephone'],
);
update_user_profile($session_user_id, $update_data_profile);
header('Location: profile_update.php?success');
exit();
}else if ( empty($errors) === false ){
echo output_errors($errors);
}
?>
然后通过使用以下函数
function update_user_profile($user_id, $update_data_profile){
$update = array();
array_walk($update_data_profile, 'array_sanitize');
foreach($update_data_profile as $field => $data )
{
$update[]='`' . $field . '` = \'' . $data . '\'';
}
mysql_query(" UPDATE `profile` SET " . implode(', ', $update) . " WHERE `user_id` = $user_id ") or die(mysql_error());
}