I am trying to create a page where a registered user can go, view the information from their profile, and update that information if necessary. I've started putting something together however it's inefficient in my opinion. Here's what I'm trying to figure out:

  1. Is there a way to check if a change has occurred in each field before it updates the DB? It seems to me that there's no need to update everything when maybe only 1-2 items has changed.
  2. I have a state drop down box which is created from the DB. I currently have it set to select the state the user registered with - but it's creating a duplicate value in the list. For example, let's say I registered in VA, then I go to update my profile. The state selected is VA but then on the drop down list VA appears a second time. Is there anyway to prevent this duplication? I enclosed the code I'm using below.
  3. Is there a better way to store User Variables then in Sessions? Currently I am setting the following variables in the Session: First Name (FName), User ID (UserID), Username (Username).
<?PHP

include 'dbconnection.php';

$query = "Select Code,Description FROM cd_states";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
 
	echo("<option select value =\"$Array[7]\">$Array[8]</option>");
 
	if (mysql_num_rows($result) > 0) { 
		 while ($row = mysql_fetch_row($result)) {
			echo("<option value=\"$row[0]\">$row[1]</option>");
		 }
	}
	else { 
      echo "No rows found!"; 
	} 
	mysql_free_result($result);
?>

In answer to your questions

1. If you are using one sql statement to make the update then the efficiency should be the same no matter how many fields are getting updated. It all depends on the number of sql calls you are making to the database. If you are using multiple sql statements to update every field then you will have to reduce it to one. If you are intent on making the check, then you will probably have to make another sql call the DB and check what's changed. That would make 2 sql calls, one to check the old data and one to update the new data. This would not only increase the lines of code but would also increase server traffic due to the second sql call.

2. In order to keep your state and prevent it from duplicating... Use the html selected="selected" attribute in the following manner.

In your while loop, check

if ($row[0]==$Array[7]){
  $s="selected='selected'";
} else {
  $s="";
}
echo("<option value=\"$row[0]\" $s >$row[1]</option>");

The above checks if the values are equal and sets that as the selected state.

3. I'm not sure here if there is a better way to store data which is going to be picked up on all pages. You could use cookies but I prefer Session variables. Not sure which is more efficient. Maybe someone else could help us out here.

In answer to your questions

1. If you are using one sql statement to make the update then the efficiency should be the same no matter how many fields are getting updated. It all depends on the number of sql calls you are making to the database. If you are using multiple sql statements to update every field then you will have to reduce it to one. If you are intent on making the check, then you will probably have to make another sql call the DB and check what's changed. That would make 2 sql calls, one to check the old data and one to update the new data. This would not only increase the lines of code but would also increase server traffic due to the second sql call.

2. In order to keep your state and prevent it from duplicating... Use the html selected="selected" attribute in the following manner.

In your while loop, check

if ($row[0]==$Array[7]){
  $s="selected='selected'";
} else {
  $s="";
}
echo("<option value=\"$row[0]\" $s >$row[1]</option>");

The above checks if the values are equal and sets that as the selected state.

3. I'm not sure here if there is a better way to store data which is going to be picked up on all pages. You could use cookies but I prefer Session variables. Not sure which is more efficient. Maybe someone else could help us out here.

Code worked beautifully and thank you for the advice on #1. As for #3, everything is being stored currently in Sessions, I just didn't know if there was some advantage to using that over something else (e.g. cookies). I believe I read somewhere that cookies can store values longer but that sessions may be more secure since they timeout. I'm not 100% sure either way, so any guidance would be much appreciated! :-/

Member Avatar for diafol

I'd use sessions, but they are just glorified cookies unless they're implemented via DB. If cookies turned off, sessions won't work either.

Have a search for security and sessions in Google, you'll get some good advice. Secure is a relative term!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.