1

I have a few tables that list different things: questions, replies, and users. The tables are from an admin's pov. On the left side, the code makes a cell that contains a small cancel.png img that's supposed to be a delete button for the row. Right now, it's not doing anything when I click on any of the buttons (there's many) yet the hover cursor animation is triggered when I hover over them. I don't know what's wrong, I've had the JS checked by a different colleague of mine but he works for another facility.

I've tried editing the JS, checked the SQL queries in the PHP, checked the db whether or not it triggered anything there and it didn't. There weren't any good examples to take note from online either.

So here's where the code starts: this is the delete function.

<?php
if(isset($_GET['udel'])) {
    $error = 0;
    $uid = mysqli_real_escape_string($link, $_GET['udel']);
    $query = "DELETE FROM users WHERE id = $uid;";
    if (!mysqli_query($link, $query)) $error = 1;
    if ($error == 0) $msg = 'The user has been deleted.';
    else $msg = 'The user couldn\'t be deleted. Contact the website administrator if the problems persists.';
}
?>

There is php/html that calls another php file to print the (e.g. users) table:

<?php
    printUsersTable(getUsers($link));

?>

It calls this stuff:

<?php // theres code above here
function printUsersTable($users) {
?>

    <table class="neutral" border="0" style="width:500px;">
        <thead>
            <tr>
                <th>DEL</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Admin</th>
                    <th>State</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($users as $user) { ?>
            <tr>
                 <td style="cursor: pointer";><img src="images/cancel.png" class="userDel" rel="<?=$user['id']?>" /></td>
                     <td><?=$user['name']?></td>
                     <td><?=$user['login']?></td>
                     <td><?php if($user['admin'] == 1)  echo 'Y'; else echo 'N'; ?></td>
                     <td><?=$user['state']?></td>
                </tr>

        <?php } ?>
        </tbody>
    </table>
<?php
}
// code continues 
?>

Which is all printed (so it's included) onto that first admin page.

Then it goes to this (on the first admin page like the first part of the code), where the rel attribute should trigger if clicked on:

$(".userDel").click(function() {
    var uid = $(this).attr("rel");
    if(confirm("Are you sure you want to delete user " + uid + "?")) {
       window.location = 'admin.php?udel=' + uid;
    }
   });

I've also tried this, but it doesn't work either:

    $(".userDel").on('click', function(event) {
      event.stopPropogation();
      event.stopImmediatePropogation();
      var uid = $(this).attr("rel");
      if(confirm("Are you sure you want to delete user " + uid + "?")) {
        window.location = 'admin.php?udel=' + uid;
      }
    });

On all my files, the php code comes first, then the JS, then any HTML. I'm out on my ass, can y'all help me out? (Also, I don't have any SQL injection vulns anywhere do I?) >.>

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • This comment will not solve your problem. You should look into prepared statements https://www.php.net/manual/en/mysqli.prepare.php because of https://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string – wizebin Apr 11 '19 at 21:16
  • Did you get any errors? – Nasser Ali Karimi Apr 12 '19 at 02:06
  • 1
    Do you check your database manually that it deletes or not, because you don't have any code to show the result `$msg` created but never used – Nasser Ali Karimi Apr 12 '19 at 02:11
  • Also I recommend to use Ajax that makes easy operations like this.https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.w3schools.com/jquery/jquery_ref_ajax.asp&ved=2ahUKEwjUsa6yvcnhAhUotIsKHUkVB4MQFjABegQIAxAB&usg=AOvVaw0EFKBJF0Mmn-PVwGosenx4 – Nasser Ali Karimi Apr 12 '19 at 02:12

1 Answers1

0

Ok so I figured it out:

  • Some of the code in other places like my main functions file weren't calling the same database (some called the test one, others the live one). So that's was a problem for another issue I was having w/ SMTP.

  • I was including/src-ing jquery's cleditor when I didn't need to use it. I removed calling that in my JS above what I posted and it fixed it. The JS now works as needed and deletes.