Wabbit Wabbit - 11 months ago 47
PHP Question

update data into mysql database by clicking on a link using PHP and HTML

Is there a way to update data in a MySQL DB when the user clicks on a link?
I'm creating a voting app. It allows users to store questions to be used instantly or for a later use. When the user is viewing his created questions, he/she can click on a link that will update a column in the database that will store either 1 or 0 based on the question ID. (1 means that the question is available to vote and 0 is the opposite). I have written the code to retrieve questions from the DB. and now I am trying to write the code that will update question data by clicking on a link.

What I can't figure is how to get the question ID where the user clicked the link so then I can update the DB.
Excuse me if my explanation is not clear. English is my second language and I was trying hard to communicate this better.

Answer Source

You can do something like this:

        $username = $_COOKIE['username'];
        $sql = "SELECT * FROM QUESTIONS WHERE username ='$username'";
        $result = mysql_query($sql) or die(mysql_error());    
        if (mysql_num_rows($result) > 0) {

        //---------- new
            $viteID = (int) $_GET['voteID'];
            // your update query

            <table width="50%" cellpadding="5" cellspacing="0" border="1">
                <tr><td><b>Question ID</b></td><td><b>Question</b></td><td><b>Active/Inactive</b></td></tr>
                $html = '';
                while ($row = mysql_fetch_row($result)) {
                    $html .= '<tr>';
                    $html .= '<td>' . $row[0] . '</td>';
                    $html .= '<td>' . $row[1] . '</td>'; 
                    $activity = $row[3];

                    // ---- new
                    $html .= '<a href="'.$_SERVER['PHP_SELF'].'?id='.$row[0].'">Vote Up</a>';               

                    //if active = 1, do the following
                    if ($activity == 1) {
                        $html .= '<td>' . '<a href=markAsInActive.php>Active</a>' . '</td>';
                    } else {
                        $html .= '<td>' . 'Inactive' . '</td>';
                    $html .='</tr>';

                echo $html;