www.webdeveloper.com
Results 1 to 3 of 3

Thread: how to show the image from database

  1. #1
    Join Date
    Aug 2013
    Posts
    1

    how to show the image from database

    how can i restrict that if there is the status of image is 1 in the database, show the picture in the php form, else no.
    <br>
    require ("connection.php");
    $r="";
    $i=1;
    echo "<form method='post' action='question1.php'>";
    echo "<center>Questionnaire 1<br>Total Questions 10 in Page 1</center>";

    $s="select * from qbank_dump where uname='".$_SESSION['user']."' and sno<=10 order by sno asc";
    $q=mysql_query($s) or die(mysql_error());
    while($r=mysql_fetch_array($q))
    {
    echo "<div style='margin-left:100px; margin-top:20px;'>";
    echo "&nbsp;Q:&nbsp;".$r[0]." .&nbsp; ".$r[3]."<br>";

    $s="select * from qbank_dump where uname='".$_SESSION['user']."' and sno<=10 order by sno asc";
    $q=mysql_query($s) or die(mysql_error());
    if($r[9] == 1 )
    {
    echo "<img src='upload_image/".$r[9]."' width=400>";
    }
    echo "<table><tr><td>Ans:</td></tr><tr><td> A)</td><td><input type='radio' name='r1[$i]' value='$r[4]' ></td><td>".$r[4]."</td></tr><tr><td>B)</td><td><input type='radio' name='r1[$i]' value='$r[5]'></td><td>".$r[5]."</td></tr><tr><td>C)</td><td><input type='radio' name='r1[$i]' value='$r[6]'></td><td>".$r[6]."</td></tr><tr><td>D)</td><td><input type='radio' name='r1[$i]' value='$r[7]'></td><td>".$r[7]."</td></tr></table></div><br><input type=hidden name='ans[$i]' value='".$r[8]."' > <input type=hidden name='q1[$i]' value='".$r[1]."'>";
    $i++;
    }

    ?>
    <center>
    <input type="submit" value="S U B M I T" name="sbt1" style="background-image:url(image/29.jpg); height:50px; width:200px; color:#FFFFFF; font-weight:bold;" />
    <br /><br />
    Click here to end this test...<input type="submit" value="Finish Online Test" name="sbt2" style="background-color:#CC00FF; color:#FFFFFF; font-weight:bold;" /></form>
    <br />
    1&nbsp;&nbsp;||&nbsp;Next Page>>>&nbsp;&nbsp;<a href="question2.php">2</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="question3.php">3</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">4</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">5</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">6</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">7</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">8</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">9</a>&nbsp;&nbsp;||&nbsp;<a href="#">10</a>
    </center>

    </body>
    </html>

  2. #2
    Join Date
    Mar 2011
    Posts
    1,133
    At a start, you're re-using the same code both times your script tries to access your database, which isn't going to work. In any case, you need to assign unique variables to each instance. It looks like the second database query may have been pasted in by accident, since there is no subsequent mysql_fetch() of any kind. But getting to the point, your code checks to see if $r[9] == 1, and then if it does, it tries to use $r[9] in the 'src' attribute of the <img> tag, which may be the heart of the problem.
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,199
    Pardon me for saying but I think that you need to learn about formatting your code before you continue programming because your code is not readable for one and this does not help with debugging.

    For example, a bit of a tidy and a change of the code
    PHP Code:
    <?php
    require ("connection.php");
    $r="";
    $i=1;
    $s="select * from qbank_dump where uname='".$_SESSION['user']."' and sno<=10 order by sno asc";
    $q=mysql_query($s) or die(mysql_error());


    ?><!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>
    <br>
    <form method='post' action='question1.php'>
    <center>Questionnaire 1<br>Total Questions 10 in Page 1</center>

    <?php 
    while($r=mysql_fetch_array($q)){
        echo 
    "<div style='margin-left:100px; margin-top:20px;'>";
        echo 
    "&nbsp;Q:&nbsp;".$r[0]." .&nbsp; ".$r[3]."<br>";

        if(
    $r[9] == )    {
            echo 
    "<img src='upload_image/".$r[9]."' width=400>";
            }
            echo 
    "<table><tr><td>Ans:</td></tr>";
            echo 
    "<tr><td>A)</td><td><input type='radio' name='r1[$i]' value='$r[4]'></td><td>".$r[4]."</td></tr>";
            echo 
    "<tr><td>B)</td><td><input type='radio' name='r1[$i]' value='$r[5]'></td><td>".$r[5]."</td></tr>";
            echo 
    "<tr><td>C)</td><td><input type='radio' name='r1[$i]' value='$r[6]'></td><td>".$r[6]."</td></tr>";
            echo 
    "<tr><td>D)</td><td><input type='radio' name='r1[$i]' value='$r[7]'></td><td>".$r[7]."</td></tr>";
            echo 
    "</table></div><br>";
            echo 
    "<input type=hidden name='ans[$i]' value='".$r[8]."'>";
            echo 
    "<input type=hidden name='q1[$i]' value='".$r[1]."'>";
    $i++;
    }

    ?>
    <center>
    <input type="submit" value="S U B M I T" name="sbt1" style="background-image:url(image/29.jpg); height:50px; width:200px; color:#FFFFFF; font-weight:bold;" />
    <br /><br />
    Click here to end this test...<input type="submit" value="Finish Online Test" name="sbt2" style="background-color:#CC00FF; color:#FFFFFF; font-weight:bold;" />
    </form>
    <br />
    1&nbsp;&nbsp;||&nbsp;Next Page>>>&nbsp;&nbsp;<a href="question2.php">2</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="question3.php">3</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">4</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">5</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">6</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">7</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">8</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">9</a>&nbsp;&nbsp;||&nbsp;<a href="#">10</a>
    </center>

    </body>
    </html>
    if the radio values are always going to be the same 4,5,6,7 then I would personally make the following changes to reduce the amount of code, make it readable and easier to trace any faults.

    PHP Code:
    <?php
    require ("connection.php");
    $r="";
    $i=1;
    $a=array(4=>"A",5=>"B",6=>"C",7=>"D");
    $s="select * from qbank_dump where uname='".$_SESSION['user']."' and sno<=10 order by sno asc";
    $q=mysql_query($s) or die(mysql_error());


    ?><!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>
    <br>
    <form method='post' action='question1.php'>
    <center>Questionnaire 1<br>Total Questions 10 in Page 1</center>
    <?php
    $output 
    "";
    while( 
    $row mysql_fetch_array($results) ) {
        
    $output .= "<div style='margin-left:100px; margin-top:20px;'>";
        
    $output .= sprintf("&nbsp;Q:&nbsp;%s .&nbsp; %s<br>",$row[0],$row[3]);

        if(
    $row[9] == ){
            
    $output .= sprintf("<img src='upload_image/%s' width=400>",$row[9]);
            }
        
    $output .= "<table><tr><td>Ans:</td></tr>";
        
        
        for(
    $qr=4$qr<7$qr++){
            
    $output .= sprintf("<tr><td>%s)</td><td><input type='radio' name='r1[%s]' value='%s'></td><td>%s</td></tr>",$a[$qr],$i,$row[$qr],$row[$qr]);
            }
        
    $output .= "</table></div><br>";    
        
    $output .= sprintf("<input type=hidden name='ans[%s]' value='%s' >",$i,$row[8]);
        
    $output .= sprintf("<input type=hidden name='q1[%s]' value='%s'>",$i,$row[1]);
        
    $i++;
        }
        
    echo 
    $output;
    ?>
    <center>
    <input type="submit" value="S U B M I T" name="sbt1" style="background-image:url(image/29.jpg); height:50px; width:200px; color:#FFFFFF; font-weight:bold;" />
    <br /><br />
    Click here to end this test...<input type="submit" value="Finish Online Test" name="sbt2" style="background-color:#CC00FF; color:#FFFFFF; font-weight:bold;" /></form>
    <br />
    1&nbsp;&nbsp;||&nbsp;Next Page>>>&nbsp;&nbsp;<a href="question2.php">2</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="question3.php">3</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">4</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">5</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">6</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">7</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">8</a>&nbsp;&nbsp;||&nbsp;&nbsp;<a href="#">9</a>&nbsp;&nbsp;||&nbsp;<a href="#">10</a>
    </center>

    </body>
    </html>
    This does not solve your $row[9] = 1 problem, not sure what you are trying, perhaps you meant to test if it wasn't empty or something...

    I would also advice that your SQL queries are made with UPPERCASE for SQL commands, etc, example...
    PHP Code:
    $s="select * from qbank_dump where uname='".$_SESSION['user']."' and sno<=10 order by sno asc"
    is not very readable but
    PHP Code:
    $s="SELECT * FROM qbank_dump WHERE uname='".$_SESSION['user']."' AND sno<=10 ORDER BY sno ASC"
    is easier to see what is SQL and what is a field, you can also use sprintf to make it easier on the eye like so
    PHP Code:
    $s=sprintf("SELECT * FROM qbank_dump WHERE uname='%s' AND sno<=10 ORDER BY sno ASC",$_SESSION['user']); 
    As for your original question, displaying an image from a database... did you mean pushing the data in to a database and retrieving it from a query? If so then the subject has been covered before and it will be a case of storing the image data as a base64 string, example of how to here on the display side of things http://www.sweeting.org/mark/blog/20...bedded-in-html

    You will find plenty of other examples but this is about the best you can get because it not only shows you but also does it correctly.

    In a nutshell the data:image/gif;base64,<base64String that has been chunked> the string itself could come from a file as in the example but just as easily out of a database query.

    You should not that this can cause issues with performance if your images are large and your visitors are many, so you will need to decide if you are going to do this.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles