Jump to content

Need help don't know how to stores images in a database table field


Recommended Posts

You create a script that takes a get parameter, such as: image.php?id=47

 

<?php

// image.php

$id = db_sanitize( $_GET['id'] );

$stmt = "select img_blob from image_table where id={$id}";

// execute and fetch such that $row is an array of the database fields

header( 'Content-Type: image/jpg' ); // or image/gif, image/jpeg, image/png, etc

header( 'Content-Size: ' . strlen( $row->img_blob ) );

// maybe set some other headers

echo $row->img_blob;

 

// AND MAKE SURE THERE IS NO LEADING OR TRAILING WHITE SPACE IN THIS FILE

?>

 

You gave me this

You create a script that takes a get parameter, such as: image.php?id=47

 

<?php
// image.php
$id = db_sanitize( $_GET['id'] );
$stmt = "select img_blob from image_table where id={$id}";
// execute and fetch such that $row is an array of the database fields
header( 'Content-Type: image/jpg' ); // or image/gif, image/jpeg, image/png, etc
header( 'Content-Size: ' . strlen( $row->img_blob ) );
// maybe set some other headers
echo $row->img_blob;

// AND MAKE SURE THERE IS NO LEADING OR TRAILING WHITE SPACE IN THIS FILE
?> 

 

To embed it around the script I have, but when I embed it around it shows the following error

 

      Fatal error: Call to undefined function db_sanitize() in C:\wamp\www\store\shoes\menu2.php on line 48

 

My code goes as follow

 

 

<?php
$sql = 'SELECT id,Subject,image FROM menusubjects;'; 
$result = mysql_query($sql); 

$subjects = array();
if($result && mysql_num_rows($result)!=0) { 
     
    echo '<ul id="nav-categories">'; 
     
    while($row = mysql_fetch_assoc($result)) { 
        
        $subjects[] = $row;
         
        $uri = 'example1.php?subject='.urlencode($row['id']); 
        $class = !is_null($cat) && $cat==$row['id']?' class="selected"':''; 
         
        echo "\t",'<li',$class,'><a href="',$uri,'">',$row['Subject'].'</a>'; 
             
        if($submenu==false && !is_null($cat) && $cat == $row['id']) { // line 58 
                 
            $sql2 = 'SELECT id,shoename FROM regularmenu WHERE menusubject_id = '.mysql_real_escape_string($cat).';'; 
            $result2 = mysql_query($sql2); 
                 
            if($result2 && mysql_num_rows($result2)!=0) { 
                     
                echo "\n\t\t",'<ul class="submenu">',"\n";     
                 
                while($row2 = mysql_fetch_assoc($result2)) { 
                     
                    $uri2 = $uri.'&menu='.urlencode($row2['id']); 
                    $class2 = !is_null($prod) && $prod==$row2['id']?' class="selected"':''; 
                     
                    echo "\t\t\t",'<li',$class,'><a href="',$uri2,'">',$row2['shoename'],'</a></li>',"\n"; 
                                }                  
                echo "\t\t",'</ul> <!-- end of ul.submenu -->',"\n";                        
            }               
            $submenu = true;             
        }             
        echo '</li>',"\n";         
    }        
    echo '</ul> <!-- end of ul#nav-categories -->',"\n ";
}



// drop in thumbnail menu
if(!empty($subjects)) {
    
    echo '<ul id="nav-subjects-thumbs">',"\n";
    
    foreach($subjects as $sub) {
        
        $uri = 'example1.php?subject='.urlencode($sub['id']); 
        $src = $sub['image'];
        $width = 40;
        $height = 40;
        $alt = $sub['Subject'];

        echo "\t",'<li><a href="',$uri,'"><img scr="',$src,'" width="',$width,'" height="',$height,'" alt="',$alt,'" /></a></li>',"\n";
    
    }
    
    echo '</ul>',"\n";

}

?>

 

 

This script has two queries on top and below a foreach loop at the end where the image value is inside of a array variable called $sub, Notice that the foreach loops uses the first query on top of the script to obtain the values of the array.

 

How can I embed your suggestion script around the script I have so it can show up the pictures?

There is no PHP function called db_sanitize(), which is why you receive the error.

 

I included that function as a reminder to you that you have to sanitize user input (stuff from $_POST, $_GET, etc) before you stick it in your database.  How you sanitize data will depend on the database back end you are using.

 

image.php is a separate script you have to write, you can not wrap your existing script inside of it.  You can not embed images into the HTML page.  Images in HTML are created with an img-tag that has a src-attribute that points at a URL.  The URL it points at should be image.php, which pulls the image data from the database and dumps it out.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.