Re: [PHP-DB] Not updating certain fields in same row
[email protected] (Chris)
| Newsgroups | php.db |
|---|---|
| Message-ID | <[email protected]> |
Jason Pruim wrote:
> Hi everyone,
>
> I am attempting to update a record for a login system while leaving
> certain fields untouched if they arn't changed, and am running into issues.
>
> Basically what I want to do, is say I have these fields:
>
> Field1
> Field2
> Field3
> Field4
>
> I update Field1 and Field3 but not Field2 and Field4. What I want to do
> is change the values in Field1 and Field3 without touching the values in
> Field2 and Field4.
>
> I have tried this code:
> $tab = "\t";
> if (!isset($_POST['txtLoginName']) || empty($_POST['txtLoginName'])) {
>
> $loginName = mysqli_real_escape_string($chpwpostlink,
> $_POST['txtLoginName']);
> }
> else
> {
> $loginName = $tab;
> }
>
> which works the fields that I've changed, but if I don't submit a value
> in the form it sets the field to be blank in MySQL. Which is what I am
> trying to avoid. Any ideas?
$fields = array('Field1', 'Field2', 'Field3', 'Field4');
$update_fields = array();
foreach ($fields as $form_field_name) {
if (!isset($_POST[$form_field_name]) || empty($_POST[$form_field_name])
|| $_POST[$form_field_name] == '') {
// ignore the blank/empty fields
continue;
}
$update_fields[] = $form_field_name . "='" .
mysqli_real_escape_string($conn, $_POST[$form_field_name]) . "'";
}
if (!empty($update_fields)) {
$query = 'update table set ';
$query .= implode(',', $update_fields);
$query .= ' WHERE recordid='x';
}
If you're just ignoring password fields, you have a confirm password box
too don't you?
You can also use that:
$password_one = $_POST['password'];
$password_confirm = $_POST['password_confirm'];
if ($password_one == '' || $password_confirm == '') {
// skip adding the pw field to the update query
}
--
Postgresql & php tutorials
http://www.designmagick.com/