Hello you all genius guyz once again i am in need of your help I am trying to create a page on admin to be able to see the profiles of all the users but filed to do that I have tried the while loop but still I am unsuccessfull let me show you how I created then please let me know where I am mistaken

<label>
                      <p>Select User</p>
                        <p>
                        <select name="select3">
                            <?php 
                                $recordset6 = mysql_query("select * from users");
                                while($record6 = mysql_fetch_array($recordset6)) {
                                    echo"<option value=",$record6["uid"],">",$record6["ulogin"],"</option>";
                                }
                            ?>  
                        </select>
                        </P>
                </label>



                <label>
                    <input type="submit" value="Show" name="button" class="button" style="float:left;margin-left:25%;">
                </label>




<?php 

                    $result3 = mysql_query("SELECT * FROM users where uid='$uid'");
                    while($row3 = mysql_fetch_array($result3))
                    { 
                        $fname=$row3['fname'];
                        $lname=$row3['lname'];
                        $uaddress=$row3['uaddress'];
                        $uphone = $row3["uphone"];
                        $uemail = $row3["uemail"];
                        $desig = $row3["udesignation"];
                        $empstat = $row3["empstat"];
                        $jdate = $row3["jdate"];
                    }

                    echo $fname . "<br />" . $lname . "<br />" . $uaddress . "<br />" . $uphone 
                    . "<br />" . $uemail . "<br />" . $desig . "<br />" . $empstat. "<br />" . $jdate;

            ?>

                    </form>

Once again thank you for your help in advance

Recommended Answers

All 23 Replies

I have tried the while loop but still I am unsuccessfull

Can you specify in what way you were unsuccessful, eg. what was expected outcome and what really came out?

A few comments on your code:

  • You should enclose your form elements within <form> tags and set appropriate form attributes (method and action)
  • in the php part you should catch the submitted form data using something like if(isset($_POST['submit'])) and only then process the data
  • I presume there would be only one record in the recordset if you search by user ID, so while loop is unnecessary
  • I am not sure if is OK that form elements are enclosed within the <label> tags (but I might be wrong about that). It is still safer to use <label for="..."> way of coding it
  • I will repeat an old tune: sanitize the input form users before using it in a db query (google for sql injection)
  • think of switching from mysql extension and using something more modern (and secure) like pdo

Well one is admin page to see the profile for all users like the same output perfomring but this one is without submit button have a look https://www.wellsfargo.com/help/routing-number/ on the middle you will see drop down liist select any state and then you will se the routing numbers for the selected state so like that here when i select the user and press submitt i will be able to see the profile of the selected user.

Well I am not an expreciened I hope you wont mind for that I really need you guyz to help me out and point out my mistakesso i cannot do it in the future.

Well I had made a big mistake didnt posted the form starting tag

<form name="form1" method="post" action="auprofile.php?uid=<?php echo $uid?>&user=<?php echo $user?>">

So if I understood it right, you want to select a user name and have profile of the selected user displayed on the same page. I did not quite understand though, whether you want a submit button or not (but that does not matter so much, since javascript can triger form submission instead of a submit button).

There are two approaches:

  1. The simple one - You select a user and the page reloads with the same form and with user data displayed
  2. The more advanced one - You select user and the page does not reload, only the element displaying user data gets updated using AJAX

It depends on the level of your familiarity with these things which one to chose. The second approach is used in modern web apps.

In the page you refer to as an example some other approach is used. All the values are being coded in the HTML page and Javascript selects the appropriate value of the second dropdown based on the value in the first dropdown. But with a database of users usualy this approach is not appropriate (especialy if you have a lot of users).

Nevertheless, I will try to find some time today to code an example for the first approach above. Meanwhile you can google a bit for ajax and check out jquery especialy the ajax part of it.

If you however wish to tahe the second approach, let me know and I'll prepare an example code for it.

the 1st one is looking great and fimiliar for me as i never used ajax so it will not be suitable for me though (you want to select a user name and have profile of the selected user displayed on the same page) You got it correct the same thing I am looking for that is why i placed the action for the same page now with the above code which i pased when i select the user and click submit nothing happens .

Thank yOU

Here you go. See the comments in the code. If anything is not clear, just ask.

<?php 
// check if form was submitted
if(isset($_POST['button'])) {
    // select user profile data
    // $uid is forced to the integer type to make sure no ugly data enters the database
    // you could also use mysql_real_escape_string() function
    // now read a record for a selected user ID and store values in variables
    $result3 = mysql_query("SELECT * FROM users WHERE uid='" . int_val($_POST['$uid']) . "'");
    $row3 = mysql_fetch_array($result3);
    $fname = $row3['fname'];
    $lname = $row3['lname'];
    $uaddress = $row3['uaddress'];
    $uphone = $row3["uphone"];
    $uemail = $row3["uemail"];
    $desig = $row3["udesignation"];
    $empstat = $row3["empstat"];
    $jdate = $row3["jdate"];
}
?>
<form method="post" action="#">
<label for="select3">Select User</label>
<select name="select3">
<?php
$recordset6 = mysql_query("SELECT uid, ulogin FROM users");
while($record6 = mysql_fetch_array($recordset6)) {
    echo"<option value=",$record6["uid"],">",$record6["ulogin"],"</option>";
}
?>
</select><br>
<input type="submit" value="Show" name="button" class="button" style="float:left;margin-left:25%;"><br>
</form>

