0

Hi, Ive been follow the tutorial in the youtube . I need to upload images to my webpage but the code seem not working. It also doesnt not appear any errors from the code.
Can someone please have a look at my code, please?

<!--
dbase.php
to connect between php and database
--> <?php

define("DATABASE_HOST", "localhost");
define("DATABASE_USER", "root");
define("DATABASE_PASSWORD", "password");

//to establish a connection to database and save in $con
//$conn = mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD);

//make connection with database
$conn = mysql_connect("localhost", "root", "") or die (mysql_error());

//if connection failed then display error
if (!$conn)
{
die("COULDNT CONNECT TO DATABASE !!!");
}

//to select 1 particular database to be used
mysql_select_db("imagedb", $conn) or die("COULDNT OPEN PRODUCT DATABASE !!!");

//set the default time zone(msia)
date_default_timezone_set('Asia/Kuala_Lumpur');

?>

index.php

<?php

    include('./classes/image_class.php');

    $obj_image = new Image();

    if(@$_POST['Submit'])
    {
        $obj_image->image_name=str_replace("'","''", $_POST['txt_image_name']);
        $obj_image->image=str_replace("'","''", $_POST['txt_image']);

        $obj_image->insert_into_image();
    }

?> 
<!DOCTYPE> 
<html>
<head> 
<title> Upload pic </title> </head> 
<center> Upload New Book</center> 
<br> <br> 
<center> 
<form method="post" enctype="multipart/form-data">
<table border="1" width="80%"> 
<tr> 
<th  width="50%">Image Name</th> 
<td width="50%"> <input type="text" name="txt_image_name"></td> </tr> 
<tr> 
<th  width="50%">Upload Image</th> 
<td width="50%"> <input type="file" type="file" name="txt_image"></td> 
</tr> 
<tr> 
<td></td> 
<td> <input type="submit" name="submit" value="Save"></td> 
</tr>
</table> 
</form>
</center>
<body> 
</body> 
</html>

image_class.php

<?php

include('./db/dbase.php');

        class Image
        {
            var
                $image_id,
                $image_name,
                $image;

                function insert_into_image()
                {
                    if($_FILES["txt_image"])
                    {
                        $tempname=$_FILES["txt_image"]["tmp_name"];
                        $originalname=$_FILES["txt_image"]["name"];
                        $size=($_FILES["txt_image"]["size"]/5242880) . "MB<br>";
                        $type=$_FILES["txt_image"]["type"];
                        $image=$_FILES["txt_image"]["name"];
                        move_uploaded_file($_FILES["txt_image"]["tmp_name"], "images/".$_FILES["txt_image"]["name"]);
                    }

                    $query="insert into imagetab (image_name, image) values 
                    (
                        '$this->image_name',
                        '$image'
                    )";

                    if(mysql_query($query))
                    {
                        echo"<script language='javascript' type='text/javascript'>
                            alert('IMAGE UPLOADED');
                            </script>";
                    }
                    else
                    {
                        echo"<script language='javascript' type='text/javascript'>
                            alert('IMAGE NOT UPLOADED');
                            </script>";
                    }
                }
        }

?>

if the upload success, it suppose to display a pop up massage to acknowledge that the image is successfull save in database.
please, have a look at my code, thank you.

Edited by Izzah_2

2
Contributors
2
Replies
15
Views
1 Year
Discussion Span
Last Post by Izzah_2
0

What exactly is not working? Is the javascript code generated in the page source? Are there any entries/records in your table?

If the javascript is generated, then it is before the doctype even. Am not sure that is the right place to put a script tag.

Edited by pritaeas

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.