Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP / MySQL Update only changed fieds

Posted on 2010-08-13
6
Medium Priority
?
1,528 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
ID: 33434245
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
ID: 33434524
UPDATE Roster
SET Image = `$Picture`'
WHERE `Car_Number` = '$Car_Number'
and image != `$Picture`
0
 
LVL 8

Expert Comment

by:ropenner
ID: 33434695
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 8

Accepted Solution

by:
ropenner earned 2000 total points
ID: 33434700
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
ID: 33435066
ropenner - thank you - How does the where statement fit into that?
0
 
LVL 8

Assisted Solution

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

927 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