Solved

PHP / MySQL Update only changed fieds

Posted on 2010-08-13
6
1,315 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 8

Accepted Solution

by:
ropenner earned 500 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 500 total points
ID: 33435117
oops....
mysql_query("UPDATE Roster SET $field={$_POST[$field]} WHERE `Car_Number` = '$Car_Number'");
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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