0

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);
?>

Edited by jrotunda85: Redacted Code &amp; Edited Question

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by diafol
1

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.

0

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! :-/

0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.