In my application I can be able to upload
an image
for a user. I am maintaining the user_id
as follows.
+----------+ | user_id | +----------+ | ADM-0001 | | ADM-0002 | | DOC-0001 | +----------+
The problem is, if I will upload the user image, it will update
all rows
in the table
. But if I manually changed the user_id
as 1
instead of ADM-0001
, then it will update the specific row.
Following is the PHP code,
<?php session_start(); require_once "../auth/dbconnection.php"; if (isset($_POST['image'])) { $croped_image = $_POST['image']; list($type, $croped_image) = explode(';', $croped_image); list(, $croped_image) = explode(',', $croped_image); $croped_image = base64_decode($croped_image); $image_name = time().'.png'; $stmt = $conn->prepare("UPDATE users SET image = ? WHERE user_id= ?"); $stmt->bind_param("si", $image_name, $_SESSION['user_id']); $stmt->execute(); echo 'Image Uploaded Successfully.'; if($stmt->affected_rows === 0); file_put_contents('blog/'.$image_name, $croped_image); }else{ echo "ERROR: Could not prepare query: $stmt. " . mysqli_error($conn); } $stmt->close(); ?>
Actually I a, wondering why it was happening. Please help me improve my code. Thanks in advance.
Advertisement
Answer
You are telling the bind-param method to convert the user_id into an integer. Change it to keep it as a string:
$stmt->bind_param("ss", $image_name, $_SESSION['user_id']);