Hello everyone i have a little problem over here im making share button so i want to update the database on the column 'isShared' with value 1 so here is my code but it doesnt work, all the time is showing me Already shared ticket!

Here is the code:

if(isset($_POST['share'])) {
    $user = getUserData('kladilnica', 'Username');
    $shared1 = getUserData('kladilnica', 'isShared');
    $sql = "SELECT `isShared` FROM kladilnica WHERE Username='".$user."'";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        $shared = $row["isShared"];
        if($shared == 1){
            echo "<b>Already shared ticket!</b>";
        } else if($shared == 0){
            echo "<b>Ticket shared succesfully.</b>";
        }
    }
}

Btw if someone knows a better way of making share button please tell in the comments i got this solution on my own.. thank you very much :)

Member Avatar for diafol

You say you want to update, but you're only doing SELECT (retrieve).

To update:

"UPDATE `kladilnica` SET `isShared` = 1 WHERE `Username`='$user'"

Yes but how can i update when the selection fail ?
And also now giving me error Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\Upstrey\home.php on line 496
the line 496 is 6th in the code above.

Member Avatar for diafol

To be honest I'm a bit lost with your thinking. You want to update if 0 but not if 1?

"UPDATE `kladilnica` SET `isShared` = 1 WHERE `Username`='$user' AND `isShared` = 0"

Then just do a num_rows affected count. If 0 - it means either there's no record for that username or isShared was 1 already.

I want to check if the column isShared is equal to 1 and if is not equal to 1 or its 0 than change it to 1

Any other sulutions please ?
Here is how i show the tickets and share button

$user = getUserData('users', 'Username');
        $sql = "SELECT `Uplata`, `Dobivka`, `match1`, `match2`, `match3`, `match4`, `match5`, `match6`, `match7`, `match8`, `match9`, `match10`, `match11`, `match12`, `match13`, `match14`, `match15`, `match16`, `tip1`, `tip2`, `tip3`, `tip4`, `tip5`, `tip6`, `tip7`, `tip8`, `tip9`, `tip10`, `tip11`, `tip12`, `tip13`, `tip14`, `tip15`, `tip16`, `ticket_id`, `isActive`, `isShared` FROM `kladilnica` WHERE `Username`='$user'";
        $result = $conn->query($sql);
        $count = 0;
        $uplata_total = 0;

        if($result === false) {
            echo "<b>Could not connect to database.</b>";
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
        } else {
            if($result->num_rows == 0) {
                echo "<b>You do not have any tickets.</b>";
            } else if($result->num_rows >= 1) {
                $count = $count + 1;
                while($row = $result->fetch_assoc()) {
                    if($result->num_rows != empty($row['match1'])) {
                        $uplata_total = $row["Uplata"];
                        $dobivka_total = $row["Dobivka"];
                        $ticket_id = $row["ticket_id"];
                        $is_active = $row["isActive"];
                        if($is_active == 1) {
                            echo "<form action='home.php' method='POST'>";
                            echo "<table id='t02' style='margin-bottom: 10px;'><tr><th colspan='2'><input type='submit' name='share' class='share_button' value='Share'/></a></th></tr>";
                            echo "<tr><th colspan='2'>Ticket ".$count." <a style='position: relative; left: 69px;'>Ticket ID: ".$ticket_id."</a></th></tr>";
                            echo "
                                    <tr><td>".$row["match1"]."</td><td>".$row["tip1"]."</td></tr>
                                    <tr><td>".$row["match2"]."</td><td>".$row["tip2"]."</td></tr>
                                    <tr><td>".$row["match3"]."</td><td>".$row["tip3"]."</td></tr>
                                    <tr><td>".$row["match4"]."</td><td>".$row["tip4"]."</td></tr>
                                    <tr><td>".$row["match5"]."</td><td>".$row["tip5"]."</td></tr>
                                    <tr><td>".$row["match6"]."</td><td>".$row["tip6"]."</td></tr>
                                    <tr><td>".$row["match7"]."</td><td>".$row["tip7"]."</td></tr>
                                    <tr><td>".$row["match8"]."</td><td>".$row["tip8"]."</td></tr>
                                    <tr><td>".$row["match9"]."</td><td>".$row["tip9"]."</td></tr>
                                    <tr><td>".$row["match10"]."</td><td>".$row["tip10"]."</td></tr>
                                    <tr><td>".$row["match11"]."</td><td>".$row["tip11"]."</td></tr>
                                    <tr><td>".$row["match12"]."</td><td>".$row["tip12"]."</td></tr>
                                    <tr><td>".$row["match13"]."</td><td>".$row["tip13"]."</td></tr>
                                    <tr><td>".$row["match14"]."</td><td>".$row["tip14"]."</td></tr>
                                    <tr><td>".$row["match15"]."</td><td>".$row["tip15"]."</td></tr>
                                    <tr><td>".$row["match16"]."</td><td>".$row["tip16"]."</td></tr>
                                ";
                            echo "<tr><th colspan='2'>Total Bet: $uplata_total</th></tr>";
                            echo "<tr><th colspan='2'>Total Win: $dobivka_total</th></tr>";
                            echo "</table>";
                            echo "</form>";
                            $count++;
                        }
                    }
                }
            }
        }

