Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts

    edit user php script (need a help)

    Iam new to php and iam having some problems with my edit user php file. The problem is the query doesnt perform. No error showing. Just PLs can some one help me with this code. Thanks in advance.


    PHP Code:
    <?php
    session_start
    ();


    if(!isset(
    $_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.  
    {                                                                                
      echo 
    '<script language="javascript">';
      echo  
    'alert("Please login as ADMINISTRATOR to edit a user");'
      echo   
    ' window.location.replace("login-form.php");';
      echo  
    '</script>';                

    //header("location:login-form.php"); // Re-direct to login-form.php 

    }
    else
    {
          include(
    "config.php");
          
    $id mysql_real_escape_string($_POST['id']);
    $login    mysql_real_escape_string($_POST['login']);
    $password mysql_real_escape_string($_POST['password']);
    $type     mysql_real_escape_string($_POST['type']);

    $qry_edit " UPDATE members SET login='$login', password='$password', type='$type' WHERE id='$id' ";

    $count mysql_query("SELECT COUNT(id) FROM members WHERE id='$id'");
          if(
    mysql_num_rows($count)==1)
             {
                
              if(
    $result=mysql_query($qry_edit))
             
    //or die(mysql_error());
                
    {
                  
    /*echo  '<script language="javascript">';
                     echo  'alert("you have successfully edited one user !" );';
                    // echo  'window.setTimeout("window.location.replace('adduser.php');",20);';
                 //echo  'window.setTimeout("window.location.replace('adduser.php'),20");';
                     echo  '</script>';*/  
               
    header("Location: view_all_user.php"); 
             
                 }
             else
                 {
                
                echo 
    "<br><font color=red size=+1 >Problem in editing !</font>" ;
                echo 
    "ERROR - unable to save new username and password!<br>";
                   
    $SQLError =  "SQL ERROR: ".mysql_errno().".  ".mysql_error()."<BR><BR>";
                   echo 
    "$SQLError";
                   
    mysql_close();   
                  }
             }
          
    //echo "<br><font color=green size=+1 >you have successfully edited one user ! <br>[ username = $login ] </font>" ;
          
    else
              {
                echo 
    "<br><font color=red size=+1 >No id !</font>" ;         
                        }
    }
    ?>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try changing this;

    PHP Code:
    if($result=mysql_query($qry_edit)) 
    to this

    PHP Code:
    if(mysql_query($qry_edit)) 

  • Users who have thanked angst for this post:

    viddz (04-17-2012)

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    Done,
    But following error occured what shal i do?

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in F:\xampplite\htdocs\vidi\edit_user_save.php on line 26

    No id !

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    you'll need to figure out why the ID isn't being passed along.

  • Users who have thanked angst for this post:

    viddz (04-17-2012)

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Add a test line to see what is happening ...


    $id = mysql_real_escape_string($_POST['id']);
    $login = mysql_real_escape_string($_POST['login']);
    $password = mysql_real_escape_string($_POST['password']);
    $type = mysql_real_escape_string($_POST['type']);
    echo "id: $id";
    exit;

  • Users who have thanked mlseim for this post:

    viddz (04-17-2012)

  • #6
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    @angst

    i ddnt get wat u say. may b my language skills r bad.

    this is my view all user page code.

    PHP Code:
    <?php
    session_start
    ();


    if(!isset(
    $_SESSION['SESS_LOGIN']) || $_SESSION['SESS_TYPE'] !='admin')// if session variable "login" does not exist.  
     
    {                                                                                
      echo 
    '<script language="javascript">';
      echo  
    'alert("Please login as ADMINISTRATOR to view all users");';  
      echo    
    ' window.location.replace("login-form.php");';
      echo  
    '</script>';                

    //header("location:login-form.php"); // Re-direct to login-form.php 

    }
    else
    {
          include(
    "config.php");

            
    // get results from database
            
    $result mysql_query("SELECT * FROM members"
                    or die(
    mysql_error());  
                    
            
    // display data in table          
            
    echo "<table border='1' cellpadding='10'>";
            echo 
    "<tr> <th>login</th> <th>type</th> <th>password</th> <th></th> <th></th> </tr>";

            
    // loop through results of database query, displaying them in the table
            
    while($row mysql_fetch_array$result )) 
            {
                    
                    
    // echo out the contents of each row into a table
                    
    echo "<tr>";
                    echo 
    '<td>' $row['login'] . '</td>';
                    echo 
    '<td>' $row['type'] . '</td>';
                    echo 
    '<td>' $row['password'] . '</td>';
                    echo 
    '<td><a href="edituser.php?id=' $row['id'] . '">Edit</a></td>';
                    echo 
    '<td><a href="delete_user.php?id=' $row['id'] . '">Delete</a></td>';
                    echo 
    "</tr>"
            } 
           
            echo 
    "</table>";
    }
            
    ?>

    this is my edit user form code

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit user</title>



    <script type="text/javascript">

      function validateForm(){
          with (document.edituserForm)
          
      {
        if(login.value == "") {
          alert("Error: login cannot be blank!");
          login.focus();
          return false;
        }
         if(login.value.length < 4) {
            alert("Error:Login must contain at least four characters!");
            login.focus();
            return false;
          
          }
        
        re = /^\w+$/;
        if(!re.test(login.value)) {
          alert("Error: login must contain only letters, numbers and underscores!");
          login.focus();
          return false;
        }

        if(password.value == "" ) {
        alert("Error: Password field cannot be blank !");
          password.focus();
          return false;
        }
          if(password.value.length < 4) {
            alert("Error: Password must contain at least four characters!");
            password.focus();
            return false;
          }
                
              if(!re.test(password.value)) {
          alert("Error: Password must contain only letters, numbers and underscores!");
          password.focus();
          return false;
          
          }

         
        
        else 
          
           // alert("You have added a new user : " + login.value);
        return true;
      
      }}
    </script>

    <link href="table.css" rel="stylesheet" type="text/css" media="screen" />

    </head>

    <body>
    <?php  $id $_GET[id]; ?>

    <form ACTION="edit_user_save.php" name="edituserForm" id="edituserForm" METHOD="POST" align="center" onsubmit="return validateForm()" >

    <input type="hidden" name="id" value="$id" />
    <div align="center"><center>
      <table width="436" id="table">
        <tr>
          <td width="214">username</td>
          <td width="254"><input name="login" maxlength="10" id="login" /></td>
          </tr>
        <tr>
          <td>password</td>
          <td><input type="text" id="password" name="password" maxlength="10" /></td>
          </tr>
        <tr>
          <td>Select user type :</td>
          <td><select  name="type" id="type">
            <option value="admin"> Admin </option>
            <option value="labassistant"> Lab Assistant </option>
            <option value="storekeeper"> Store Keeper </option>
            </select></td>
          </tr>
        
        <tr>
          <td></td>
          <td><input type="submit" name="button" id="button" value="save"/></td>
          </tr>
        </table>
    </center></div>
          
          </form>
    </body>
    </html>
    anythng wrong with those codes ??

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Here's one problem I see ...


    This line:
    <input type="hidden" name="id" value="$id" />

    Should be:
    <input type="hidden" name="id" value="<?=$id=>" />

  • Users who have thanked mlseim for this post:

    viddz (04-17-2012)

  • #8
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    @mlseim,

    done as u told,
    this is the result: id: $id

    wat would be the error ?

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    id:$id

    That is an error ... $id should be a number, not the literal "$id".

    Fix this:

    This line:
    <input type="hidden" name="id" value="$id" />

    Should be:
    <input type="hidden" name="id" value="<?=$id=>" />

  • Users who have thanked mlseim for this post:

    viddz (04-17-2012)

  • #10
    New Coder
    Join Date
    Apr 2012
    Posts
    95
    Thanks
    50
    Thanked 0 Times in 0 Posts
    yeah !! thank u very much mlseim & angst. problem is with id.
    problem solved


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •