473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Updating existing records from HTML GET form

I'm just rewriting some backend management pages, in fact rewriting the
whole database too. One of these pages has a simple HTML form to edit
existing rows. I don't want to populate the form beforehand with all
existing data. I just want to check which of the 20 fields have changed and
update the data accordingly.

Before (I wrote the script years ago, forgive me!), I just had a check
whether any data in each field and each had its own update query .. i.e.

if ($email) {$res=mysql_query("update $table set email='$email' where
id='$id' ");}
if ($contact) {$res=mysql_query("update $table set contact='$contact'
where id='$id' ");}

Now, I must admit that this would still work, but there must be a much
tidier way of doing this ... any pointers appreciated ..

Nick


Jul 23 '05 #1
5 1755

"elyob" <ne*********@gmail.com> wrote in message
news:DN****************@text.news.blueyonder.co.uk ...
I'm just rewriting some backend management pages, in fact rewriting the
whole database too. One of these pages has a simple HTML form to edit
existing rows. I don't want to populate the form beforehand with all
existing data. I just want to check which of the 20 fields have changed
and update the data accordingly.

Before (I wrote the script years ago, forgive me!), I just had a check
whether any data in each field and each had its own update query .. i.e.

if ($email) {$res=mysql_query("update $table set email='$email'
where id='$id' ");}
if ($contact) {$res=mysql_query("update $table set contact='$contact'
where id='$id' ");}

Now, I must admit that this would still work, but there must be a much
tidier way of doing this ... any pointers appreciated ..


Guess I should also mention I'm using PHP. I presume I should be taking the
information into an array, but not sure really .. as it's nearly 2am and I
should really be in bed.

Jul 23 '05 #2
elyob wrote:
if ($email) {$res=mysql_query("update $table set email='$email' where
id='$id' ");}
if ($contact) {$res=mysql_query("update $table set contact='$contact'
where id='$id' ");}

Now, I must admit that this would still work, but there must be a much
tidier way of doing this ... any pointers appreciated ..


Here's a trick...

Update is a no-op when you set a field to the same value it had before.
So you could skip the "if" logic, and just execute the update
regardless. Only execute one update statement, with all fields
corresponding to your web form parameters.

update $table set email = '$email', contact = '$contact', etc...
where id = '$id'

Regards,
Bill K.
Jul 23 '05 #3
Bill Karwin wrote:
elyob wrote:
if ($email) {$res=mysql_query("update $table set email='$email'
where id='$id' ");}
if ($contact) {$res=mysql_query("update $table set
contact='$contact' where id='$id' ");}

Now, I must admit that this would still work, but there must be a much
tidier way of doing this ... any pointers appreciated ..

Here's a trick...

Update is a no-op when you set a field to the same value it had before.
So you could skip the "if" logic, and just execute the update
regardless. Only execute one update statement, with all fields
corresponding to your web form parameters.

update $table set email = '$email', contact = '$contact', etc...
where id = '$id'

Regards,
Bill K.


Hi Bill,

That'd be the most straightforward way of doing it, although in original
post I mentioned I don't want to "populate the form beforehand with all
existing data". This is because the form is multi-functional, and does
all the inserts, updates and deletes from the databases. It's a simple
script and the only way I can think of getting around this is by using a
mysql enabled javascript call. i.e. enter the id, then choose <update>
which will populate the form.

Alternatively I was thinking of creating the mysql query on the fly,
however I can still see tons of IF statements. If this is the case, then
it'd be more straightforward to use my previous IF method as it is even
more straightforward.

Cheers

Nick
Jul 23 '05 #4
elyob wrote:
Alternatively I was thinking of creating the mysql query on the fly,
however I can still see tons of IF statements. If this is the case, then
it'd be more straightforward to use my previous IF method as it is even
more straightforward.


I have used this method too. One variation would be for each form
input, if it's non-blank, then push an associative array element. At
least that way it's easy to add another form field to your application.

# Create an associative array of all form inputs
# if they are present and have a non-blank value.
$form_field = array();
if ($email) { $form_field['email'] = $email; }
if ($contact) { $form_field['contact'] = $contact; }
....same for other fields...

The rest is boilerplate, even if the set of form fields changes.

# Convert the associative array into an array
# of key = 'value' strings.
$set_field = array();
foreach( $form_field as $key => $value )
{
$set_field[] = "$key = '$value'";
}

# Implode the list into a comma-separated string.
$csv_set_string = implode(',', $set_field);

# Execute the sql update, if there is anything to change.
if ($csv_set_string)
{
$res = mysql_query("update $table set $csv_set_string where id = '$id'");
}

Regards,
Bill K.
Jul 23 '05 #5

"Bill Karwin" <bi**@karwin.com> wrote in message
news:d3********@enews1.newsguy.com...
elyob wrote:
Alternatively I was thinking of creating the mysql query on the fly,
however I can still see tons of IF statements. If this is the case, then
it'd be more straightforward to use my previous IF method as it is even
more straightforward.


I have used this method too. One variation would be for each form input,
if it's non-blank, then push an associative array element. At least that
way it's easy to add another form field to your application.

# Create an associative array of all form inputs
# if they are present and have a non-blank value.
$form_field = array();
if ($email) { $form_field['email'] = $email; }
if ($contact) { $form_field['contact'] = $contact; }
...same for other fields...

The rest is boilerplate, even if the set of form fields changes.

# Convert the associative array into an array
# of key = 'value' strings.
$set_field = array();
foreach( $form_field as $key => $value )
{
$set_field[] = "$key = '$value'";
}

# Implode the list into a comma-separated string.
$csv_set_string = implode(',', $set_field);

# Execute the sql update, if there is anything to change.
if ($csv_set_string)
{
$res = mysql_query("update $table set $csv_set_string where id =
'$id'");
}

Regards,
Bill K.


Regards Bill, but I had to get it sorted earlier. It really is a case of not
over compilating stuff isn't it!

if ($email) {$res=mysql_query("update $table set email='$email'
where id='$id' ");}
if ($contact) {$res=mysql_query("update $table set contact='$contact'
where id='$id' ");}

etc ...

The script is only a 10-100 times a day max manual script. KISS is sometimes
the best method! I did tidy it up for readability though ;)

Sometimes SQL is just purely simple! For 10,000+ calls a day I will rewrite
this stuff...!

Anyway, you are a very reliable member of this newsgroup, thanks Bill.

Regards

Nick

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason | last post by:
Let's say I have an html form with 20 or 30 fields in it. The form submits the fields via POST to a php page which updates a table in a database with the $_POST vars. Which makes more sense? ...
7
by: Paige | last post by:
I have a database on my local machine that I make entries and corrections on. I'd like to be able to upload that to my server and have that update the database that's on the server. What I've been...
2
by: Gene Vital | last post by:
Hi all. I am using Visual FoxPro via ODBC to update records on a 7.3.4 PostgreSql server and it appears that it always uses delete/insert instead of updating the current record. Can this be...
0
by: | last post by:
I am updating MS access tables with data in an xml document. I create two dataset, one for existing data and one for new data. I fill the first dataset with the records from MS Access, the second...
4
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a...
34
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have...
1
by: cover | last post by:
I'm trying to put together a system that upgrades records in a database and apparently have run into a bit of a glitch. I think the problem is with the $HTTP_POST_VARS portion of the code. Is...
6
by: Brian Blair | last post by:
I have created a input form that enters a number in a talble. If I open the form again it enters a new record instead of editing the existing record. It seems like it should be very basic but I...
2
tdw
by: tdw | last post by:
Hi all, I have several ideas on how to do this, but am having difficulty putting the ideas together and figuring out the most efficient way to do this. I have a database of survey coordinate...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.