Jump to content

[SOLVED] Updating form data in a db


rondog

Recommended Posts

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.

Link to comment
Share on other sites

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['..']; ?>" ..

Link to comment
Share on other sites

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.";
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.