0

i'm new to php and i have this php script, but for some reason its not working. What i want is when the checkbox is checked, the value of the specific table will increase by 1 after i click the 'submit' button.

php:

<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $selected){
$sql = "UPDATE users SET marks = marks + 2 WHERE id = $selected";
mysqli_query($sql);
}
}
}
?>

and here is my table code which collect the data:

<form action="#" method="post">
    <table class='default'>
    <tr>
        <th>Delete</th><th>Username</th><th>Email</th><th>First Name</th><th>Last Name</th><th>Join Date</th><th>Last Sign In</th><th>Logins</th>
    </tr>
        <tbody>

<?php foreach ($userData as $v1) { ?>

            <tr>
            <td><input type="checkbox" name="check_list[]" value="<?=$v1->id?>" /></td>
            <td><?=$v1->username?></td>
            <td><?=$v1->fname?></td>
            <td><?=$v1->lname?></td>
            <td><?=$v1->marks?></td>
            <td><?=$v1->join_date?></td>
            </tr>
            <?php } ?>

        </tbody>
    </table>
    <input type="submit" name="submit" value="Submit"/>
</form>

where am i doing wrong?

kang08
  • 1
  • 1
    can you please specify the error/output? – Riddhi Rathod Jul 08 '16 at 12:51
  • Without knowing what's actually happening, it's hard to help – Taegost Jul 08 '16 at 12:54
  • Please read this article about MySQL injection: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php Also, don't use if(isset($_POST['submit'])){ Some browsers do filter the submit button out. use if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) { Also, don't use =$var?> this can cause problems when migrating to other servers/hosting. use instead. – Scriptman Jul 08 '16 at 12:55
  • @Scriptman What sort of problems would `= ?>` cause? – apokryfos Jul 08 '16 at 12:56
  • @apokryfos, That is called PHP Shorttags and can be disabled in server configuration (php.ini, "short_open_tags") There is a possibility that shorttags will be removed in the future. – Scriptman Jul 08 '16 at 13:00
  • `short_open_tags` doesn't affect the `= ?>` tags since version 5.4, and I don't think not using something on the off-chance of it being removed in the future is a good approach since you'll end up not using anything. – apokryfos Jul 08 '16 at 13:06
  • @Riddhi Rathod There's no error or any message shown. When i clicked the submit button, the page just refresh and the value is not updated. UPDATE: Here is the error: Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/u523029837/public_html/marks.php on line 106: `mysqli_query($sql);` – kang08 Jul 09 '16 at 13:28

0 Answers0