Php/Mysql Image Problem

This is a discussion on "Php/Mysql Image Problem" within the PHP Forum section. This forum, and the thread "Php/Mysql Image Problem are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Oct 27th, 2007, 16:17
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Php/Mysql Image Problem

hello, i using the code above i just copy and paste it and change the variables but it is not working the image/ photo is not stored in the databse.. below is the code


Code: Select all
<?php 
       if (array_key_exists('_submit_check', $_POST))
{
    //This is the directory where images will be saved
    $target = "../cj";
    $target = $target . basename( $_FILES['photo']['couponstore']); 
    
    //This gets all the other information from the form 
    
                    $couponstore=$_POST['couponstore'];
                    $coupontitle=$_POST['coupontitle'];
                    $website=$_POST['website'];
                    $pic=($_FILES['photo']['couponstore']); 
                    $detail=$_POST['detail'];
                    $days=$_POST["days"];
                    $date=$_POST['date'];
                    $feat=$_POST['feat'];
                    $sto_cat=$_POST['sto_cat'];
            
    //Writes the information to the database 
   mysql_query("INSERT INTO `deals`  VALUES ('$couponstore', '$coupontitle', '$website', '$detail', '$days', '$date', '$feat', '$sto_cat', '$pic')") ; 
   
            //Writes the photo to the server
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
        { 
    //Tells you if its all ok 
    $msg = "The file ". basename( $_FILES['uploadedfile']['couponstore']). " has been uploaded, and your information has been added to the directory"; 
    } 
    else { 
    //Gives and error if its not 
    $error = "Sorry There Was A Problem Uploading The File."; 
    } 
         } 
    ?>

Last edited by Rakuli; Oct 27th, 2007 at 17:27. Reason: Split off another thread, added link to old thread
Reply With Quote

  #2 (permalink)  
Old Oct 27th, 2007, 17:31
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

Okay, then it is probably your INSERT query

change the line

mysql_query("INSERT INTO `deals` VALUES ('$couponstore', '$coupontitle', '$website', '$detail', '$days', '$date', '$feat', '$sto_cat', '$pic')") ;

to mysql_query("INSERT INTO `deals` VALUES ('$couponstore', '$coupontitle', '$website', '$detail', '$days', '$date', '$feat', '$sto_cat', '$pic')")
or
die (mysql_error());

And see if you are getting any MYSQL errors, you aren't escaping any of your data so there is a good chance something is going wrong with the query
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #3 (permalink)  
Old Oct 27th, 2007, 17:50
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

helo thanks for the response i follow the instruction there is an error occur

errror:

Column count doesn't match value count at row 1
Reply With Quote
  #4 (permalink)  
Old Oct 27th, 2007, 17:56
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

Okay, so you need to make sure that you have the right amount of columns in your MYSQL table.. Your query specifies 9 values so you have to make sure that your table has only 9 columns and you are inserting them in the right order...

If you have more than 9 columns or want a different order you will have to specify the column names in your query..

eg

PHP: Select all

mysql_query("INSERT INTO deals (column1, column2, column3, column4, column5, column6, column7, column8, column9)  VALUES ('$couponstore', '$coupontitle', '$website', '$detail', '$days', '$date', '$feat', '$sto_cat', '$pic')") ; 

Replacing 'columnx' with the name of the columns in your table..

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #5 (permalink)  
Old Oct 27th, 2007, 17:58
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Storing Image in Database

helllo

i have a script below in which storing of image in the database but there i have a problem with the script it will not store images into the dbase

PHP: Select all

<?php 
       
