PHP and MYSQL - checkboxes

Discussion in 'PHP' started by LaWoure, Dec 16, 2008.

  1. #1
    Hi there, all !

    I need your help
    My problem is:

    I have data in my mysql db (users)
    So I query it using while loop and they are one user per line with checkbox

    I want to if I check some of checkboxes so according username after submit will be deleted from database.

    Please help me, I need it - your help
     
    LaWoure, Dec 16, 2008 IP
  2. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just run a loop again when you submit the form that checks those that are ticked.
     
    Yesideez, Dec 16, 2008 IP
  3. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's some code I've just copied from one of my scripts that selectively deletes messages from an inbox system I made...
      $msgcount=0;
      if ($_POST['delsel']) {
        $fetch=mysql_query("SELECT * FROM inbox WHERE `userid`='$uid' ORDER BY isread ASC, whenin DESC");
        while ($inbox=mysql_fetch_array($fetch)) {
          $msgid=$inbox[msgid];
          if ($_POST['chk'.$msgid]) {
            mysql_query("DELETE FROM inbox WHERE `msgid`='$msgid' LIMIT 1");
            $msgcount++;
          }
        }
        if ($msgcount>0) {$message="Messages deleted: ".$msgcount;} else {$message="You didn't select any messages to delete";}
      }
    PHP:
     
    Yesideez, Dec 16, 2008 IP
  4. LaWoure

    LaWoure Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Please write it concrete ... please
     
    LaWoure, Dec 16, 2008 IP
  5. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I posted some code just before that... let me know if that's what you mean by "concrete"
     
    Yesideez, Dec 16, 2008 IP
  6. LaWoure

    LaWoure Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    And how to name (or do) checkboxes ?
     
    LaWoure, Dec 16, 2008 IP
  7. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I thought you were going to ask that :D

    I've just looked at my inbox script and I would post the code that makes them but its VERY long so if you want to hang on a couple secs I'll write some code for you...
     
    Yesideez, Dec 16, 2008 IP
  8. LaWoure

    LaWoure Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    ofcs take your free time :)
     
    LaWoure, Dec 16, 2008 IP
  9. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Here's some code that will go through my inbox and make a list of checkboxes followed by the inbox subject title:
      $fetch=mysql_query("SELECT * FROM inbox WHERE userid='$uid' ORDER BY isread ASC, whenin DESC");
      $htmInbox="";
      while ($inbox=mysql_fetch_assoc($query)) {
        $htmInbox.='<input type="checkbox" name="chk'.$inbox['msgid'].'" /> '.$inbox['subject'].'<br />';
      }
    PHP:
    Generates HTML like this...
    <input type="checkbox" name="chk1" /> A message title<br />
    <input type="checkbox" name="chk2" /> Some other message<br />
    <input type="checkbox" name="chk3" /> And another<br />
    <input type="checkbox" name="chk4" /> One last message<br />
    Code (markup):
    btw, msgid is the unique identifier in my database for that table.
     
    Yesideez, Dec 16, 2008 IP
  10. LaWoure

    LaWoure Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Still i dont understand one thing...
    How to name checkboxes... because its php not html....

    so here is my plan:

    while (select from db - usernames)
    $username = username from db
    echo $username;
    echo checkbox

    after submit i want:

    delete checked usernames from this database
    count how many boxes were checked
    write all usernames which were checked



    HELP HELP HELP
     
    LaWoure, Dec 16, 2008 IP
  11. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I need the following:
    Names of the identifier and username
     
    Yesideez, Dec 16, 2008 IP
  12. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Here's some code for you to play with...
    <?php
      if ($_POST['delsel']) {
        $intCounter=0;
        $arrDeleted=array();
        $query=mysql_query("SELECT `id`,`username` FROM `users` ORDER BY `username` ASC");
        while ($row=mysql_fetch_array($query)) {
          if ($_POST['chk'.$row['id']]) {
            mysql_query("DELETE FROM `users` WHERE `id`='".$row['id']."' LIMIT 1");
            array_pop($row['username']);
            $intCounter++;
          }
        }
        if ($intCounter>0) {
          $msg="Messages deleted: ".$intCounter;
        } else {
          $msg="You didn't select any messages to delete";
        }
      }
    ?>
    <html>
    <head>
      <title>User Deleter Thingamebob</title>
      <style type="text/css">
        table.boxed {border: 1px #000000 solid;}
        table.boxed td {border: 1px #000000 solid;}
      </style>
    </head>
    <body>
      <?=$msg?><br /><br />
    <?php
      if ($intCounter>0) {
        echo $arrDeleted[0];
        if ($intCounter>1) {
          for ($i=1;$i<count($arrDeleted);$i++) {
            echo ', '.$arrDeleted;
          }
        }
        echo '<br /><br />';
      }
    ?>
      User list:
      <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
      <table cellspacing="0" class="boxed">
    <?php
      $query=mysql_query("SELECT `id`,`username` FROM `users` ORDER BY `username` ASC");
      while ($row=mysql_fetch_assoc($query)) {
        echo '<tr><td><input type="checkbox" name="chk'.$row['id'].'" /> '.$row['username'].'</td></tr>';
      }
    ?>
      </table>
      <input type="submit" name="delsel" value="Delete Selected Users" />
      </form>
    </body>
    </html>
    Code (markup):
    That should do just what you want but I haven't tested it - back up your table before trying it.
     
    Yesideez, Dec 16, 2008 IP
  13. LaWoure

    LaWoure Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Very nice explain for my problem
    Thanx a lot... I am gonna try it online
     
    LaWoure, Dec 16, 2008 IP