0

I am trying to delete records from tables matching users ID while i delete the user. but somehow it deletes records only from the cv table.

what i am trying is

if($_GET['deluser'] !='1'){

         $qr = "delete from members where member_id IN(".$_GET['deluser'].")";
         $qr = "delete from company where caller_id IN(".$_GET['deluser'].")";
         $qr = "delete from cv where agent_id IN(".$_GET['deluser'].")";
         $st = $db->prepare($qr);
         $st->execute();        

    header('Location: users.php?action=DELETED');
    exit;

what could i be doing wrong?

1 Answers1

5

In your case you overwrite the value in $qr every time so you need to execute it, everyone of them separately,

you need also to fix the SQL injection problem so you can fix it by using bind your data in the execute method or by using bindParam

first, you need to add ? with the same number of input you want to pass

you can check how it work here in this answer

$in  = str_repeat('?,', count(explode(',', $_GET['deluser'])) - 1) . '?';
$qr = "delete from members where member_id IN($in)";
$st = $db->prepare($qr);
$st->execute(explode(',', $_GET['deluser']));   

$qr = "delete from company where caller_id IN($in)";
$st = $db->prepare($qr);
$st->execute(explode(',', $_GET['deluser']));   

$qr = "delete from cv where agent_id IN($in)";
$st = $db->prepare($qr);
$st->execute(explode(',', $_GET['deluser']));      

You can read more about BindParam and Execute in the docs

Dharman
  • 30,962
  • 25
  • 85
  • 135
Joseph
  • 5,644
  • 3
  • 18
  • 44