if (array_key_exists('_submit_check'$_POST))
{
    
if (
$_FILES) {
  
$image_types = Array ("../cj/jpeg""../cj/gif");
                        
  if (
is_uploaded_file ($_FILES[userfile][tmp_name])) { 
    
$userfile  addslashes (fread 
                 
(fopen ($_FILES["userfile"]["tmp_name"], "r"), 
                 
filesize ($_FILES["userfile"]["tmp_name"])));
    
$file_name $_FILES["userfile"]["name"];
    
$file_size $_FILES["userfile"]["size"];
    
$file_type $_FILES["userfile"]["type"];

                    
$couponstore=$_POST['couponstore'];
                    
$coupontitle=$_POST['coupontitle'];
                    
$website=$_POST['website'];
                    
$detail=$_POST['detail'];
                    
$days=$_POST["days"];
                    
$date=$_POST['date'];
                    
$feat=$_POST['feat'];
                    
$sto_cat=$_POST['sto_cat'];

    if (
in_array (strtolower ($file_type), $image_types)) {
      
$sql "INSERT INTO deals "
             
"(couponstore, coupontitle, image_type, image, image_size, image_name, website, detail, days, date, feat, sto_cat)  ";
      
$sql.= "VALUES (";
      
$sql.= "'{$couponstore}','{$coupontitle}','{$website}','{$detail}','{$date}','{$days}','{$feat}','{$sto_cat}','{$file_type}', '{$userfile}', '{$file_size}', "
             
"'{$file_name}', NOW())";
             
        
$result=mysql_query($sql,$dbh) or die(mysql_error());
      
Header("Location:".$_SERVER["PHP_SELF"]);
      exit(); 
        } 
    }
  }

    
?>
Please anyone need your help im stack..

Last edited by c010depunkk; Oct 28th, 2007 at 09:04. Reason: code became php
Reply With Quote
  #6 (permalink)  
Old Oct 27th, 2007, 18:15
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

Hi There - please post questions relating to the same code into the same thread rather than create a new one.

Okay so you have defined 12 columns but have 13 values in your query so you would be getting a MySQL column/value count error
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #7 (permalink)  
Old Oct 27th, 2007, 18:19
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

ok thanks i have put all the 9 columns in place but when i run the script to my server this is the error:

Warning: move_uploaded_file(../cj) [function.move-uploaded-file]: failed to open stream: Is a directory in /home/simply22/public_html/admin/deal/form.php on line 33

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpCj3Ne3' to '../cj' in /home/simply22/public_html/admin/deal/form.php on line 33
Reply With Quote
  #8 (permalink)  
Old Oct 27th, 2007, 18:25
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

You need to use the absolute path to the folder you want to upload to...

eg.

$target = '/home/users/path/to/cj';

or

$target = 'C:/path/to/cj';

or if the folder is in the root of the site

$target = '/cj';
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #9 (permalink)  
Old Oct 27th, 2007, 19:25
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

yes, i got the absolute path of my folder but the problem is the image is not store in the mysgl database folder how store the image inside mysql database folder?
Reply With Quote
  #10 (permalink)  
Old Oct 27th, 2007, 19:29
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

You would need to specify the path of the mysql folder.

The images don't actually go into the database -- unless you store them as a binary string.

The point of thes script is to store the location of the image in the database so PHP knows where to acess it when you retrieve later.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #11 (permalink)  
Old Oct 27th, 2007, 19:48
Up'n'Coming Member
Join Date: Jul 2006
Location: manila
Age: 28
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

how to create binary string and store the images into the database?
Reply With Quote
  #12 (permalink)  
Old Oct 27th, 2007, 20:12
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Php/Mysql Image Problem

the basics are quite simple.

You need to have the mysql column holding the image set to receive binary data.

You can do this by making the column type LONGBLOB.

Then after checking that the file is a valid uploaded one, read it into a string and add it to the database like a normal string..

eg.

$data = mysql_real_escape_string(fread(fopen($_FILES[userfile][tmp_name], "r"), filesize($form_data)));

then add $data as a string like normal
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Image Upload with PHP/MySQL kidreapertronV PHP Forum 5 Jan 31st, 2008 17:08
[SOLVED] Php/Mysql Image Problem longstand PHP Forum 6 Oct 17th, 2007 17:27
Upload image file to MySQL as BLOB csun PHP Forum 4 Aug 22nd, 2007 13:14
What is the best way to link an image with a record in mysql AdRock PHP Forum 2 Aug 1st, 2006 12:10
Displaying an Image in php via path in MYSQL chimp PHP Forum 1 Jul 5th, 2006 09:43


All times are GMT. The time now is 11:45.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43