hi all,
i had a users page in which the Admin can add users.so while adding a user i am able to restrict the duplicate entries(same names).Those users will be displayed in a table, and there is a edit button, so Admin can edit users account(like active,inactive etc). here is my problem when i click on edit button a pop up wil open and i didnt change any thing and click update button, as i didnt change anything it should update but i am getting a popup as user already exist. so can any one...
Thanks in advance..

-----update.php-------------

<html>
<body>
<?php
$id=$_GET['f1'];
include 'connection.php';

$query="select * from users where userid='$id'";
mysql_error();
$result = mysql_query ($query) ;
$num=mysql_num_rows($result);
$i=0;
While($i < $num)
{
$userid=mysql_result($result,$i,"userid");
$sqlusername=mysql_result($result,$i,"username");
$password=mysql_result($result,$i,'password');
$role=mysql_result($result,$i,"role");
$status=mysql_result($result,$i,'status');
$allocatedmemory=mysql_result($result,$i,"allocatedmemory");
$arr_id=split("[{ }-]" ,$allocatedmemory);
$i++;
}
?>
<form action="update.php" method="post">
<div>
<center>
<table>
	<tr>
		<td style="color: navy;">Userid</td>
		<td><input type="text" readonly="readonly" name="userid" value="<? echo $userid; ?>"></td>
	</tr>
	<tr>
		<td style="color: navy;">Username</td>
		<td><input type="text" name="username" id="txtname" value="<? echo $sqlusername; ?>"></td>
	</tr>
	<tr>
		<td style="color: navy;">Password</td>
		<td><input type="password" name="password" id="pwd" value="<? echo $password; ?>"></td>
	</tr>
		<tr>
		<td style="color: navy;">Status:</td>
		<td><select name="status">
		<?php
		if($status == 1)
		{
			echo '<option value="Active" selected="selected">Active</option>';
			echo '<option value="Inactive">Inactive</option>';
		}
		else
		{
			echo '<option value="Inactive" selected="selected">Inactive</option>';
			echo '<option value="Active">Active</option>';
		}
		?>
		</select></td></tr>
        <tr>
        <td style="color: navy;">Allocatedmemory</td>
        <td><input type="text" onkeypress="return isNumberKey(event)" id="am" name="allocatedmemory" value="<? echo $arr_id[0]; ?>"></td>
        <td><select name="size">
        <?php
           if($arr_id[1]== "KB")
           {
                echo '<option value="MB">MB</option>';
                echo '<option value="KB" selected="selected">KB</option>';
           }
           else
           {
                   echo '<option value="MB" selected="selected">MB</option>';
                echo '<option value="KB">KB</option>';

           }?>
        </select>
        </td>
    </tr>
		<tr>
	        <td></td>
	   
	    <td><input type="submit" name="btn" value="Update" onclick="return (confirm('Are you sure you want to Update this?'),check())"/>
	    <input type="submit" name="btn" value="Delete" onclick="return (confirm('Are you sure you want to delete this?'))"/></td>
	</tr>
	</table>    
</center>
</div>
</form>
</body>
</html>
<?php
 $user=$sqlusername;
echo $user;
 if(isset($_POST['btn'])) 
{
    include 'connection.php';
     if($_POST[btn]== 'Update')
 {
      $pname=$_POST['username']; 
     if($_POST[status] == 'Active')
        $_POST[status] = 1;
    else{
            $_POST[status] = 0;
        }
       echo $pname;
       if($user==$pname)
     {
         mysql_query("UPDATE users SET username = '$_POST[username]',password = '$_POST[password]', role = '1', status = $_POST[status], allocatedmemory = '$_POST[allocatedmemory] $_POST[size]' WHERE userid = '$_POST[userid]'");
         echo "<script language='javascript'>
                alert('User details updated');
                </script>";
                echo "<script language='javascript' type='text/javascript'>
                window.close();
                </script>";
     }
     else{
            $query="select * from users where username='$pname'";
            mysql_error();  
            $result=mysql_query($query);
            $count=mysql_num_rows($result);
            if($count==0)
            {
                mysql_query("UPDATE users SET username = '$_POST[username]',password = '$_POST[password]', role = '1', status = $_POST[status], allocatedmemory = '$_POST[allocatedmemory] $_POST[size]' WHERE userid = '$_POST[userid]'");
            }
            else
            {
                echo "<script language='javascript'>
                    alert('Username already exists');
                    </script>";
            }
         }
 }
 else if($_POST[btn] == 'Delete')
  {
        mysql_query("Delete from users where userid ='$_POST[userid]'");
        echo "<script language='javascript'>
              alert('Data Deleted');
              </script>";
        echo "<script language='javascript' type='text/javascript'>
               window.close();
               </script>";        
  }
} 
 ?>
Member Avatar for rajarajan2017

I am not able to view the long code, but whatever it is if the sql query return the count > 0 then it always return "user already exits". Check whether the name updated twice in your databse?

Thanks for ur response..
ok then i can make it clear ..
see the line no.34,88,95 and 102 i think it wil be clear for u if u see these lines u can understand my idea.

Member Avatar for rajarajan2017

Then 102 line is not satisfied the condition. So plz echo the values of $user & $pname before the condition, then you come to know the inequalent values are meet. One more thing what would be your result of $user, because an while loop is running while you retrieving from the database for the first time then what would be result of $user?

Thanks for ur response.
this $user is the value present in username textbox before the update button click. and $pname is the value present in username textbox after the update button click.

i came to know that i am not getting the value of $user on line 102. so because of that i am getting error.

Member Avatar for rajarajan2017

Thats why I asked, what was the value echoed by $user and $pname

While($i < $num)
{
$userid=mysql_result($result,$i,"userid");
$sqlusername=mysql_result($result,$i,"username");
[B]//Check it here itself by echo $sqluesrname[/B]
$password=mysql_result($result,$i,'password');
$role=mysql_result($result,$i,"role");
$status=mysql_result($result,$i,'status');
$allocatedmemory=mysql_result($result,$i,"allocatedmemory");
$arr_id=split("[{ }-]" ,$allocatedmemory);
$i++;
}

i had done that. the value is coming. On line 89 also i am getting the value but after that i am not getting the value of $user after line 90

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.