0

I'm trying to print img paths from db in a while to print them all, but it's only printing the first one.

if ($stmt = $mysqli->prepare("  SELECT *
                                FROM user_uploads
                                WHERE userID = ?
                                LIMIT 1")) {
    $stmt->bind_param('i', $_SESSION['user_id']);
    $stmt->execute();

    $result = $stmt->get_result();

    while ($row = $result->fetch_array()) {

        if (!empty($row['description'])) {
            $desc = $row['description'];
        } else {
            $desc = 'Descripción de la imagen';
        }

        echo '
                <div id="form-main">
                    <form class="form" action="" method="post" name="image">
                        <img src="/images/user_uploads/' . $row['image_path'] . '" alt="' . $row['image_name'] . '" height="600" width="600">
                        <p class="text">
                            <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input image" id="image" placeholder="' . $row['image_name'] . '"/>
                        </p>
                        <div class="submit">
                            <input type="submit" value="Cambiar nombre" id="button-blue"/>
                            <div class="ease"></div>
                        </div><br><br><br><br>
                    </form>
                    <form class="form" action="" method="post" name="image">
                        <p class="text">
                            <textarea name="text" type="text" class="validate[required,length[6,300]] feedback-input comment" id="comment" placeholder="' . $desc . '"></textarea>
                        </p>
                        <div class="submit">
                            <input type="submit" value="Cambiar descripción" id="button-blue"/>
                            <div class="ease"></div>
                        </div>
                    </form>
                </div>';
    }
}

In my testing, I have 4 image paths saved in the table. I don't see what's wrong.

Thanks in advance!

3
  • 6
    remove the limit from your query Commented Oct 12, 2014 at 21:04
  • 1
    LIMIT 9999999999 ;) or just remove the LIMIT 1 altogether. Commented Oct 12, 2014 at 21:16
  • Oh! I see, I just copied from another script and I forgot to remove that. Thank you! :) Commented Oct 13, 2014 at 1:10

2 Answers 2

2

You have a LIMIT clause in your query that basically limits your result set to just 1 record. Remove it to retrieve all records.

Sign up to request clarification or add additional context in comments.

Comments

1

remove the limit from your query, You are limiting your result there.

and than your if statement should look like this.

if ($stmt = $mysqli->prepare("  SELECT *
                                FROM user_uploads
                                WHERE userID = ?
                                ")) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.