<?php 
// here you display user data if exists
if(isset($_POST['button'])) {
    echo "$fname<br>$lname <br>$uaddress<br>$uphone <br>$uemail<br>$desig<br>$empstat<br>$jdate";
    // you can unset the post variable so you are back to initial state
    unset($_POST);
}
?>

It would be good idea to replace al mysql* functions with mysqli* functions. mysql extension is deprecated, it was replaced with mysqli. However, in future think of switching to PDO.

Well great and appriciate your help but unfortunetly that didnt worked:( when i clicked on show the drop down and show button dissappreared and gived me a blank area

Failed to load resource: net::ERR_CACHE_MISS

this eror is coming up in the console

Can you send me some data from the users table. In phpmyadmin run the following query:

SELECT * FROM users LIMIT 10

and paste result here so I can test with the real data.

The code I posted was not tested, more just to show the concept.

It is now working but when I select second user from the drop down and click show its shows me the same user profile of admin which is me not for the other user like when i select second user and click on show it drives back to the 1st user and shows same profile

Showing rows 0 - 9 (10 total, Query took 0.0006 sec)

This was the thing you were asking for

Field Type Collation Attributes Null Default Extra
uid int(11) No None AUTO_INCREMENT
fname varchar(25) latin1_swedish_ci No
lname varchar(25) latin1_swedish_ci No
uaddress varchar(25) latin1_swedish_ci No
uphone varchar(25) latin1_swedish_ci No None
uemail varchar(25) latin1_swedish_ci No
fthname varchar(25) latin1_swedish_ci No
ulogin varchar(25) latin1_swedish_ci No
dob varchar(20) latin1_swedish_ci No
upassword varchar(25) latin1_swedish_ci No
jdate varchar(20) latin1_swedish_ci No
udesignation varchar(45) latin1_swedish_ci No

Sorry for the bad pattern

You posted the database structure instead of data. But never mind I will make up the data and check the code.

Ohh I am really sorry for that mistake yes please if you can

@UK-1991,

thanks for your message. I am confident that broj1 can deliver the solution to your problem.

Yes very helpful Love it and really really appreciated

Yeah, I had a few errors (like int_val instead of intval function name) in the code, sorry. This should work:

<?php 
// check if form was submitted
if(isset($_POST['button'])) {
    // select user profile data
    // $uid is forced to the integer type to make sure no ugly data enters the database
    // you could also use mysql_real_escape_string() function
    // now read a record for a selected user ID and store values in variables
    $result3 = mysql_query("SELECT * FROM users WHERE uid='" . intval($_POST['select3']) . "'");
    $row3 = mysql_fetch_array($result3);
    $fname = $row3['fname'];
    $lname = $row3['lname'];
    $uaddress = $row3['uaddress'];
    $uphone = $row3["uphone"];
    $uemail = $row3["uemail"];
    $desig = $row3["udesignation"];
    $empstat = $row3["empstat"];
    $jdate = $row3["jdate"];
}
?>
<form method="post" action="#">
<label for="select3">Select User</label>
<select name="select3">
<?php
$recordset6 = mysql_query("SELECT uid, ulogin FROM users");

while($row = mysql_fetch_array($recordset6)) {
    echo "<option value=", $row["uid"], ">", $row["ulogin"], "</option>";
}
?>
</select><br>
<input type="submit" value="Show" name="button" class="button"><br>
</form>

<?php 
// here you display user data if exists
if(isset($_POST['button'])) {
    echo "$fname<br>$lname <br>$uaddress<br>$uphone <br>$uemail<br>$desig<br>$empstat<br>$jdate";
    // you can unset the post variable so you are back to initial state
    unset($_POST);
}
?>

An improvement: once you selected a value and the data gets displayed your select element is set to the last selected value.

while($row = mysql_fetch_array($recordset6)) {
    echo "<option value=", $row["uid"];
    if(isset($_POST['button']) && $row["uid"] == $_POST['select3']) {
        echo ' selected';
    }
    echo ">", $row["ulogin"], "</option>";
}

OMG! Kooooool it worked you rock thumbs up for you.

Well need one more favor from you if you be so kinds engough and let me know where I was mistaken and how it worked

aND WHY YOU jad used # on action what if we used <?php $PHP_SELF ?>

would that have been worked?

aND WHY YOU jad used # on action what if we used <?php $PHP_SELF ?>

They both work since they both mean about the same thing. So it is fine if you use any of these two options.

Well need one more favor from you if you be so kinds engough and let me know where I was mistaken and how it worked

There were few issues:
- line 7: while($record6 = mysql_fetch_array($recordset6)) is not OK since you assign a row to a resultset.
- line 24 and below: you did not check if the form has been submitted, since only then you can do the reading of user data and displaying it. Before the form is submitted you basicaly display nothing about the user.
- line 44: the </form> closing tag should be somewhere on line 20
- the label tags should not enclose elements (at least I think so), but that should not stop the script to do its work anyway

Now you can style the form to look nicer :-)

yes I will make it nicer and will show you :) well once again you are best and explained me well I really appreciate your help and thank you once again cheers.

No worries, mate. If this is it, please mark it as solved.

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.