1

I have create file index.php for uploading image and after uploaded it will be stored in database and also displayed in home page. Now it works fine.

Now i added more fields like firstname,lastname and other user required fields.

Now it displays problem uploading image.

May i know, what is the exact way to fix it?

Any help would be appreciated.

Thank you!!.

This is index.php:

<html>
<head>
<title>upload images to database</title>
</head>
<body>
<h1>Register Form</h1>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
Upload Photo:<input type= "file" name= "image"><br />
<input type= "submit" value= "upload">
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//connect to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image") or die(mysql_error());
$file = '';
 $file= $_FILES['image']    ['tmp_name'];
 if(!isset($file))
 {
 echo "please select an image";
 }
 else
 {
$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$image_name = mysql_real_escape_string($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size ==FALSE)
{
echo "That's not an image";
}
else
    {
    if(!$insert = mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))
    {
    echo "Problem uploading image";
    }
    else{

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

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

This code works fine when upload image, but when i add, These lines of code, i got problem uploading image

First Name: <input type = "text" name= "fname"><br />
    Last Name:<input type = "text" name= "lname"><br />
    Password :<input type = "text" name = "password"><br />
    Retype-password: <input type = "text" name = "rpassword"><br />
    Email:<input type = "text" name = "email"><br />
    Phone Num: <input type = "text" name = "phone"><br />
    Address: <input type ="text" name = "address"><br />

Can anyone help me, still what code need to add?

selvi
  • 11
  • 2
  • 8

2 Answers2

0

Your question is pretty cryptic and vague, but I am risking some assumptions here

i added more fields like firstname,lastname and other user required fields.

If by this you mean that you added more fields to the database table called upload, then your error is in the SQL below:

...mysql_query("INSERT INTO upload VALUES('','$image_name','$image')"))...

Here you have not mentioned the field names, which is fine if you are inserting values into all fields. If you are not inserting values into all table fields, then you need to specify the exact fields for which these values are being inserted. Something like:

    ...mysql_query("INSERT INTO upload (field1,field2,field3)
                    VALUES('','$image_name','$image')"))... 
raidenace
  • 12,789
  • 1
  • 32
  • 35
0

To see whether, as you suspect, the upload fails because of the newly added html code.
Remove the database part.
The database section is not necessary and only complicates the troubleshooting.

  • The uploadFile will be deleted when script ends.
    You have to move it to a folder inside your root.

bool move_uploaded_file ( string $filename , string $destination )

set to a folder inside your app path

$destination ="images/".$_FILES['image']['name'];   

or set to a absolute folder

$destination ="/home/..../http/pre/rid/25/132596/htdocs/myAppDir/images/"
               .$_FILES['image']['name'];   

look for your path with phpinfo();

enter image description here

$file= $_FILES['image']['tmp_name'];
if ( move_uploaded_file ($file , $destination )) {
   $upOK = TRUE;
 } else {
   $upOK = FALSE;
   echo "File could not be moved : ".$destination;
 }         

phpinfo()

enter image description here

Common Pitfalls

  • The MAX_FILE_SIZE item cannot specify a file size greater than the file size that has been set in the upload_max_filesize in the php.ini file. The default is 2 megabytes.

  • If a memory limit is enabled, a larger memory_limit may be needed. Make sure you set memory_limit large enough.

  • If max_execution_time is set too small, script execution may be exceeded by the value. Make sure you set max_execution_time large enough.


test for errors

switch ($_FILES['image']['error']) {
    case UPLOAD_ERR_OK:
        break;
    case UPLOAD_ERR_NO_FILE:
        echo "No file sent.";
        throw new RuntimeException('No file sent.');
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        echo "Exceeded filesize limit.";
        throw new RuntimeException('Exceeded filesize limit.');
    default:
        echo "Unknown errors";
        throw new RuntimeException('Unknown errors.');
}
// You should also check filesize here.
if ($_FILES['image']['size'] > 1000000) {
    echo "Exceeded filesize limit.";
    throw new RuntimeException('Exceeded filesize limit.');
}

How to set Post Values

[...]
<?php
  if (isset($_POST["fname"])) {$fname=$_POST["fname"];} else {$fname = "";}
  if (isset($_POST["lname"])) {$lname=$_POST["lname"];} else {$lname = "";}
  // et cetera
?>
<form action="index.php" method = "POST" enctype = "multipart/form-data">
    First Name: <input type = "text" name= "fname" value="<?php echo $fname;?>"><br />
    Last Name : <input type = "text" name= "lname" value="<?php echo $lname;?>"><br />
    // et cetera
[...]
moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • Did you see my post, i edited. Now i want like register form. When i click upload button, it should display all the details in home page. Can you help? – selvi Jul 26 '14 at 18:29
  • There are many example on the web . Maybe this can help you. [How to print all POST results when a form is submitted?](http://stackoverflow.com/a/9332766/1322642) – moskito-x Jul 26 '14 at 18:48