SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Send images to database

    Trying to send images to database, error on line 6 of the php page, HTML first:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <h1><Upload a File></h1>
    
    <FORM enctype="multipart/form-data" method="post" action="insert_data.php">
    <INPUT type="file" name="img1" size="30" />
    <br ><br>
    <INPUT type="submit" name="submit" value="Send this file"/ >
    
    </FORM>
    </body>
    </html>
    PHP Code:
    <?php 

    $db 
    = @mysql_connect("localhost""********""*******");
    @
    mysql_select_db("********""$db");

    $binary_data addslashes(fread(fopen($img1"r"), filesize($img1)));

    $insert_data "INSERT INTO my_imagesVALUES

    ('', '
    $binary_data', '$img1_name', 'img1_size', 'img1_type')";

    @
    msql_query($insert_data);
    ?>

    <html>
    <head>

    <title>Success</title>
    </head>

    <body>
    <h1>Success!</h1>
    <p>You have successfully inseted the following in your database<br>
    <?php echo "$img1_name";?>, a<?php echo "$img1_size";?> byte file with a mime type of 
    <?php echo "$img1_type";?>.</p>


    </body>
    </html>
    Any help, would be great!

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Errors are very useful things; they tell you what you've done wrong!

    What does it say?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $insert_data = "INSERT INTO my_imagesVALUES
    Could this be it? the missing space?
    my_imagesVALUES => my_images VALUES

  4. #4
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    This line won't help
    Code:
    @msql_query($insert_data);
    


    It should be:
    Code:
    @mysql_query($insert_data);
    

    "Don't you just love it when you solve a programming bug only to create another."

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    o.k. corrected the couple of mistakes, this is what the output is:
    Code:
    Warning: fopen() [function.fopen]: Filename cannot be empty in /customers/designer-drew.com/designer-drew.com/httpd.www/blog/insert_data.php on line 6
    
    Warning: fread(): supplied argument is not a valid stream resource in /customers/designer-drew.com/designer-drew.com/httpd.www/blog/insert_data.php on line 6
    
    Success!
    You have successfully inseted the following in your database
    , a byte file with a mime type of .

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this link just for storing images on a server, not in a database. What is wrong with this script, it came out of "mysql in 24 hours", should be right.
    My aim is to to upload to database, retrieve, show on webpage...

    Regards Drew

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your php book seems to be very outdated. You've also made typos.

    To start, turn up error reporting.
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    Now get rid of every occurrence of the @ operator, which silences errors. Errors need to be seen so you can fix them.

    That, long with reading the other link given to you, should get you going.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    o.k. I got a script to work.

    PHP Code:
    <?php 
    $errmsg 
    ""
    if (! 
    mysql_connect("localhost","*****","****")) 

    $errmsg "Cannot connect to database"

    mysql_select_db("*******"); 
      
    $strSQL "select * from my_images"
    $rsPix mysql_query($strSQL); 
    $numRows mysql_numrows($rsPix); 
    $i 0
      
    while(
    $i $numRows){ 
    ?> 
    <h1><?php echo mysql_result($rsPix,$i,"filetype"); ?></h1> 
    <img src="pix.php?id=<?php echo mysql_result($rsPix,$i,"filetype"); ?>"/> 
    <?php 
    $i
    ++; 

    ?>
    this is what comes up:

    http://designer-drew.com/blog/ret.php
    how do I display the image ??

  10. #10
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    For the image source you only need the folder name then the name of the image: e.g. - "images/mypic.jpg".

    So, something like this: '<img src="images/'.$myimage.'" />' is what you will need. The image name will need to be in the database.
    "Don't you just love it when you solve a programming bug only to create another."

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't really get what you mean, can you retrieve images from a database, or do you have to get them from a seperate folder on your server? Do you replace the code:
    PHP Code:
    <img src="pix.php?id=<?php echo mysql_result($rsPix,$i,"filetype); ?>"/>
    with the one listed?
    Drew

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, that's related to an issue common amongst alot of beginners in image output, but not to you.

    Look at the source of the HTML output, then look at the image directly in the browser, i.e. go to pix.php?id=img1_type.

    That's your problem.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •