In der folgenden Abfrage versuche ich, das Profilbild des Benutzers auszuwählen und auszugeben. Meine Einfüge-Abfrage und Select-Abfrage funktioniert (glaube ich). Im Moment kann ich das Bild hochladen und die User-ID des Benutzers ist mit dem Bild in meiner Datenbank verknüpft. Dann versuche ich in meiner Select-Abfrage das Bild auszuwählen, das diesem Benutzer zugeordnet ist.Ausgabe des ausgewählten Abfrageproblems
Meine Datenbank dafür ist einfach und hat nur die folgenden ... id, user_id, img
.
Ich glaube, das Problem mit meinem Ausgang befinden kann, hier:
<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"; ?>" />
Ich weiß einfach nicht, was ich falsch mache. Sieht jemand, ob ich das falsch ausgibt? Momentan wird das Standardbild beim Laden der Seite geladen. Sobald ich jedoch ein Bild für einen Benutzer hochlade, wird es angezeigt, aber wenn ich die Seite neu lade, wird das Bild auf den Standard zurückgesetzt.
function getPhoto($con,$dest)
{
$user_id = (isset($_SESSION['user']) ? $_SESSION['user'] : ""); //added
$result = mysqli_query($con,"SELECT * FROM `profile_img` where `user_id` = '$user_id' ORDER BY `id` DESC LIMIT 1");
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>
Ändern der Rückgabewert in Ihrem 'getPhoto()' von der Rückgabe '$ row' auf' $ row [ 'img'] 'in Ihr Ergebnis über Sie geben nur die ganze Spalte anstelle der Bildspalte zurück –
Auch Ihr $ profPic prüft, ob Daten in die Datenbank eingefügt wurden. Da beim Aktualisieren keine Daten in die Datenbank eingefügt werden, wird der Standard angezeigt. Ordnen Sie also einfach '$ profPic' eine separate unbedingte Anweisung zu, die nur Daten aus der Datenbank abruft –
Dadurch wird verhindert, dass das hochgeladene Foto angezeigt (ausgewählt) wird. – Becky