Okay so here is my whole code and now it works but i cannot figure out how do i make just to work on the current button with this code its sharing all the tickets...

$user = getUserData('users', 'Username');
        $sql = "SELECT `Uplata`, `Dobivka`, `match1`, `match2`, `match3`, `match4`, `match5`, `match6`, `match7`, `match8`, `match9`, `match10`, `match11`, `match12`, `match13`, `match14`, `match15`, `match16`, `tip1`, `tip2`, `tip3`, `tip4`, `tip5`, `tip6`, `tip7`, `tip8`, `tip9`, `tip10`, `tip11`, `tip12`, `tip13`, `tip14`, `tip15`, `tip16`, `ticket_id`, `isActive`, `isShared` FROM `kladilnica` WHERE `Username`='$user'";
        $result = $conn->query($sql);
        $count = 0;
        $uplata_total = 0;

        if($result === false) {
            echo "<b>Could not connect to database.</b>";
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
        } else {
            if($result->num_rows == 0) {
                echo "<b>You do not have any tickets.</b>";
            } else if($result->num_rows >= 1) {
                $count = $count + 1;
                while($row = $result->fetch_assoc()) {
                    if($result->num_rows != empty($row['match1'])) {
                        $uplata_total = $row["Uplata"];
                        $dobivka_total = $row["Dobivka"];
                        $ticket_id = $row["ticket_id"];
                        $is_active = $row["isActive"];
                        $shared = $row["isShared"];
                        if($is_active == 1) {
                            echo "<form action='home.php' method='POST'>";
                            echo "<table id='t02' style='margin-bottom: 10px;'><tr><th colspan='2'><input type='submit' name='share' class='share_button' value='Share'/></a></th></tr>";
                            echo "<tr><th colspan='2'>Ticket ".$count." <a style='position: relative; left: 69px;'>Ticket ID: ".$ticket_id."</a></th></tr>";
                            echo "
                                    <tr><td>".$row["match1"]."</td><td>".$row["tip1"]."</td></tr>
                                    <tr><td>".$row["match2"]."</td><td>".$row["tip2"]."</td></tr>
                                    <tr><td>".$row["match3"]."</td><td>".$row["tip3"]."</td></tr>
                                    <tr><td>".$row["match4"]."</td><td>".$row["tip4"]."</td></tr>
                                    <tr><td>".$row["match5"]."</td><td>".$row["tip5"]."</td></tr>
                                    <tr><td>".$row["match6"]."</td><td>".$row["tip6"]."</td></tr>
                                    <tr><td>".$row["match7"]."</td><td>".$row["tip7"]."</td></tr>
                                    <tr><td>".$row["match8"]."</td><td>".$row["tip8"]."</td></tr>
                                    <tr><td>".$row["match9"]."</td><td>".$row["tip9"]."</td></tr>
                                    <tr><td>".$row["match10"]."</td><td>".$row["tip10"]."</td></tr>
                                    <tr><td>".$row["match11"]."</td><td>".$row["tip11"]."</td></tr>
                                    <tr><td>".$row["match12"]."</td><td>".$row["tip12"]."</td></tr>
                                    <tr><td>".$row["match13"]."</td><td>".$row["tip13"]."</td></tr>
                                    <tr><td>".$row["match14"]."</td><td>".$row["tip14"]."</td></tr>
                                    <tr><td>".$row["match15"]."</td><td>".$row["tip15"]."</td></tr>
                                    <tr><td>".$row["match16"]."</td><td>".$row["tip16"]."</td></tr>
                                ";
                            echo "<tr><th colspan='2'>Total Bet: $uplata_total</th></tr>";
                            echo "<tr><th colspan='2'>Total Win: $dobivka_total</th></tr>";
                            echo "</table>";
                            echo "</form>";
                            $count++;
                        }
                    }

                    if(isset($_POST['share'])) {
                        if($shared == 0) {
                            echo "Ticket shared sucessfully.";
                            $sql = "UPDATE `kladilnica` SET `isShared`=1 WHERE `ticket_id`='$ticket_id'";
                            $return = $conn->query($sql);
                        } else if($shared == 1) {
                            echo $ticket_id;
                            echo "Ticket already shared!";
                        }
                    }
                }
            }
        }
        $conn->close();
