0

I have the right idea. I am trying to display a profile image when the user logs in. I have a run time error. I have three different accounts set up. If I order the statment by User_Name, then I only get the ID of 2. If I order it by desc then I only get the ID of 3.. and so on and so forth. I am using two different naming conditions. mysql, and mysqli. I just need this to work as soon as possible. Here is my source code. Is their anyway to check for ID to equal the User_Name that I have within that mysql statment? Or is it more complex than I am thinking?

<?php
    include "open_html.php";
    include "htmlObody.php";
    include "connect.php";

    $query = mysqli_query($con, "SELECT User_Name FROM user");
    $row = mysqli_fetch_assoc($query);

    @setcookie("user", $row);

    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("info");

    $query2 = "SELECT ID FROM user ORDER BY User_Name"; 

    $result = mysql_query($query2);
    $row2 = mysql_fetch_array($result);
?>

    <div>
        <img src="user_file.php?ID=<?php echo $row2['ID']; ?>"/>
        <br />
        <?php echo $_COOKIE["user"]; ?>
    </div>

<?php
    include "htmlCbody.php";
    include "close_html.php";
?>
JazzCat
  • 4,243
  • 1
  • 26
  • 40
  • 1
    Just use the `mysqli` connection, you shouldn't need `mysql_`. You should select with a `where` clause for the current user, right? – chris85 Feb 08 '16 at 21:41
  • 1
    You're not looping through your record set. You are just grabbing the last one. This is especially a problem since you are pulling ALL the users down instead of just the one with the ID you already know (presumably). – durbnpoisn Feb 08 '16 at 21:43
  • currently you are using a cookie to store the user ... `$_COOKIE["user"];` since a cookie can be changed by the client you need to take some steps to validate that information its better to use a session ... `$_SESSION["user"];` – cmorrissey Feb 08 '16 at 21:51

1 Answers1

0

My images were not being echoed by ID but by their names. The query looks like this:

    $sql = "SELECT ID, User_Image, User_Name FROM user WHERE User_Name = Image_Name";

Make sure that when the user creates a new user to have the user name equal the image name. So something like:

$userName = $_POST["user"];
$userImage = $_POST["user"];
$query = mysqli_query($con, "SELECT * FROM user INSERT INTO user_name, user_image VALUES ("userName", "userImage"));

That's not to insert an image to the database, but to make it so that you can correctly grab the profile image. I used this link to upload images.

Community
  • 1
  • 1