0

hi,
i am new to php. I am trying to fetch the image stored in database by using mysqli but couldnt fetch it. Please help me with this. Please find the code and let me know where i am lacking...

<?php 
$link = mysqli_connect('localhost','root','','db_image') or die(mysqli_connect_error());

if(isset($_POST['btn_submit'])){
extract($_POST);

$file_name=$_FILES["image"]["name"];
$file_tmp=$_FILES["image"]["tmp_name"];

$check = getimagesize($_FILES["image"]["tmp_name"]);
print_r($check);

echo'<br>';
if($check !== false){
    $file_name = $_FILES["image"]["name"];
    print_r($file_name);
    echo "<br>";
    $imgContent = addslashes(file_get_contents($file_tmp));
    $query = "insert into insert_image(Image,Image_Name,user_regon) values('$imgContent','$file_name', NOW())";
    if(mysqli_query($link,$query)){
        echo"Image uploaded successfully";  
    }
    else{
        echo"Failed to upload image". mysqli_error($link);; 
    }   
}else{
        echo "Please select an image file to upload.";
    }   
}

if(isset($_POST['btn_display'])){ 
 $query = "select * from insert_image"; 
 $result = mysqli_query($link,$query);
 while($row = mysqli_fetch_assoc($result)){
    extract($row);
    //$imgdata = $row["Image"];
    echo $row[0] . ': ' . $row[1] . ' ' . $row[2];
    //print_r($imgdata);
?>
<table border="1">
<tr>
    <td>ID</td>
    <td><?=$id?></td>
</tr>
<tr>
    <td>Image</td>
    <td><?=$Image?></td>
</tr>    
</table>
<?php   
    echo "User ID". $id;
    echo "<br />";
}
mysqli_close($link);
}

?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="image"/>
        <input type="submit" name="btn_submit" value="UPLOAD"/>
        <input type="submit" name="btn_display" value="Display"/>
</form>
</body>
</html>

Edited by Reverend Jim: Moved to programming forum

2
Contributors
1
Reply
17
Views
3 Months
Discussion Span
Last Post by AndrisP
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.