Member Avatar for diafol

I want to check if the column isShared is equal to 1 and if is not equal to 1 or its 0 than change it to 1

This is what my update query does. Have you even bothered trying it?

Any other sulutions please ?

Ok, remind me not to waste my time with you again.

Yes i have tryed you can see in the example above its all in the code ... and now i have a problem with getting the correct ticket button.

Member Avatar for diafol

My apologies Stefan - I was feeling really cranky and made wrong assumptions. OK, as recompense, I shall look into it and advise accordingly - I may take a little while. Again please accept my apologies.

hehe no worries it happen sometimes :D btw when i click any Share button its sharing all the tickets so i think its selecting the whole column of tickets.

Member Avatar for diafol

Ok, perhaps this may work - not tested

<?php 
        $responseText = '';

        //HANDLE POSTED DATA
        //This really should be done in a separate file
        //You shouldn't send a form to itself
        //Send the form to a form handler file and redirect back
        //Anyway...

        $user = getUserData('users', 'Username');

        if(isset($_POST['share'])) {

            $ticket_id = (int) $_POST['share'];

            //Ideally you'd use a prepared statement here as bind the
            //ticket_id and user
            //You MUST use Username here or an usere could sppof the 
            //form and share other users' tickets
            //This way, only the user that owns the ticket can share it

            $sql = "UPDATE `kladilnica` SET `isShared` = 1 
                    WHERE `ticket_id` = '$ticket_id' AND `Username`='$user'";
            $return = $conn->query($sql);
            $responseText = ($conn->affected_rows) 
            //use ->rowCount() if using PDO
                ? "You have successfully shared the ticket $ticket_id" 
                : "The ticket ($ticket_id) was shared already";

        }


        $sql = "SELECT `Uplata`, `Dobivka`, 
            `match1`, `match2`, `match3`, `match4`, 
            `match5`, `match6`, `match7`, `match8`, 
            `match9`, `match10`, `match11`, `match12`, 
            `match13`, `match14`, `match15`, `match16`, 
            `tip1`, `tip2`, `tip3`, `tip4`, 
            `tip5`, `tip6`, `tip7`, `tip8`, 
            `tip9`, `tip10`, `tip11`, `tip12`, 
            `tip13`, `tip14`, `tip15`, `tip16`, 
            `ticket_id`, `isActive`, `isShared` 
        FROM `kladilnica` 
            WHERE `Username`='$user'";

        $result = $conn->query($sql);
        $count = 0;
        $uplata_total = 0;

        if($result === false) {
            echo "<b>Could not connect to database.</b>";
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);

        } else {

            if($result->num_rows == 0) {

                echo "<b>You do not have any tickets.</b>";

            } else if($result->num_rows >= 1) {

                $count = $count + 1;

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

                    if($result->num_rows != empty($row['match1'])) {

                        $uplata_total = $row["Uplata"];
                        $dobivka_total = $row["Dobivka"];
                        $ticket_id = $row["ticket_id"];
                        $is_active = $row["isActive"];
                        $shared = $row["isShared"];

                        if($is_active == 1) {
?>
    <form action='home.php' method='POST'>

        <!-- 
                THIS IS A PROBLEM - id='t02' - you may have multiples of
                this - not allowed as each 'id' on a page must be unique -
                you could leave it out or use a class if you are attaching
                CSS or JS to it. class='t02' would work just as well?

                If you add a hidden input you can attach this to the form
                and you then add an identifier.

                <input type="hidden" value="<?= $ticket_id ?>" name="ticket_id" />

                Alternatively, you could just add a button like this:

                <button name="share" value="<?= $ticket_id ?>" type="submit">Share</button>

                You pick up the value from...
                $_POST['share'] (it will be the ticket_id

                Personally, I'd rather the button method. Hidden inputs
                smack of corner-cutting to me. So, I've replaced your
                submit button with a button(submit)
         --> 

        <table id='t02' style='margin-bottom: 10px;'>
            <thead>
                <tr>
                    <th colspan='2'>
                        <button name="share" value="<?= $ticket_id ?>" type="submit">Share</button>
                    </th>
                </tr>
                <tr>
                    <th colspan='2'>
                        Ticket <?= $count ?>
                        <a style='position: relative; left: 69px;'>Ticket ID: <?= $ticket_id ?></a>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><?= $row["match1"] ?></td>
                    <td><?= $row["tip1"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match2"] ?></td>
                    <td><?= $row["tip2"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match3"] ?></td>
                    <td><?= $row["tip3"] ?></td>
                    </tr>
                <tr>
                    <td><?= $row["match4"] ?></td>
                    <td><?= $row["tip4"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match5"] ?></td>
                    <td><?= $row["tip5"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match6"] ?></td>
                    <td><?= $row["tip6"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match7"] ?></td>
                    <td><?= $row["tip7"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match8"] ?></td>
                    <td><?= $row["tip8"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match9"] ?></td>
                    <td><?= $row["tip9"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match10"] ?></td>
                    <td><?= $row["tip10"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match11"] ?></td>
                    <td><?= $row["tip11"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match12"] ?></td>
                    <td><?= $row["tip12"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match13"] ?></td>
                    <td><?= $row["tip13"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match14"] ?></td>
                    <td><?= $row["tip14"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match15"] ?></td>
                    <td><?= $row["tip15"] ?></td>
                </tr>
                <tr>
                    <td><?= $row["match16"] ?></td>
                    <td><?= $row["tip16"] ?></td>
                </tr>
                <tr>
                    <th colspan='2'>Total Bet: <?= $uplata_total ?></th>
                </tr>
                <tr>
                    <th colspan='2'>Total Win: <?= $dobivka_total ?></th>
                </tr>
            </tbody>
        </table>
    </form>

<?php
                            $count++;
                        }
                    }
                }
            }
        }
        $conn->close();

        echo $responseText;

?>      

@diafol men i can't thank you enough what you do to me ... THANK YOU VERY MUCH

Member Avatar for diafol

No problem. I don't usually go this far, but seeing as I was so rude, it seemed like the least I could do. Come back if something doesn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.