Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit the user information by admin

Tags:

php

mysql

I am developing my first simple website using PHP. Now, I am working in the admin page and I want to let him adding, deleting users and editing the personal information of the existed users. I did the adding and deleting. Now, I want to develop editing users. First of all, I want him to choose the user from drop list, then fetch the user information automatically after choosing him from the drop list, and after that editing his information. So how can I do that?

My code:

<?php
ob_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="13524"; // Mysql password
$db_name="sharingi_db"; // Database name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<script language="javascript">
    function reload(form){
        var val=form.username.options[form.username.options.selectedIndex].value;
        self.location='editUser2.php?username=' + val ;
    }
    </script>
</head>
<body>

<div id="content_main" class="admin_student_height">

<!-- Here starts the first form -->
<form method="get">
        <h3>Choose A User</h3> <br />
        select name="username" onchange="reload(this.form)">

                <option> 
        <?php
           if(isset($_GET['username']))
           echo "{$_GET['username']}"; 
           else echo "Select one";
        ?> 
            </option>

                <?php
                   if(isset($_GET['username'])){
           $exceptcc = $_GET['username'];
           $sql = "SELECT username FROM user WHERE user.username NOT IN
                                 ('$exceptcc')";
                    }
           else 
           $sql = "SELECT username FROM user";
           $result = mysql_query($sql);
           while($row = mysql_fetch_array($result)){
           echo "<option value={$row['username']}>{$row['username']}</option>";
                                }
        ?>
        </select><br /><br />

        <h3>User Information</h3> <br />
        <?php 
           $thecc = $_GET['username'];
           $sql = "SELECT Firstname FROM user WHERE Username=$thecc";
           $result = mysql_query($sql);
           while($row = mysql_fetch_array($result)){
           echo "{$row['Firstname']}>{$row['Firstname']}}";
                                }
        ?>
        <br /><br />
        </form> <br />

        </div>
    </div>
</body>
like image 265
user730077 Avatar asked May 23 '11 20:05

user730077


People also ask

What is user admin?

An administrator is someone who can make changes on a computer that will affect other users of the computer. Administrators can change security settings, install software and hardware, access all files on the computer, and make changes to other user accounts.

How do I change my user ID in Office 365?

Watch: Change a user's name or email addressIn the Microsoft 365 admin center, select Users > Active users. Select the user from the list of active users. Select Manage contact information. Change the display name, and select Save changes.

Where is admin in Google Analytics?

The Admin page in Google Analytics gives you access to the Analytics administrative features. At the bottom left of the page, hover over the gear icon and click Admin to open the page.


2 Answers

I've been working on making a web-based ticketing system and ran into this same situation. I solved the problem like this:

  1. When the page is loaded, determine if they have admin rights or throw them off the page.
  2. Do an SQL Query to get the List of users, to either display in a list or in a drop down box.
  3. Once the User to edit has been selected, Do another Query and load each item into a field; what I did was use the same form for adding new users but have php build the form and insert the current values for that user into the fields.
  4. When this form is submitted, (and submitter verifed) I have the php script look at the submitted username and use that for the where clause in the sql update statement

If you want me to post up an example of what I did I can do that.

like image 181
Kit Ramos Avatar answered Sep 25 '22 18:09

Kit Ramos


You are only echo'ing the user's information.

Instead, you need to put the information into a form, which will allow for editing.

<?php

 if ($_POST['submit']) {

 $username = $_POST['username'];

 //if you want to update
 mysql_query("UPDATE users SET username = '$username', password = '$password'");

 //if you want to delete
 mysql_query("DELETE FROM users WHERE username = '$username'");

 }

 ?>

 <?

 //show all users

$user_query = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($user_query)) {

echo $row['username'] . ' ' . $row['first_name'] . ' ' . $row['last_name'];
//and so on.. depending on your table fields

}


 ?>




<form method="POST">

Username: <input name="name" value="<?echo $row['username'?>"/>
<input type="submit" name="submit"/>

</form>
like image 28
psarid Avatar answered Sep 22 '22 18:09

psarid