Forum Moderators: coopster & phranque

Message Too Old, No Replies

Update Deleting info where no user input

Update Deleting info where no user input

         

typomaniac

9:22 pm on Sep 23, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



How can I prevent a column in a table from going blank if the user types nothing in the corresponding field in the form? Updating is
going great except for blank fields in the form resulting in blank columns in the table (erasing the info in the column where no user
input. I'm doing it like so:
my $dbh = DBI->connect('dbi:mysql:**************************') or die("Couldn't connect");
$dbh->do('UPDATE personnel SET fame=?, mame=?, lame=? WHERE id=?',
undef,
$_[3],
$_[4],
$_[5],
$_[0]
);

phranque

10:10 pm on Sep 23, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you can remove (not include) those columns whose fields are blank in the submitted form from the UPDATE statement.

e.g. if the fame and mame fields are blank, the constructed sql statement should look like:
UPDATE personnel SET lame='foo' WHERE id=1234

typomaniac

2:31 am on Sep 24, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi Phranque and thank you for responding.
I think I worded the message wrong-my apologies. I'm using a form for users to update profiles and when they update their password, the password updates. I've given complete freedom to change everything in profile fields and although the desired columns get updated, the field for the ones they did not desire to change are left blank. Say for instance, they didn't fill out the part of the form to change their email address, the server receives a blank field and Msql is updated with an empty field. I only want the fields updated fields in the html form to update and the empty fields on the form to not be effected.

phranque

2:44 am on Sep 24, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i understand your problem as initially posted.
you still must programmatically construct the UPDATE statement to include only those fields that should be updated.
your other option is to update those columns with the previously existing values from the db.
or prepopulate the form fields with values from the db...

[edited by: phranque at 9:27 am (utc) on Sep 24, 2022]

typomaniac

4:44 am on Sep 24, 2022 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for the wake up call. Just have to change the textarea fields to text fields on the form. Thanks again