Ich versuche, eine Benutzerprofilbildfunktion zu erstellen. Bisher bin ich in der Lage, ein Bild hochzuladen und auszuwählen und es anzuzeigen, aber ich kann nicht herausfinden, wie man das Bild auswählt, das für einen Benutzer spezifisch ist. Meine Abfrage funktionierte (obwohl mit dem oben beschriebenen Problem), bis ich den folgenden Code hinzugefügt habe (// hinzugefügt, was ich geändert habe).Wählen Sie eine Abfrage aus, die den Spalten entspricht
Ich konnte meine Einfügeabfrage bekommen, um die user_id des Benutzers mit ihm zu senden, ich kann einfach nicht den ausgewählten Teil herausfinden.
Meine Datenbank dafür ist klein, es hat nur id, user_id, img
.
Ich möchte die img
auswählen, die in meiner Datenbank ist, die User_id des Benutzers hat. Ich trage die user_id mit einer Sitzung und das ist was $user_id
ist.
Wer sieht, was ich in meiner Select-Abfrage falsch mache?
function getPhoto($con,$dest)
{
$user_id = (isset($_SESSION['user']) ? $_SESSION['user'] : ""); //added
// $result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$dest'");
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$user_id'"); //added
if($row = mysqli_fetch_array($result))
return $row;
return 0;
}
Bearbeiten: Mehr Code.
function getPhoto($con,$dest)
{
$user_id = (isset($_SESSION['user']) ? $_SESSION['user'] : ""); //added
// $result = mysqli_query($con,"SELECT * FROM `profile_img` where `img` = '$dest'");
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `user_id` = '$user_id'"); //added
if($row = mysqli_fetch_array($result))
return $row;
return 0;
}
// Make sure all functions above are include here
// Get the database connection
$con = Connection();
// Check for post
if(isset($_POST['create'])) {
// Try uploading
$upload = UploadFile($_FILES);
// If upload fails
if(!$upload['success'])
echo '<h3>Sorry, an error occurred</h3>';
else {
// You could add error handling here based on the results of
// each function's success or failure below.
// Try to save it
$saveToDb = SaveToDb($con,$upload['file']['dest']);
// Get the profile from image name
$profPic = ($saveToDb)? getPhoto($con,$upload['file']['dest']) : false; ?>
<?php
}
}
?>
<img id="profile-pic" src="<?php echo (!empty($profPic) && $profPic != 0)? $profPic['img'] : "profile_images/default.jpg"; ?>" alt="<?php echo (!empty($profPic) && $profPic != 0)? "Profile Picture" : "No Picture"; ?>" />
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" class="inputbarfile" onchange="readURL(this);">
<img width="400px" height="300px" id="file" src="#" alt="your image">
<input type="submit" name="create" id="signinButton" value="Upload">
</form>
wenn Sie wollen 'img' wählen Sie es, nicht alles mit (*) – nogad