Solved

PHP / MySQL Update only changed fieds

Posted on 2010-08-13
6
1,261 Views
Last Modified: 2013-12-13
How can I make this so that it only updates the fields that have changed? For instance if someone gets a new phone number or email address.

Thank you
$Car_Number = $_POST['Car_Number'];

$Driver_Name=$_POST['Driver_Name'];

$Home_Town=$_POST['Home_Town'];

$Cartoon_Character=$_POST['Cartoon_Character'];

$Highlights=$_POST['Highlights'];

$Email=$_POST['Email'];

$Phone=$_POST['Phone'];



$newname = $Car_Number.".JPG";

$foldername = "CarPics";

$Picture = '<img src="'.$foldername."/".$newname. '" width="260" height="195">';

move_uploaded_file($_FILES['mailfile']['tmp_name'], "../$foldername/$newname");

chmod ("../$foldername/$newname", 0644);



$sql = mysql_query("UPDATE Roster SET `Image` = `$Picture`, `Driver_Name` = '$Driver_Name',

        `Home_Town` = '$Home_Town', `Cartoon_Character` = '$Cartoon_Character', `Highlights` = '$Highlights',

        `Email` = '$Email', `Phone` = '$Phone' WHERE `Car_Number` = '$Car_Number'") or die(mysql_error());

Open in new window

0
Comment
Question by:patrickm12981
6 Comments
 
LVL 3

Expert Comment

by:dockhand
Comment Utility
I would create some variables to save the original values when you read the customer info and then create your "$sql" statement dynamically.  Something like:

$sql = 'mysql_query("UPDATE Roster SET `Image` = `$Picture`'
If $Origanal_Driver_Name != $Drive_Name {
   $sql &=  "`Driver_Name` = '$Driver_Name'"
}

etc.
0
 
LVL 2

Expert Comment

by:binghu
Comment Utility
UPDATE Roster
SET Image = `$Picture`'
WHERE `Car_Number` = '$Car_Number'
and image != `$Picture`
0
 
LVL 8

Expert Comment

by:ropenner
Comment Utility
The only way I can see that updating a field could be a problem is if your form you are submitting doesn't have the same values (blank perhaps) as in the database ... if that is the case you need to check each variable if it is empty first.

IF CHECKING for BLANK VALUES

foreach (array('Car_Number','Driver_Name','Home_Town','Cartoon_Character','Highlights','Email','Phone') as $field) {
      if ($_POST[$field]) { # checks if it has a value
            $sql = mysql_query("UPDATE Roster SET $field='$_POST[$field]'");
      }
}

OR


IF THE same values are in the input variables ... it doesn't matter because the database will look the same whether the update is done or not.  You don't save any time because to check each value if it has changed you'd have to do a query to fetch the current values.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Accepted Solution

by:
ropenner earned 500 total points
Comment Utility
oops error in syntax.

foreach (array('Car_Number','Driver_Name','Home_Town','Cartoon_Character','Highlights','Email','Phone') as $field) {
      if ($_POST[$field]) { # checks if it has a value
            $sql = mysql_query("UPDATE Roster SET $field='".$_POST[$field]."'");
      }
}
0
 

Author Comment

by:patrickm12981
Comment Utility
ropenner - thank you - How does the where statement fit into that?
0
 
LVL 8

Assisted Solution

by:ropenner
ropenner earned 500 total points
Comment Utility
oops....
mysql_query("UPDATE Roster SET $field={$_POST[$field]} WHERE `Car_Number` = '$Car_Number'");
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now