0

I already have multiple-image upload which allows to upload maximum 5 images to a bootstrap card. I'm trying to add an image update page to my website where users can delete/update their images separately, one by one. The problem is, I don't know how to set the maximum limit here, because there are people who have 2 images uploaded, so they need 3 more input fields. But if someone has 0 image in the database, he needs 5 more input fields to upload the images. I'm stuck, because I don't know how to show the input fields, depends on the database.
Here's my code:

<?php
$sql = "SELECT id FROM cardimages WHERE cardid= ?";
$stmt = $conn->prepare($sql);
$id = $_GET['id'];
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();

$number = $stmt->num_rows();
echo $number;

if ($stmt->num_rows() > 0){
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
        $number = $stmt->num_rows();
        echo $number;
?>
        <img src="imageView.php?id=<?php echo $row["id"]; ?>" class="img-fluid" alt="" style="width: 30%; margin-bottom: 10px;"/>
        <button type="submit"  class="btn btn-primary btn-xl">Módosítás</button>
        <button type="submit"  class="btn btn-danger btn-xl" style="background-color: red;">Törlés</button>
<?php
    } // while
} else {
    echo '<h5>Nincs megjelenítendo kép.</h5>';
} //if
?>
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >
<input type="file" name="uploaded_file[]" id="file" >

it allows them to upload 5 more images, but there are 2 images already exists in the database. How can I disable or hide the input fields in SERVER side, depends on the database values?

CodeAngry
  • 12,760
  • 3
  • 50
  • 57
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Mar 18 '20 at 10:47
  • Hint: `$stmt->num_rows()` would tell you how many rows were returned. You can then subtract that value from the max number of images you've specified, and use that total in a loop to generate the correct number of ` – ADyson Mar 18 '20 at 10:48
  • I've just tried to add this before my if statement: ```$number = $stmt->num_rows(); echo $number;``` but the output is 0 instead of 2. (This card already has 2 images) – kristof12301 Mar 18 '20 at 11:09
  • I assume you've put that line that _after_ `$stmt->execute();`? – ADyson Mar 18 '20 at 11:12
  • P.S. I've just noticed you are running `$stmt->execute(); $result = $stmt->get_result();` **twice**. That makes no sense. Remove the second copy of those lines. And also `if (mysqli_fetch_assoc($result) != null) {` isn't helping you, because it fetches the first row of the table, before you actually need it. Remove that too. The `while` loop is all you need to do - if there are no results, it just won't loop. You can use `if ($stmt->num_rows() > 0)` to decide whether or not to display a "no results" type of message. – ADyson Mar 18 '20 at 11:18
  • This is how it looks like now: https://gyazo.com/d084d1a3e2fd5ce6f4e832be3001d393 but now it doesn't display my images, just showing the 'nincs megjelenítendő kép' (it means 'there isn't any displayable image') and the output is still 0 – kristof12301 Mar 18 '20 at 11:31
  • Don't post your code on another site, or as pictures, please. There is an "edit" button just under your question here if you need to update it. Please paste it as text, not an image. Thanks. – ADyson Mar 18 '20 at 11:37
  • Sorry, edited it. Could you take a look at it please? – kristof12301 Mar 18 '20 at 11:38
  • No problem. My turn to apologise: I forgot that in order to use run_rows() you have to run store_result() first. See https://stackoverflow.com/a/45901622/5947043 – ADyson Mar 18 '20 at 11:42
  • Yes, it shows '2' now, but also got an error: ```Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\vallalkozok-v2\update.php:338 Stack trace: #0 {main} thrown in``` – kristof12301 Mar 18 '20 at 11:44
  • You have to change to using fetch(), and use bind_result() as well. See https://stackoverflow.com/a/20431902/5947043 – ADyson Mar 18 '20 at 12:02
  • But actually I just thought of an easier way than changing all that - see below. – ADyson Mar 18 '20 at 12:07

1 Answers1

1

In theory you could use $stmt->num_rows() to count the existing rows, but irritatingly this requires you to change quite a few other things in your code related to the way to execute your query and read the results.

However, I've just noticed that you are wanting to generate your <input type="file" elements after you've finished looping over your results to output other things. Therefore, you can just add a simple counter into that loop and use that instead to know how many rows it fetched. Something like this:

<?php
$sql = "SELECT id FROM cardimages WHERE cardid= ?";
$stmt = $conn->prepare($sql);
$id = $_GET['id'];
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$rowCount = 0;

while ($row = $result->fetch_assoc()) {
  $data[] = $row;
  $rowCount++;
?>


  <img src="imageView.php?id=<?php echo $row["id"]; ?>" class="img-fluid" alt="" style="width: 30%; margin-bottom: 10px;"/>
  <button type="submit"  class="btn btn-primary btn-xl">Edit</button>
  <button type="submit"  class="btn btn-danger btn-xl" style="background-color: red;">Delete</button>

<?php
}

if ($rowCount == 0) {
    echo '<h5>Nincs megjelenítendő kép.</h5>';
}
for ($i = 0; $i < (5 - $rowCount); $i++) { 
?>
  <input type="file" name="uploaded_file[]" id="file" >
<?php
}
ADyson
  • 57,178
  • 14
  • 51
  • 63