i have three entries in phpmyadmin id name and image
i want to upload image from the form into database and display that back into the form where from i am uploading it.I get this error

PHP Warning: mysql_insert_id() expects parameter 1 to be resource, null given in D:\path\to\sitename\httpdocs\sql\uploadimage.php on line 13 is first line

    <?php
        // connect to database
       $id = mysql_insert_id(id);
        $file = $_FILES['image']['tmp_name'];
        if (!isset($file))
            echo "Please Upload your Photograph.";
        else
        {
        $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 "Are you sure you uploaded an image";
           else
           {
            if (!$insert = mysql_query("INSERT INTO fgusers4 VALUES ('','$image_name','$image')"))
             echo "Problem Uploading Image.";
        else 
        {
        $lastid = mysql_insert_id();
        echo "Image Uploaded.<p /> Your Photograph:<p /><img src=get.php?id=$lastid>";

        }
           }
        }   
        ?>


`$id = mysql_insert_id($id);
    $file = $_FILES['image']['tmp_name'];`

`

`

mysql_insert_id needs the mysql connection as parameter. Am not sure what you are trying to do with id.

Member Avatar for Zagga

You can only get the id of the inserted record after you have inserted it. I don't think you need line 3 at all by the code you have shown.

Hello Zagga and pritaeas !I am glad to see you both here after pretty long time. Both of you are correct .As far as pritaeas is concerned the place where you see a comment connect to database.I am actually including a file get.php and my connection is over there.But i would like to show you that code also it is here:I am actually taking lastid from id and the line where i have written header changes get.php into an image .As far as Zagga is concerned i have already deleted that line.

<?php
    mysql_connect("host","username","password") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error());
//  $id = addslashes mysql_insert_id();
    $id = addslashes($_REQUEST['id']);
    $image = mysql_query("SELECT * FROM fgusers4 WHERE id=$id");
    $image = mysql_fetch_assoc($image)
    $image = $image['image']

    header("cotent-type: image/jpeg");

    echo $image;
?>

not in that entry and also i cant create id as id is already in the new form.I cant create two id's with autoincreament .I get this error.

Sorry for line after code.I actually deleted some lines.I was working on localhost and now i have uploader file to server and i get this error.I am sure problem lies where zagga and pritaeas has pointed to but i am unable to find how to correct it.Here is new error.

PHP Parse error:  syntax error, unexpected T_VARIABLE in path\to\get.php on line 8
Member Avatar for Zagga

Hi again,
You are missing the ending semi-colons on lines 7 and 8 of get.php

Let us know how it goes

It gives me this.:
PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\path\to\get.php on line 7
PHP Warning: Cannot modify header information - headers already sent by (output started at D:\path\to\register.php:125) in D:\INETPUB\VHOS

Member Avatar for Zagga

Can you post the code you are currently using? I'll see if I can clear it up a bit for you

code of which file .I am actually submitting a form , a contact us form and i have also included username password in it .Once the user submiits a form .It gets saved in phpmyadmin.and it sends mail to user for activation of his account.It also sends mail to admin at the same time.Once the user confirms .Another mail goes to admin with details of user.
Now i added one more thing to it that is user photograph.I had tested both things and it worked correctly .Suddenly my laptop shut down and i think i lost some file unsaved.or i missed it.It is two days since.I am not able to solove it.I will give you my localhost files .As it will take a long time to download files from server.But How am i supposed to attache these file there no such fecility here.I want you to help me in uplading an image rest i will try because i had already succesfully done that and as i already said i lost some file or code.
In image upload i had also succesfully uploaded and saved to database but it was still showing me the errors which you two had pointed above.If you solove this error i am sure it will help me in soloving others.And if i get stuck I will post the error here.
Thanks in advance
I have to use two files for image upload one index other get.php first code is of index other of get.php

1.index code
`

<html>
<head>
      <title>Upload Your Photograph</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
File:
    <input type="file" name="image"> <input type="submit" value="upload">
</form>

<?php
// connect to database
include "get.php"
/*$id = mysql_insert_id();*/
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
    echo "Please Upload your Photograph.";
else
{
$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 "Are you sure you uploaded an image";
   else
   {
    if (!$insert = mysql_query("INSERT INTO fgusers4 VALUES ('','$image_name','$image')"))
     echo "Problem Uploading Image.";
else 
{
$lastid = mysql_insert_id();
echo "Image Uploaded.<p ></p> Your Photograph:<p ></p><img src=get.php?id=$lastid>";

}
   }
}   
?>
</body>
</html>

2.get.php

<?php
    mysql_connect("localhost","masoodi","tmit4321") or die(mysql_error());
    mysql_select_db("image") or die(mysql_error());
//  $id = mysql_insert_id()
    $id = addslashes($_REQUEST['id']);

    $image = mysql_query("SELECT * FROM fgusers4 WHERE id=$id")
    $image = mysql_fetch_assoc($image);
    $image = $image["image"];

    header("cotent-type: image/jpeg");

    echo $image;
?>

It gives me this error on localhost

( ! ) Parse error: syntax error, unexpected '$image' (T_VARIABLE) in C:\wamp\www\get.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0008  143320  {main}( )   ..\index.php:0

`

In index file i have changed form action from upload.php to index.php

When i add semicolon in get.php line 8 it gives me this error but it uploads and shows image .

( ! ) Notice: Undefined index: id in C:\wamp\www\get.php on line 5
Call Stack
#   Time    Memory  Function    Location
1   0.0007  143368  {main}( )   ..\index.php:0
2   0.0014  146368  include( 'C:\wamp\www\get.php' )    ..\index.php:13

( ! ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\get.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0007  143368  {main}( )   ..\index.php:0
2   0.0014  146368  include( 'C:\wamp\www\get.php' )    ..\index.php:13
3   1.0102  153328  mysql_fetch_assoc ( )   ..\get.php:8
Member Avatar for Zagga

Ok, we'll work through each error as they arrise. Post them here as you get them.

This error is caused because the query on line 7 of get.php is failing. This is because there is no value for $id. You can't use $id in a query until you give it a value.

Where is $id set?

Actually it is the id of data saved in phpmyadmin.We request that through this line in get.php

$id = addslashes($_REQUEST['id']);

and then display image of that id through this line in index

$lastid = mysql_insert_id();
echo "Image Uploaded.<p /> Your Photograph:<p /><img src=get.php?id=$lastid>";

i am now able to dispay image and even save it as i already told you i was able to do so.But these two errors still fill up the webpage.I dont understand how come it works and still shows errors

Member Avatar for Zagga

It seems like you are trying to display the image twice. Once in get.php (which is causing a notice and a warning, but not an error) and again in index.php (which is working correctly).

Can you not just delete the code in get.php that is trying to display the image?
eg. make get.php just this code (changing your username and password):

<?php
mysql_connect("localhost","XXX","YYY") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
?>

i placed only above code in index.php and it shows me this

( ! ) Notice: Undefined index: image in C:\wamp\www\index.php on line 15
Call Stack
#   Time    Memory  Function    Location
1   0.0609  141408  {main}( )   ..\index.php:0

And when i upload a file it does give message image uploaded but dosent show it.

sorry i place above code in get.php

Member Avatar for Zagga

This error should only happen the first time you run the page.
I have rearranged your code a bit. Try this instead of your current index.php

<html>
<head>
<title>Upload Your Photograph</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="upload">
</form>
<?php
// connect to database
include "get.php";
if (!isset($_FILES['image']['tmp_name'])){
    echo "Please Upload your Photograph.";
} else {
    $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 "Are you sure you uploaded an image";
    } else {
        $insert = mysql_query("INSERT INTO fgusers4 VALUES '','$image_name','$image'");
        $lastid = mysql_insert_id();
        if (!$insert){
            echo "Problem Uploading Image.";
        } else {
            echo "Image Uploaded.<p ></p> Your Photograph:<p ></p><img src='get.php?id=$lastid'>";
        }
    }
}
?>
</body>
</html>

