0

I have a web page which is using php code to search and retrieve images matching tags the user enters. It is using mysql to store the image paths and retrieve the images for the user along with image information.

I'm trying to create a button within each retrieved image information to select it for download. This button will update the database table flag field. My problem is that I can't seem to call the function inside the fetch image/image info loop. I've tried one click methods and $_POST calls.

Thanks in advance

function fun(){
              echo "Hello";
          }

   if(isset($_POST['insert'])){
        $message= "The insert function is called.";
        echo "The insert function is called.";
        
    }
    if(isset($_POST['select'])){
        $message="The select function is called.";
    }


$searchTags = $_POST['searchTag'];

$conn = new mysqli($servername,$username,$password,$db);

if ($conn->connect_error) {
    die("Connection Error: ").$conn->connect_error;
}

$sql = "select pic_id, pic_name, pic_description, pic_tags FROM picture where pic_tags like'%$searchTags%'";

$result = $conn->query($sql);

if ($result->num_rows > 0 ){
    while($row = $result->fetch_assoc()) {
        $imgNumber = $row['pic_id'];
        $imgSrc = $row['pic_name'];
        $imgDescription =$row['pic_description'];
        $imgTags = $row['pic_tags'];
        
        echo "<div class='pictureBox'><br>Pic #".$imgNumber." 
        <br><input type='button' value='NEXT' onclick='document.write('<?php 'fun();'?>);       

        <br><input type='submit' name='insert' value='insert'>
        <br>Pic Name: ".$imgSrc."
        <br>Pic Description: ".$imgDescription."
        <br> Pic Tags: ".$imgTags."
        <br><a href='./galleryEditDB.php?picNumber=".$imgNumber."'>Edit</a>
        <br><img src='".$imgSrc."'><br><br></div><br><br>";
        
        echo $value;

    }
} else {
    echo "0 Results";
}

$conn->close();
5
  • 1
    A submit button won't do anything unless it's inside a form
    – ADyson
    Commented Jun 30 at 12:17
  • Thanks. What about the onclick button calling the function?
    – Cypherwolf
    Commented Jun 30 at 12:46
  • 1
    That will just inject PHP as plain text into the DOM. PHP is server side, onclick is client side. You could send an AJAX request. Commented Jun 30 at 13:00
  • 1
    You also are open to SQL injections with this. Use prepared statements. This also likely won't perform well in the future if you have a lot of records. pic_tags cant use an index, might want to look into full text indexing. Commented Jun 30 at 13:01
  • 1
    Additionally die("Connection Error: ").$conn->connect_error; will not help you die() ends the script so you'd never get the error. Commented Jun 30 at 13:03

0

Browse other questions tagged or ask your own question.