Angela
Angela

Reputation: 81

Error: Fetching only 1 variable from db in button

My problem is that, i have 3 products from DB. 1 product is supposed to have disabled button, but in total all 3 of them have disabled button which is wrong. I echoed the value of "$dss2" and found that it only calls for 1 product shown below here in my picture link.

$dss2 is supposed to have different value based on its product

I don't know the problem, because these codes are inside my fetching from the database loop. It would helped me a lot. Been working for this error for days..

This is my code for fetching the product's details that is inside the fetching from data loop:

<?php 
    include('connectdb.php');
    $sql = "SELECT * from posted WHERE (seller='$userid') and (prod='$produkto') and (activityset='GROUP')  and (datee = '$araw')";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {             
    $result = $conn->query($sql); ?>
    <?php while($data = $result->fetch_assoc()) { 
    $dc = $data["dateclick"];
    **$dss2 = $data["datestart"];**
    $equaldate = $data["datee"];
    $as2 = $data["activityset"];
    }} ?>

This is my button code:

<!-- Group -->
      <?php
      if ($produkto == $row['cartname']) { 
         if ($dss2 > $datetoday) { ?>
        <button type="button" class="btn btn-info btn-sm" title="Return after 1-2 hours" data-toggle="modal" data-target="#modal-4" disabled>Group <?php echo $groupcount ?>/3</button>
      <?php }
        else { ?>
         <button type="button" class="btn btn-info btn-sm" title="Return after 1-2 hours" data-toggle="modal" data-target="#modal-4" disabled>Group <?php echo $dss2 ?><?php echo $groupcount ?>/3</button>
      <?php } } ?>

Button code are also inside my fetching loop

Upvotes: 1

Views: 58

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to correct your loop+button code like this:

<?php 
    include('connectdb.php');
    $sql = "SELECT * from posted WHERE (seller='$userid') and (prod='$produkto') and (activityset='GROUP')  and (datee = '$araw')";
    $result = $conn->query($sql);
    if($result->num_rows > 0) {             
        while($data = $result->fetch_assoc()) { 
            if ($produkto == $data['cartname']) { // i don't know from where $produkto is coming so check yourself

                if (strtotime($data["datestart"]) > strtotime($datetoday)) { ?>

                <button type="button" class="btn btn-info btn-sm" title="Return after 1-2 hours" data-toggle="modal" data-target="#modal-4" disabled>Group <?php echo $groupcount ?>/3</button><!-- from where you got $groupcount? you have to check yourself-->

                <?php }else { ?>

                    <button type="button" class="btn btn-info btn-sm" title="Return after 1-2 hours" data-toggle="modal" data-target="#modal-4" disabled>Group <?php echo $dss2 ?><?php echo $groupcount ?>/3</button>

            <?php } 
            }
        }

    }
?>

Note: Your SQL code is vulnerable to SQL injection attacks, you shouldn't inject directly your variables in SQL string. To prevent from it, use prepared statements.

References:

Upvotes: 1

Related Questions