rondog Posted July 21, 2009 Share Posted July 21, 2009 I am making an admin page where you can edit members information. Their is about 8 fields. When I hit save changes, how can I tell which data has changed? I want to avoid having to UPDATE a field if the value hasn't changed. Quote Link to comment https://forums.phpfreaks.com/topic/166842-solved-updating-form-data-in-a-db/ Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 compare the new values against the retrieved values: <?php //.. $row = mysql_fetch_assoc($result); //.. if (!empty($_POST)) { //validate if ($row['..'] !== $_POST['..']) { $set .= 'field = ' . $_POST['..']; } $query = "UPDATE table SET $set WHERE id = .."; //query.. } ?> <form action="" ..> <input type="text" name=".. " value="<?php print $row['..']; ?>" .. Quote Link to comment https://forums.phpfreaks.com/topic/166842-solved-updating-form-data-in-a-db/#findComment-879776 Share on other sites More sharing options...
rondog Posted July 21, 2009 Author Share Posted July 21, 2009 Ok I pretty much did exactly what you posted. Its not the cleanest, but it works..any suggestions? if (isset($_POST['save'])) { $id = $_GET['id']; $current = mysql_query("SELECT * FROM users WHERE id = '".$id."'")or die(mysql_error()); $current = mysql_fetch_array($current); $salutation = $_POST['salutation']; $firstname = mysql_real_escape_string($_POST['firstname']); $lastname = mysql_real_escape_string($_POST['lastname']); $email = mysql_real_escape_string($_POST['email']); $userid = mysql_real_escape_string($_POST['userid']); $active = $_POST['actives']; $level = $_POST['level']; $updates = array(); $status = ""; if ($current['salutation'] != $salutation) { array_push($updates,"salutation = '".$salutation."'"); } if ($current['firstname'] != $firstname) { array_push($updates,"firstname = '".$firstname."'"); } if ($current['lastname'] != $lastname) { array_push($updates,"lastname = '".$lastname."'"); } if ($current['email'] != $email) { $query = mysql_query("SELECT email FROM users WHERE email = '".$email."' AND id != '".$id."'") or die(mysql_error()); $num = mysql_num_rows($query); if ($num > 0) { $status .= "The email you chose is already in use.<br/>"; } else { array_push($updates,"email = '".$email."'"); } } if ($current['userid'] != $userid) { $query = mysql_query("SELECT userid FROM users WHERE userid = '".$userid."' AND id != '".$id."'") or die(mysql_error()); $num = mysql_num_rows($query); if ($num > 0) { $status .= "The user ID you chose is already in use."; } else { array_push($updates,"userid = '".$userid."'"); } } if ($current['active'] != $active) { array_push($updates,"active = '".$active."'"); } if ($current['level'] != $level) { array_push($updates,"level = '".$level."'"); } if ($status == "" && count($updates) != 0) { $query = mysql_query("UPDATE users SET ".implode(", ",$updates)." WHERE id = '".$id."'") or die(mysql_error()); $status .= "Successfully updated."; } } Quote Link to comment https://forums.phpfreaks.com/topic/166842-solved-updating-form-data-in-a-db/#findComment-879796 Share on other sites More sharing options...
ignace Posted July 21, 2009 Share Posted July 21, 2009 $current = mysql_query("SELECT * FROM users WHERE id = '".$id."'")or die(mysql_error()); $current = mysql_fetch_array($current); Must come before the if statement like $row in my example oterhwise you'll load it twice. Quote Link to comment https://forums.phpfreaks.com/topic/166842-solved-updating-form-data-in-a-db/#findComment-879805 Share on other sites More sharing options...
rondog Posted July 21, 2009 Author Share Posted July 21, 2009 Ahh gotcha..thanks a lot dude Quote Link to comment https://forums.phpfreaks.com/topic/166842-solved-updating-form-data-in-a-db/#findComment-879807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.