Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IMG dir can't be stored in db but viewed from the same variables used in query

I'm trying to upload an image to my server using the following form by Sanwebe. Can be found here. However when I'm pressing upload, the new thumb loads perfectly fine. However, my image can't be uploaded to the database using the exact same variables from which the image is being viewed. How come? I tried putting the db information just infront of the query. Like this:

echo '<div align="center">';
echo '<img src="images/profile-pictures/'.$thumb_prefix . $new_file_name.'" alt="Thumbnail">';
echo '</div>';

$profile_pic_temp = "../images/profile-pictures/" . $thumb_prefix . $new_file_name;
$profile_pic_full_temp = "../images/profile-pictures/" . $new_file_name;
$session_user = $_SESSION['user_confirm'];

require 'database.php';

$profile_pic_db_upload = $db->prepare("UPDATE login SET profile_picture_temp = :profile_pic_temp, profile_picture_full_temp = :profile_pic_full_temp WHERE user_session = :session_user");
$profile_pic_db_upload->bindParam(':session_user', $session_user, PDO::PARAM_STR);
$profile_pic_db_upload->bindParam(':profile_pic_temp', $profile_picture_temp, PDO::PARAM_STR);
$profile_pic_db_upload->bindParam(':profile_pic_full_temp', $profile_picture_full_temp, PDO::PARAM_STR);
$profile_pic_db_upload->execute();
$confirm_upload_db = $profile_pic_db_upload->rowCount();

if($confirm_upload_db != 0){
    $popup_message = "Profile picture has been uploaded.";
    echo $popup_message;
}
else{
    $popup_message = "Profile picture could not be uploaded.";
    echo $popup_message;
}

EDIT TWO: The query now runs, however, I get the feedback "Profile picture could not be uploaded.". How come the query does not run properly?

EDIT FOUR: I have tried changing the user_session = :session_user to id = 1 instead. I then get upload successfull, however, the value is only inserted into profile_picture_temp and is set to 0. Somehow the bindParam changes the value. Why?

EDIT THREE: I have now tried using mysqli aswell. Same results here. Returning could not be uploaded. However, does not change value in DB.

$sql = "UPDATE login SET profile_picture_temp = ? AND profile_picture_full_temp = ? WHERE user_session = ?";
$stmt = $mysqli->prepare($sql) or die ("Database error<br>" . $sql . "<br><b>Error message:</b> " . $mysqli->error);
$stmt->bind_param("sss", $profile_picture_temp, $profile_picture_full_temp, $session_user);
$stmt->execute() or die("Something went wrong");
if($stmt->fetch()){
    $popup_message = "Profile picture has been uploaded.";
    echo $popup_message;
}
else{
    $popup_message = "Profile picture could not be uploaded.";
    echo $popup_message;
}
$stmt->free_result();
$stmt->close();
like image 926
Gjert Avatar asked May 29 '15 21:05

Gjert


2 Answers

Are you sure this line isn't throwing a PHP error...

$confirm_upload_db = $$profile_pic_db_upload->rowCount();
                     ^^

The $$ (two dollar signs) are how we reference a variable variable; but $profile_pic_db_upload doesn't contain the name of another variable, it's a reference to a PDO statement object.

Another note. The rowCount() function returns the number of rows affected by the UPDATE statement; if the UPDATE statement succeeds, but no actual changes are made to the row (because the values assigned to the columns are the same as what's already stored in the columns), then rowCount() will return 0.

(To change that behavior, to have it return the number of matched rows, you can use PDO::MYSQL_ATTR_FOUND_ROWS).

like image 148
spencer7593 Avatar answered Oct 20 '22 00:10

spencer7593


The problem was fixed using the following query:

$profile_picture_temp = "../images/profile-pictures/" . $thumb_prefix . $new_file_name;
$profile_picture_full_temp = "../images/profile-pictures/" . $new_file_name;
$session_user = $_SESSION['user_confirm'];

$sql = "UPDATE login l SET l.profile_picture_temp = ?, l.profile_picture_full_temp = ? WHERE l.user_session = ?";
$stmt = $mysqli->prepare($sql) or die ("Database error<br>" . $sql . "<br><b>Error message:</b> " . $mysqli->error);
$stmt->bind_param("sss", $profile_picture_temp, $profile_picture_full_temp, $session_user);
$stmt->execute() or die("Something went wrong");

$result = $stmt->affected_rows;
if($result == 1){
    $popup_message = "Profile picture has been uploaded.";
    echo $popup_message;
}
else{
    $popup_message = "Profile picture could not be uploaded.";
echo $popup_message;
}
$stmt->free_result();
$stmt->close();

I can't identify the problem itself, however, I managed to fix it by adding UPDATE login l. Using alias fixed it somehow.

like image 37
Gjert Avatar answered Oct 19 '22 23:10

Gjert