1

trying to uploadimage in database with this

$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);

if($image_size == false)
{
    echo "Not an image";
}
else
{
    mysqli_query($link, "INSERT into Events (imagetype, Attachments) 
                         VALUES ('$image_name', '$image')");
}

connection.php includes my connection

trying to retrieve image with this:

include "connection.php";

$query = "SELECT  imagetype,Attachments from Events where E_id='$id' ";
$result = mysqli_query($link,$query);
while ($row = mysqli_fetch_assoc($result))
{
    header("Content-type:image/jpeg");
    echo mysqli_result($row['Attachments'], 0);
}
Imdad
  • 5,942
  • 4
  • 33
  • 53
  • you're entering the "image" - `'image'` string rather than the intended variable. – Funk Forty Niner Nov 05 '15 at 19:04
  • can you tell me specifically where – Shubham Gupta Nov 05 '15 at 19:05
  • 2
    I already have. unless that was a bad paste – Funk Forty Niner Nov 05 '15 at 19:05
  • 1
    @ShubhamGupta Look in your `mysqli_query` statement. `'image'` is not the same as `'$image'`. – h2ooooooo Nov 05 '15 at 19:06
  • 3
    *Oh, and another thing.* You're using the MySQL keyword `events` and it's interpreting that as a function, rather than a table name declaration ([2nd time I see this in 2 days](http://stackoverflow.com/q/33536639/)). https://dev.mysql.com/doc/refman/5.5/en/keywords.html - It needs special attention. Your query should be erroring out, but you're not checking for that. So, that's 2 hits against you. – Funk Forty Niner Nov 05 '15 at 19:08
  • @Kenney He'd need the quote if he chooses to insert the picture through a paste-in variable :-) – h2ooooooo Nov 05 '15 at 19:09
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 05 '15 at 19:10
  • @h2ooooooo you're right! – Kenney Nov 05 '15 at 19:13
  • 1
    There are far too many unknowns here in order to provide a full solution for this question. It is up to you to debug your code and find out where you have gone wrong. Consult these following links http://php.net/manual/en/mysqli.error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. Good luck. – Funk Forty Niner Nov 05 '15 at 19:22
  • @SubinThomas I can't say if it's the solution here for certain. Look at the comments left above. There is more here than meets the eye ;-) – Funk Forty Niner Nov 05 '15 at 19:54
  • Why are you using `header("Content-type:image/jpeg");` in block **while**? It is enough to use one time. – Danila Ganchar Nov 05 '15 at 19:56
  • @Fred-ii- :-| true.. – Subin Thomas Nov 05 '15 at 20:02
  • 1
    @SubinThomas which is why I decided to post comments rather than an answer, because I know and feel it in my bones, that that would not be enough to solve their question. There are far too many unknowns. *Cheers* and good luck. I hope this will get resolved. – Funk Forty Niner Nov 05 '15 at 20:04

1 Answers1

1

Here is your error.

In your code, echo mysqli_result($row['Attachments'], 0); in this line, I am not clear why do you use mysqli_result.

If your datatype for Attachments is VARCHAR , I think $row['Attachments'] is enough.

So your code for showing image should be

while($row= mysqli_fetch_assoc($result)){
   header("Content-type:image/jpeg");
    echo ($row['Attachments']);
Subin Thomas
  • 1,408
  • 10
  • 19