Solved

PHP / MySQL Update only changed fieds

Posted on 2010-08-13
6
1,367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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!

 
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

Industry Leaders: 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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 …

690 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