0

I am trying to display image and details from database where i have stored. I have got code which connects to database but doesn't fetch. Edited: Thanks guys i was able to fetch but I have incurred anther problem. I have stored image as blob. so when i queried it displayed some mumbojumbo language. screenshot of the result screenshot of how i stored image ....... echo"Connected to db" ;

if (isset($_POST['info'])){
    $info= 'info';
    if ($info== 1){
        $sql= "SELECT name,img,price FROM product WHERE price<400";
        $result=mysqli_query($conn,$sql);
        while ($row = mysqli_fetch_assoc($result)){
            echo "<div class= 'row'>";
            echo    "<div class = 'col-sm-3'>";
            echo        "<p>".$row['img']."</p><br/>";
            echo        "<h3>".$row['name']."</h3><br/>";
            echo        "<h4>£".$row['price']."</h4><br/>";
            echo    "</div>";
            echo "</div>";
        }
    }

exit();

Here is my jquery code where it prints the the output of checkbox and it echo connected to database but can't fetch.

$(".id_price").each(function() {
if ($(this).is(":checked")) {
    var check = $(this).val();
    console.log(check);
    $.post('database.php',{info:check},function(response){
        $(".product").html(response).show(); 

    });

}

});

Kiran
  • 15
  • 1
  • 7

2 Answers2

0
    echo"Connected to db" ;

if (isset($_POST['info'])){
    $info= 'info'; // <----------------
    if ($info== 1){
        $sql= "SELECT name,img,price FROM product WHERE price<400";
        $result=mysqli_query($conn,$sql);
        while ($row = mysqli_fetch_assoc($result)){
            echo "<div class= 'row'>";
            echo    "<div class = 'col-sm-3'>";
            echo        "<p>".$row['img']."</p><br/>";
            echo        "<h3>".$row['name']."</h3><br/>";
            echo        "<h4>£".$row['price']."</h4><br/>";
            echo    "</div>";
            echo "</div>";
        }
    }

exit();

You set $info == "info" and then check if $info == 1

Try this:

    .......
echo"Connected to db" ;

    if (isset($_POST['info'])){
                $sql= "SELECT name,img,price FROM product WHERE price<400";
                $result=mysqli_query($conn,$sql);
                while ($row = mysqli_fetch_assoc($result)){
                    echo "<div class= 'row'>";
                    echo    "<div class = 'col-sm-3'>";
                    echo        "<p>".$row['img']."</p><br/>";
                    echo        "<h3>".$row['name']."</h3><br/>";
                    echo        "<h4>£".$row['price']."</h4><br/>";
                    echo    "</div>";
                    echo "</div>";

            }
        }
        exit();
tasosxak
  • 109
  • 10
  • Thanks it queried but it displayed some random fonts like info but not image.(i stored image as blob, does it affect? – Kiran Mar 27 '17 at 11:10
  • if you want to show an image use this : echo "" , . You should store in database the URL of image (the path) – tasosxak Mar 27 '17 at 11:14
  • I used php admin for that. So while i was storing images, i copied and pasted the image as blob. I tried like you said but it still prints same. Also i have got screenshot above. – Kiran Mar 27 '17 at 11:22
  • Your code is " echo "

    ".$row['img']."


    ";" for the image, did you chanced the tags to "" ?
    – tasosxak Mar 27 '17 at 11:26
  • You should take into account that the image's path is relative. Can you put the line of php code that echo the image, and an example of image's path which saved in database? – tasosxak Mar 27 '17 at 11:36
  • for php copied and pasted yours and screenshot is above. Thnx – Kiran Mar 27 '17 at 11:41
  • still same result no image displayed but now it doesn't print random info, it just displays icon :( – Kiran Mar 27 '17 at 11:57
  • if your image's path is C://User/Desktop/image.jpg you can add all the path in database or you can move the image file to the htdocs folder in same place with php code, and save in database only the image's name (image.jpg), then you can use this path in your php code like : echo ""; – tasosxak Mar 27 '17 at 12:03
  • Thanks very much, it worked now.You saved my life :) – Kiran Mar 27 '17 at 12:13
  • If you appreciate this answer, please do not forget to upvote and accept, to help other members which have the same question. You are welcome :) – tasosxak Mar 27 '17 at 12:16
  • One last question, how do i compare the different values of checkbox if i had to displayed the image according to the checked value? – Kiran Mar 27 '17 at 13:58
  • may you are looking for : http://stackoverflow.com/questions/31166862/change-image-on-radio-button-click-using-js-or-html – tasosxak Mar 27 '17 at 14:50
0
$info= 'info';
if ($info== 1){

$info will always equal 'info' so it will not attempt the query.

Doug
  • 1