This does not work My code atleast displays an image with error this does not dispaly an image

( ! ) Notice: Undefined variable: image in C:\wamp\www\get.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0006  143480  {main}( )   ..\index.php:0
2   0.0009  145760  include( 'C:\wamp\www\get.php' )    ..\index.php:12

( ! ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in C:\wamp\www\get.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0006  143480  {main}( )   ..\index.php:0
2   0.0009  145760  include( 'C:\wamp\www\get.php' )    ..\index.php:12
3   1.0147  152368  mysql_fetch_assoc ( )   ..\get.php:8
Problem Uploading Image. 

Change line 13 to:

if (!isset($_FILES['image'])) {

and line 22 to:

$insert = mysql_query("INSERT INTO fgusers4 VALUES ('', '$image_name', '$image')");

If it still fails the database connection failed.

It shows same errors.Dabase conncetion has not failed.IT is storing image in database and retriving same to display on webpage but it still shows these errors.Can you tell me how to suppress these errors .Zagga was right in saying that these are not errors but warnings and i also think same because code is working and doing its job.I think it shows these errors because usually we give image src as abd.jpeg.Here we are giving a file name get.php and then chaniging that file to image with headers.So it gives us warnings.Only thing we need now is to suppress these errors so that they dont get displayed on webbpage. Thanks
Can you tell me how to suppress these errors so that they dont show up.

Zagga was right in saying that these are not errors but warnings

Suppressing errors/warnings/notices is a very bad idea. It's bad coding, will introduce strange bugs later on.

The last error you showed about the mysql_fetch_assoc was not in the code you showed.

Member Avatar for Zagga

Hi again,
As Pritaeas said, supressing messages and warnings is a bad idea, they are there to tell you there are problems with the code.

I posted some extra code (revised get.php) last night but it doesn't seem to be showing here.

With the current warnings you are getting, the first is because you missed a semicolon off the end of your query on line 7 in get.php.
The second is because the query failed and returned NULL instead of the data you expect. PHP tries to use this NULL value in the mysql_fetch_assoc() call and so this also fails, giving you the second warning.

Fix the first problem, then post the code you are using now and the messages you are getting.

After some changes when i uploaded same file on server .It gives me these errors

`

PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in path\to\get.php on line 7
PHP Warning:  Cannot modify header information - headers already sent by (output started at path\to\register.php:125) in path\to\get.php on line 10
Member Avatar for Zagga

As mentioned before, this is because your query is failing. Change your query to

$image = mysql_query("SELECT * FROM fgusers4 WHERE id=$id") or die('Error - Could not perform the query: ' . mysql_error());

and see what the error message says

It gives this one
Error - Could not perform the query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

which line one is it talking about

In addition to the error, also echo the full query.

I changed the code as you were telling

<?php
    mysql_connect("host","username","password") or die(mysql_error());
    mysql_select_db("image") or die(mysql_error());
//  $id = mysql_insert_id()
    $id = addslashes($_REQUEST['id']);

//  $image = mysql_query("SELECT * FROM fgusers4 WHERE id=$id");
    $image = mysql_query("SELECT * FROM fgusers4 WHERE id=$id") or die('Error - Could not perform the query: ' . mysql_error());
    echo $image;
    $image = mysql_fetch_assoc($image);
    $image = $image["image"];

//  header("cotent-type: image/jpeg");

    echo $image;
?>
 it gives this error and does not upload image however if i continue to use my query it does store and display the image but shows warnings

( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined index: id in C:\wamp\www\get.php on line 5
Call Stack
#   Time    Memory  Function    Location
1   0.0010  143336  {main}( )   ..\index.php:0
2   0.0015  146528  include( 'C:\wamp\www\get.php' )    ..\index.php:13
Error - Could not perform the query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

( ! ) Notice: Undefined index: id in C:\wamp\www\get.php on line 5

There was no query parameter named "id" specified when calling this script.

what does that mean what i have to do now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.