1

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

4
  • 2
    1. you are overriding variables.2.dates can not be compare straight forward with < or >.3 from where you got $row['cartname'] suddenly? Commented Nov 14, 2018 at 8:07
  • 1
    Also your SQL code is vulnerable to injection attacks, you shouldn't inject directly your variables in SQL string, but use prepared statements Commented Nov 14, 2018 at 8:09
  • 1
    Can you post here what code should i correct? it would help a lot thanks :( @AlivetoDie Commented Nov 14, 2018 at 8:09
  • 1
    Hi @AlivetoDie, you can post your code from your link, so i can select it as the right answer here. It works! Thank you so much! :) Commented Nov 14, 2018 at 8:27

1 Answer 1

1

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:

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

2 Comments

glad to help you :):)
@tenshi kindly check my edited solution. I have Modified my code as well as added some important information

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.