Hello,

I am trying to create a page for gallery (picture) input:

input_gallery.php

<?php

    include('../includes/koneksi.php');

    $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : '';  
    $confirmation = isset($_POST['confirmation']) ? $_POST['confirmation'] : '';  
    $kategori = isset($_POST['kategori']) ? $_POST['kategori'] : ''; 
    $post_image = isset($_POST['post_image']) ? $_POST['post_image'] : '';
    $page = isset($_POST['page']) ? $_POST['page'] : '';


    //Load image
    if (!empty($_GET['post_id'])){
        $result = mysql_query("SELECT * FROM static_page WHERE post_id =".$_GET['post_id']) or die(mysql_error());
        $data = mysql_fetch_array($result);
        $post_id = $data['post_ID'];
        $page = $data['page'];
        $post_image = $data['post_image'];


    }
    else {
    echo "unable to select data";
    echo "id is empty";
    }

    //Update Image
    if (isset($_POST['update'])){

        if (empty($_POST['post_id']))
            {
            $sqlstr = "INSERT INTO static_page(page, post_image) VALUES('".$page."','".$post_image."')";
            }
        else
        {
            $sqlstr = "UPDATE static_page SET page='".$page."', post_image='".$post_image."' WHERE id=".$_POST['post_id'];

        }
        $result = mysql_query($sqlstr) or die(mysql_error());

        // cek type file & simpan file pada folder
            $file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
            $upload_exts = end(explode(".", $_FILES["image"]["name"]));
            if ((($_FILES["image"]["type"] == "image/gif")
            || ($_FILES["image"]["type"] == "image/jpeg")
            || ($_FILES["image"]["type"] == "image/png")
            || ($_FILES["image"]["type"] == "image/pjpeg"))
            && ($_FILES["image"]["size"] < 2000000)
            && in_array($upload_exts, $file_exts))
            {
            if ($_FILES["image"]["error"] > 0)
            {
            echo "Return Code: " . $_FILES["image"]["error"] . "<br>";
            }
            else
            {
            echo "Upload: " . $_FILES["image"]["name"] . "<br>";
            echo "Type: " . $_FILES["image"]["type"] . "<br>";
            echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";   
            echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>";
            // Enter your path to upload file here
            if (file_exists("c:\xampp\xampp\htdocs\rustoleum/photocms/" .
            $_FILES["image"]["name"]))
            {
            echo "<div class='error'>"."(".$_FILES["image"]["name"].")".
            " already exists. "."</div>";
            }
            else
            {
            move_uploaded_file($_FILES["image"]["tmp_name"],
            "c:\xampp\xampp\htdocs\rustoleum/photocms/" . $_FILES["image"]["name"]);
            echo "<div class='sucess'>"."Stored in: " .
            "c:\xampp\xampp\htdocs\rustoleum/photocms/" . $_FILES["image"]["name"]."</div>";
            }
            }
            }
            else
            {
            echo "<div class='error'>Invalid file</div>";
            }

        //Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
        //if (empty($_REQUEST['id']))   kirimEmail($idKategori, $judul, $news);
        $confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";  

    }
    ?>
    <div align="center">
        <div style="width:800px;text-align:left;">
        <?php echo $confirmation;?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
            <input type="hidden" name="id" value="<?php echo $id; ?>"/>
            <table>
                <tr>
                    <td>Page <font color="red"></font></td>             
                    <td><input type="text" size="50px" name="page" value="<?php echo $page; ?>" ></td>
                </tr>
                <tr>
                    <td>Featured Image </td>              
                    <td>Image</td>                    
                </tr>
                <tr>
                    <td><input size="30px" type="file" name="image" value="<?php echo $image; ?>"/></td>
                </tr>    
                <tr>             
                    <td><br><br><input type="submit" name="update" value="Update"/><input type="reset" name="reset" value="Reset"/></td>
              </tr>
            </table>
        </form>
        </div>
    </div>
</div>    


<p> </p>

<?php include('adminfooter.php'); ?>

unable to select dataid is empty
Notice: Undefined index: image in C:\xampp\htdocs\rustoleum\administrator\input_gallery.php on line 137

Notice: Undefined index: image in C:\xampp\htdocs\rustoleum\administrator\input_gallery.php on line 138

Notice: Undefined index: image in C:\xampp\htdocs\rustoleum\administrator\input_gallery.php on line 139

Notice: Undefined index: image in C:\xampp\htdocs\rustoleum\administrator\input_gallery.php on line 140

Notice: Undefined index: image in C:\xampp\htdocs\rustoleum\administrator\input_gallery.php on line 141
Invalid file
Data telah tersimpan.

line 137 - 141:

$upload_exts = end(explode(".", $_FILES["image"]["name"]));
            if ((($_FILES["image"]["type"] == "image/gif")
            || ($_FILES["image"]["type"] == "image/jpeg")
            || ($_FILES["image"]["type"] == "image/png")
            || ($_FILES["image"]["type"] == "image/pjpeg"))

---------------------

The picture that I just tried to upload is JPEG type.

Hi, before the undefined index notice there is:

unable to select dataid is empty

but this is no where in the above code, it could be related to the error. Also, do not mix backslash and slash as here:

c:\xampp\xampp\htdocs\rustoleum/photocms/

And if you want to use it, at least escape it, because the backslash is an escape character, so:

# escaped:
"c:\\xampp\\xampp\\htdocs\\rustoleum\\photocms\\"

# or with single quotes:
'c:\\xampp\\xampp\\htdocs\\rustoleum\\photocms\\'

# better with slashes:
'c:/xampp/xampp/htdocs/rustoleum/photocms/'

Docs:

ok I fix that one. This time, when I try to upload a picture this error appears:

unable to select dataid is emptyDuplicate entry '0' for key 'post_ID'

Why is it?

Duplicate entry '0' for key 'post_ID'

it happens because post_ID is probably a unique key, in the static_page table, but not the primary key, otherwise the message would be a bit different:

ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

instead of:

ERROR 1062 (23000): Duplicate entry '0' for key 'post_ID'

it happens because post_ID misses the auto_increment attribute: for this reason, to the first inserted image will be assigned a value of 0, when you try to add a new image you get the Duplicate entry '0' error.

You can fix that by adding the attribute to the column:

alter table static_page modify post_ID int unsigned not null auto_increment unique;

ok, I fixed that one. Now, I have another error:

unable to select dataid is empty
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\rustoleum\administrator\input_gallery.php on line 137

line 137: $upload_exts = end(explode(".", $_FILES["image"]["name"]));

The unable to select dataid error is probably in the included file:

include('../includes/koneksi.php');

Regarding line 137 this problem is caused by end() because the first argument must be an array:

This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Ref. http://www.php.net/end

So, save the result of explode() in a variable and then use end() against this variable, for example:

$exts = explode('.', $_FILES["image"]["name"]);
$extension = end($exts);

Now, I receive this message:

unable to select dataid is empty
Invalid file
Data telah tersimpan. (Data is saved)

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.