I am writing a php script the stores contact info in a database and then ranks the by the rank that i give them. I am having a problem when it comes to changing their ranks. Let me give you an example:
Joe Smith has a rank of 1
John Doing has a rank of 2
Beth Franks has a Rank of 3
Jim Johnson has a Rank of 4
Now what should happen is that if i want beth to have a rank of 1 it should bump down everyone elses score. Instead when i run it, it gives Beth a score of 1 and everyone else a score of 4. For the life of me i can't figure out why.
<?
$ud_id=$_POST['ud_id'];
$ud_name=$_POST['ud_name'];
$ud_address=$_POST['ud_address'];
$ud_city=$_POST['ud_city'];
$ud_state=$_POST['ud_state'];
$ud_phone=$_POST['ud_phone'];
$ud_rank=$_POST['ud_rank'];
$rank=$ud_rank; // copies the ud_rank to rank, rank is used in the while loop
$username="********";
$password="*********";
$database="*********";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to Connect to database");
$query="SELECT * FROM spots ORDER BY rank";
$result=mysql_query($query);
$num=mysql_numrows($result); //gets the number of rows in the table
$newrank=$rank; //copies the current records rank to newrank
$runs= ($num - $rank); //tells how many times the while loop needs to run
while ($runs > 0){
echo "Rank = $rank <br>";
$newrank=($newrank + 1) ; //adds one to the new rank
echo "New Rank =$newrank <br><br>";
$query="UPDATE spots SET rank='$newrank' WHERE rank='$rank'"; //stores the new values
@mysql_select_db($database) or die ("Unable to connect to Database");
mysql_query($query);
$rank=($rank + 1) ; //addes 1 to the newrank
$runs=($runs -1); //subtracts one to limit the rumbers of runs...
}
// tell mysql to update the changes
$query="UPDATE spots SET name='$ud_name', address='$ud_address', city='$ud_city', state='$ud_state', phone='$ud_phone', rank='$ud_rank' WHERE id='$ud_id'";
//do not forget this it is needed to connect to the data base
@mysql_select_db($database) or die ("Unable to conect to Database");
mysql_query($query);
echo "Record Updated";
mysql_close();
?>
I don't know if its necessary, but here is the code from the previous page where the information is entered in:
<?
$id=$_GET['id'];
$username="********";
$password="********";
$database="*************";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to Connect to database");
$query="SELECT * FROM spots ORDER BY rank";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num){
$id=mysql_result($result, $i, "id");
$name=mysql_result($result,$i,"name");
$address=mysql_result($result,$i,"address");
$city=mysql_result($result, $i, "city");
$state=mysql_result($result, $i, "state");
$phone=mysql_result($result, $i, "phone");
$rank=mysql_result($result, $i , "rank");
?>
<form action="updated.php" method="post">
<input type="text" name="ud_id" value="<? echo $id; ?>">
Name:<input type="text" name="ud_name" value="<? echo $name; ?>"><br>
Address:<input type="text" name="ud_address" value="<? echo $address; ?>"><br>
City:<input type="text" name="ud_state" value="<? echo $city; ?>"><br>
State: <input type="text" name="ud_state" value="<? echo $state; ?>"><br>
Phone: <input type="text" name="ud_phone" value="<? echo $phone; ?>"><br>
Rank: <input type="text" name="ud_rank" value="<? echo $rank;?>">
<input type="submit" value="Update">
</form>
<?
$i++;
}
?>
I'm sure the problem is something simple that i have overlooked. But for the live of me i can't figure out what the problem is.