By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

Restrict Duplicate entries

P: 18
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..
here is my code
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <?php
  4. $id=$_GET['f1'];
  5. include 'connection.php';
  6.  
  7. $query="select * from users where userid='$id'";
  8. mysql_error();
  9. $result = mysql_query ($query) ;
  10. $num=mysql_num_rows($result);
  11. $i=0;
  12. While($i < $num)
  13. {
  14. $userid=mysql_result($result,$i,"userid");
  15. $sqlusername=mysql_result($result,$i,"username");
  16. $password=mysql_result($result,$i,'password');
  17. $role=mysql_result($result,$i,"role");
  18. $status=mysql_result($result,$i,'status');
  19. $allocatedmemory=mysql_result($result,$i,"allocatedmemory");
  20. $arr_id=split("[{ }-]" ,$allocatedmemory);
  21. $i++;
  22. }
  23. ?>
  24. <form action="update.php" method="post">
  25. <div>
  26. <center>
  27. <table>
  28.     <tr>
  29.         <td style="color: navy;">Userid</td>
  30.         <td><input type="text" readonly="readonly" name="userid" value="<? echo $userid; ?>"></td>
  31.     </tr>
  32.     <tr>
  33.         <td style="color: navy;">Username</td>
  34.         <td><input type="text" name="username" id="txtname" value="<? echo $sqlusername; ?>"></td>
  35.     </tr>
  36.     <tr>
  37.         <td style="color: navy;">Password</td>
  38.         <td><input type="password" name="password" id="pwd" value="<? echo $password; ?>"></td>
  39.     </tr>
  40.         <tr>
  41.         <td style="color: navy;">Status:</td>
  42.         <td><select name="status">
  43.         <?php
  44.         if($status == 1)
  45.         {
  46.             echo '<option value="Active" selected="selected">Active</option>';
  47.             echo '<option value="Inactive">Inactive</option>';
  48.         }
  49.         else
  50.         {
  51.             echo '<option value="Inactive" selected="selected">Inactive</option>';
  52.             echo '<option value="Active">Active</option>';
  53.         }
  54.         ?>
  55.         </select></td></tr>
  56.         <tr>
  57.         <td style="color: navy;">Allocatedmemory</td>
  58.         <td><input type="text" onkeypress="return isNumberKey(event)" id="am" name="allocatedmemory" value="<? echo $arr_id[0]; ?>"></td>
  59.         <td><select name="size">
  60.         <?php
  61.            if($arr_id[1]== "KB")
  62.            {
  63.                 echo '<option value="MB">MB</option>';
  64.                 echo '<option value="KB" selected="selected">KB</option>';
  65.            }
  66.            else
  67.            {
  68.                    echo '<option value="MB" selected="selected">MB</option>';
  69.                 echo '<option value="KB">KB</option>';
  70.  
  71.            }?>
  72.         </select>
  73.         </td>
  74.     </tr>
  75.         <tr>
  76.             <td></td>
  77.  
  78.         <td><input type="submit" name="btn" value="Update" onclick="return (confirm('Are you sure you want to Update this?'),check())"/>
  79.         <input type="submit" name="btn" value="Delete" onclick="return (confirm('Are you sure you want to delete this?'))"/></td>
  80.     </tr>
  81.     </table>    
  82. </center>
  83. </div>
  84. </form>
  85. </body>
  86. </html>
  87. <?php
  88.  $user=$sqlusername;
  89. echo $user;
  90.  if(isset($_POST['btn'])) 
  91. {
  92.     include 'connection.php';
  93.      if($_POST[btn]== 'Update')
  94.  {
  95.       $pname=$_POST['username']; 
  96.      if($_POST[status] == 'Active')
  97.         $_POST[status] = 1;
  98.     else{
  99.             $_POST[status] = 0;
  100.         }
  101.        echo $pname;
  102.        if($user==$pname)
  103.      {
  104.          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]'");
  105.          echo "<script language='javascript'>
  106.                 alert('User details updated');
  107.                 </script>";
  108.                 echo "<script language='javascript' type='text/javascript'>
  109.                 window.close();
  110.                 </script>";
  111.      }
  112.      else{
  113.             $query="select * from users where username='$pname'";
  114.             mysql_error();  
  115.             $result=mysql_query($query);
  116.             $count=mysql_num_rows($result);
  117.             if($count==0)
  118.             {
  119.                 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]'");
  120.             }
  121.             else
  122.             {
  123.                 echo "<script language='javascript'>
  124.                     alert('Username already exists');
  125.                     </script>";
  126.             }
  127.          }
  128.  }
  129.  else if($_POST[btn] == 'Delete')
  130.   {
  131.         mysql_query("Delete from users where userid ='$_POST[userid]'");
  132.         echo "<script language='javascript'>
  133.               alert('Data Deleted');
  134.               </script>";
  135.         echo "<script language='javascript' type='text/javascript'>
  136.                window.close();
  137.                </script>";        
  138.   }
  139.  ?>
May 22 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.