Problem deleting multiple checkboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beam1985
    New Member
    • Mar 2008
    • 7

    Problem deleting multiple checkboxes

    So my problem lays in the $_POST['delete'] if statement at the bottom operation, I keep getting Notice: Undefined index: delete and also Notice: Undefined index: checkbox. Please let me know if you have any ideas or suggestions im desperate to get this working, its part of my midterm project due tomorrow! Thank you so much


    [PHP]$host="localhos t"; // Host name
    $username="user name"; // Mysql username
    $password="pass "; // Mysql password

    mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
    mysql_select_db ("ragra");

    $sql="SELECT * FROM article";
    $result=mysql_q uery($sql);

    $count=mysql_nu m_rows($result) ;

    echo "<table>";
    echo "<tr>";
    echo "<td>";
    echo "<img src='comcarblog .jpg'>";
    echo "</td>";
    echo "</tr>";
    echo "</table>";
    echo "<table>";
    echo "<tr>";
    echo "<td><a href='main.php' alt='Home'><img src='home.jpg'> </a>";
    echo "</td>";
    echo "<td><a href='' alt=''><img src='blank.jpg' ></a>";
    echo "</td>";
    echo "<td><a href='add.php' alt='Admin'><im g src='admin.jpg' ></a>";
    echo "</td>";
    echo "</tr>";
    echo "</table>";
    echo "<table width='400' border='0' cellspacing='1' cellpadding='0' >";
    echo "<tr>";
    echo "<td><form name='form1' method='post'>" ;
    echo "<table width='400' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCC C'>";
    echo "<tr>";
    echo "<td bgcolor='#FFFFF F'> </td>";
    echo "<td colspan='4' bgcolor='#41a1f 3'>You're about to delete some posts!</td>";
    echo "</tr>";
    echo "</table>";
    echo "<table width='400' border='0' cellspacing='1' cellpadding='0' >";
    echo "<tr>";
    echo "<td align='center' bgcolor='#f3d84 1'>Number</td>";
    echo "<td align='center' bgcolor='#f3d84 1'>Id</td>";
    echo "<td align='center' bgcolor='#f3d84 1'>Title</td>";
    echo "<td align='center' bgcolor='#f3d84 1'>Tagline</td>";
    echo "<td align='center' bgcolor='#f3d84 1'>Author</td>";
    echo "</tr>";

    while($rows=mys ql_fetch_array( $result))
    {


    echo "<tr>";
    echo "<td align='center' bgcolor='#FFFFF F'>";
    echo "<input type='checkbox' name='checkbox" ;
    echo $rows['id'];
    echo "' id='checkbox[]' value='";
    echo $rows['username'];
    echo "'>";
    echo "</td>";
    echo "<td bgcolor='#FFFFF F'>";
    echo $rows['id'];
    echo "</td>";
    echo "<td bgcolor='#FFFFF F'>";
    echo $rows['title'];
    echo "</td>";
    echo "<td bgcolor='#FFFFF F'>";
    echo $rows['tagline'];
    echo "</td>";
    echo "<td bgcolor='#FFFFF F'>";
    echo $rows['author'];
    echo "</td>";
    echo "</tr>";


    }

    echo "<tr>";
    echo "<div>";
    echo "<td colspan='5' align='center' bgcolor='#FFFFF F'>";
    echo "<input name='delete' type='submit' id='delete' value='Delete'> ";
    echo "</td>";
    echo "</div>";
    echo "</tr>";


    if($_POST['delete'] == 'Delete'){
    for($i=0;$i<$co unt;$i++){
    $del_id = $_POST['checkbox'][$i];
    $sql = "DELETE FROM article WHERE id='$del_id'";
    $result = mysql_query($sq l);
    }

    echo "<meta http-equiv=\"refresh \" content=\"0;URL =delete.php\">" ;
    }

    mysql_close();[/PHP]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Is the submit button part of a form?
    I dont see a form.

    Comment

    • beam1985
      New Member
      • Mar 2008
      • 7

      #3
      Originally posted by markusn00b
      Is the submit button part of a form?
      I dont see a form.
      yes its there, Line 32

      Comment

      • beam1985
        New Member
        • Mar 2008
        • 7

        #4
        i was instructed by my teacher to do this

        You probably already have a loop to display each data row. Just add a
        counter to that row....and then echo an input with the final counter
        value as that input value at the end of the form that wraps the table.

        so ive added this into the loop,

        [PHP]echo "<imput type='hidden' NAME='cbnbr' VALUE='$cbnbr' size=60>";
        $cbnbr= $_POST['cbnbr'];
        [/PHP]

        and i tried to reflect that in the bottom, but im flying blind here i really need assistance. thanks for the reply

        [PHP]
        if($_POST['delete'] == 'Delete'){
        for($i=0;$i<$co unt;$i++){
        $del_id = $_POST['cbnbr'][$i];
        $sql = "DELETE FROM article WHERE id='$del_id'";
        $result = mysql_query($sq l);
        }

        echo "<meta http-equiv=\"refresh \" content=\"0;URL =delete.php\">" ;
        }

        mysql_close();
        [/PHP]

        Comment

        • beam1985
          New Member
          • Mar 2008
          • 7

          #5
          this is what im currently looking at codewise, this is what it come out as on my server


          [PHP]<?php

          $host="localhos t"; // Host name
          $username="user "; // Mysql username
          $password="pass "; // Mysql password

          mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
          mysql_select_db ("ragra");

          $sql="SELECT * FROM article";
          $result=mysql_q uery($sql);

          $count=mysql_nu m_rows($result) ;

          echo "<table>";
          echo "<tr>";
          echo "<td>";
          echo "<img src='comcarblog .jpg'>";
          echo "</td>";
          echo "</tr>";
          echo "</table>";
          echo "<table>";
          echo "<tr>";
          echo "<td><a href='main.php' alt='Home'><img src='home.jpg'> </a>";
          echo "</td>";
          echo "<td><a href='' alt=''><img src='blank.jpg' ></a>";
          echo "</td>";
          echo "<td><a href='add.php' alt='Admin'><im g src='admin.jpg' ></a>";
          echo "</td>";
          echo "</tr>";
          echo "</table>";
          echo "<table width='400' border='0' cellspacing='1' cellpadding='0' >";
          echo "<tr>";
          echo "<td><form name='form1' method='post'>" ;
          echo "<table width='400' border='0' cellpadding='3' cellspacing='1' bgcolor='#CCCCC C'>";
          echo "<tr>";
          echo "<td colspan='4' bgcolor='#41a1f 3'>You're about to delete some posts!</td>";
          echo "</tr>";
          echo "</table>";
          echo "<table width='400' border='0' cellspacing='1' cellpadding='0' >";
          echo "<tr>";
          echo "<td align='center' bgcolor='#f3d84 1'>Number</td>";
          echo "<td align='center' bgcolor='#f3d84 1'>Id</td>";
          echo "<td align='center' bgcolor='#f3d84 1'>Title</td>";
          echo "<td align='center' bgcolor='#f3d84 1'>Tagline</td>";
          echo "<td align='center' bgcolor='#f3d84 1'>Author</td>";
          echo "</tr>";

          while($rows=mys ql_fetch_array( $result))
          {


          echo "<tr>";
          echo "<td align='center' bgcolor='#FFFFF F'>";
          echo "<input type='checkbox' name='checkbox[]' value='";
          echo $rows['username'];
          echo "'>";
          echo "</td>";
          echo "<td bgcolor='#FFFFF F'>";
          echo $rows['id'];
          echo "</td>";
          echo "<td bgcolor='#FFFFF F'>";
          echo $rows['title'];
          echo "</td>";
          echo "<td bgcolor='#FFFFF F'>";
          echo $rows['tagline'];
          echo "</td>";
          echo "<td bgcolor='#FFFFF F'>";
          echo $rows['author'];
          echo "</td>";
          echo "</tr>";
          echo "</table>";
          echo "<imput type='hidden' NAME='cbnbr' VALUE='$cbnbr' size=60>";

          $cbnbr= $_POST['cbnbr'];

          }


          echo "<table width='400' border='0' cellspacing='1' cellpadding='0' >";
          echo "<tr>";
          echo "<div>";
          echo "<td colspan='5' align='center' bgcolor='#FFFFF F'>";
          echo "<input name='delete' type='submit' id='delete' value='Delete'> ";
          echo "</td>";
          echo "</div>";
          echo "</tr>";



          if($_POST['delete'] == 'Delete'){
          for($i=0;$i<$co unt;$i++){
          $del_id = $_POST['cbnbr'][$i];
          $sql = "DELETE FROM article WHERE id='$del_id'";
          $result = mysql_query($sq l);
          }

          echo "<meta http-equiv=\"refresh \" content=\"0;URL =delete.php\">" ;
          }

          mysql_close();
          ?>[/PHP]

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            There's an algorithmic mistake.
            Make your file work like this:
            [php]//CONNECT TO DATABASE
            mysql_connect(. ..........);
            mysql_select_db (...........);

            //IF DELETE BUTTONS IS NOT PRESSED SHOW THE FORM
            if (!isset($_POST['delete']))
            {
            //WRITE HERE ALL THE STUFF FROM LINE 10 TO 87
            }

            //IF DELETE BUTTON HAS BEEN PRESSED.
            else
            {
            //LINE NUMBER 91 TO 96

            //HERE, REDIRECTION IS NOT DONE BY META TAG
            //DO IT LIKE THIS:
            header("Locatio n:delete.php");
            }
            //CLOSE DB
            //END
            'DELETE LINE 75'
            'WHATS $count IN LINE 92?? IT SHOULD BE count($_POST['checkbox'])'
            'Also dont use $cbnbr, instead put the value in the value attribute of checkboxes'[/php]
            Have a look at this tutorial. This may help you.

            Comment

            • beam1985
              New Member
              • Mar 2008
              • 7

              #7
              Originally posted by hsriat
              [PHP]
              'Also dont use $cbnbr, instead put the value in the value attribute of checkboxes'[/php]
              OK I need a little help with the above instruction, im not sure how to make that happen. otherwise ive made all the suggested changes and im recieveng Undefined index: checkbox from this area

              [PHP]
              else
              {

              if($_POST['delete'] == 'Delete'){
              for($i=0;$i<cou nt($_POST['checkbox']);$i++){
              $del_id = $_POST['cbnbr'][$i];
              $sql = "DELETE FROM article WHERE id='$del_id'";
              $result = mysql_query($sq l);

              }
              header("Locatio n:delete.php");
              }
              }

              mysql_close();
              ?>
              [/PHP]

              Thank you so much for the advice i do sincerely appreciate it

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Originally posted by beam1985
                OK I need a little help with the above instruction, im not sure how to make that happen. otherwise ive made all the suggested changes and im recieveng Undefined index: checkbox from this area
                I see that you have already given value=$rows['usernae'] to the checkboxes. Instead of that, use value=$rows['id'] as I can see that id is the unique key in your database.
                And I don't think you would get that error this time if you would change your code according to what I said.
                When first time you open the page, there is nothing as $_POST['checkbox']. $_POST variable is set after you submit the page.
                Read the stuff at the tutorial I gave you, that will help you interpret what $_POST is.

                Comment

                • beam1985
                  New Member
                  • Mar 2008
                  • 7

                  #9
                  Ok we are REALLY close now, i'm not getting any more errors, but the querys arn't being deleted. you can check out the php in action here

                  delete2.php

                  and heres the current code

                  [PHP]
                  <?php

                  $host="localhos t"; // Host name
                  $username="user "; // Mysql username
                  $password="pass "; // Mysql password

                  mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
                  mysql_select_db ("ragra");

                  if (!isset($_POST['delete'])){

                  $sql="SELECT * FROM article";
                  $result=mysql_q uery($sql);
                  $count=mysql_nu m_rows($result) ;

                  ?>
                  <table><tr><td> <img src="comcarblog .jpg"></td></tr></table>
                  <table><tr><td> <a href="main.php" alt="Home"><img src="home.jpg"> </a></td>
                  <td><a href="" alt=""><img src="blank.jpg" ></a></td>
                  <td><a href="add.php" alt="Admin"><im g src="admin.jpg" ></a></td>
                  </tr></table><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
                  <tr><td colspan="4" bgcolor="#41a1f 3"><form name="form1" method="post">
                  <?php echo "You're about to delete some posts!"; ?></td></tr></table>
                  <table width="400" border="0" cellspacing="1" cellpadding="0" >
                  <tr><td align="center" bgcolor="#f3d84 1">Number</td>
                  <td align="center" bgcolor="#f3d84 1">Id</td>
                  <td align="center" bgcolor="#f3d84 1">Title</td>
                  <td align="center" bgcolor="#f3d84 1">Tagline</td>
                  <td align="center" bgcolor="#f3d84 1">Author</td></tr>
                  <?php
                  while($rows=mys ql_fetch_array( $result)){
                  ?>
                  <tr><td align="center" bgcolor="#FFFFF F"><input type="checkbox" name="checkbox[]" value="
                  <?php
                  $rows['id']
                  ?>">
                  </td><td bgcolor="#FFFFF F">
                  <?php
                  echo $rows['id'];
                  ?></td><td bgcolor="#FFFFF F">
                  <?php
                  echo $rows['title'];
                  ?>
                  </td>
                  <td bgcolor="#FFFFF F">
                  <?php
                  echo $rows['tagline'];
                  ?>
                  </td><td bgcolor="#FFFFF F">
                  <?php
                  echo $rows['author'];
                  ?>
                  </td></tr>
                  <input type="hidden" NAME="cbnbr[]" VALUE="cbnbr">
                  <?php

                  }
                  ?>
                  <tr><div><td colspan="5" align="center" bgcolor="#FFFFF F">
                  <input name="delete" type="submit" id="delete" value="Delete"> </td></div></tr></table>

                  <?php

                  }
                  else{

                  if($_POST['delete'] == 'Delete'){
                  for($i=0;$i<cou nt($_POST['checkbox']);$i++){
                  $del_id = $_POST['checkbox'][$i];
                  $sql = "DELETE FROM article WHERE id='$del_id'";
                  $result = mysql_query($sq l);
                  }
                  header("Locatio n:delete2.php") ;
                  }
                  }

                  mysql_close();
                  ?>
                  [/PHP]

                  Comment

                  • beam1985
                    New Member
                    • Mar 2008
                    • 7

                    #10
                    My teacher said that i still "havn't structured the code properly and it make it much harder to read thru and troubleshoot" he added a hidden imput type, resulting it the code looking like this

                    [PHP]
                    <?php

                    $host="localhos t"; // Host name
                    $username="oeua oeu"; // Mysql username
                    $password="aoio ei"; // Mysql password

                    mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
                    mysql_select_db ("ragra");

                    if (!isset($_POST['delete'])){

                    $sql="SELECT * FROM article";
                    $result=mysql_q uery($sql);
                    $count=mysql_nu m_rows($result) ;

                    ?>

                    <table>
                    <tr>
                    <td><img src="comcarblog .jpg"></td>
                    </tr>
                    </table>

                    <table>
                    <tr>
                    <td><a href="main.php" alt="Home"><img src="home.jpg"> </a></td>
                    <td><a href="" alt=""><img src="blank.jpg" ></a></td>
                    <td><a href="add.php" alt="Admin"><im g src="admin.jpg" ></a></td>
                    </tr>
                    </table>

                    <form name="form1" method="post">

                    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
                    <tr>
                    <td colspan="4" bgcolor="#41a1f 3">
                    You're about to delete some posts!
                    </td>
                    </tr>
                    </table>

                    <table width="400" border="0" cellspacing="1" cellpadding="0" >
                    <tr>
                    <td align="center" bgcolor="#f3d84 1">Number</td>
                    <td align="center" bgcolor="#f3d84 1">Id</td>
                    <td align="center" bgcolor="#f3d84 1">Title</td>
                    <td align="center" bgcolor="#f3d84 1">Tagline</td>
                    <td align="center" bgcolor="#f3d84 1">Author</td>
                    </tr>
                    <?php
                    while($rows=mys ql_fetch_array( $result)) {
                    ?>
                    <tr>
                    <td align="center" bgcolor="#FFFFF F">
                    <input type="checkbox" name="checkbox[]" />
                    <input type="hidden" value="<?php echo $rows['id']; ?>" name="itemid[]" />
                    </td>
                    <td bgcolor="#FFFFF F">
                    <?php
                    echo $rows['id'];
                    ?>
                    </td>
                    <td bgcolor="#FFFFF F">
                    <?php
                    echo $rows['title'];
                    ?>
                    </td>
                    <td bgcolor="#FFFFF F">
                    <?php
                    echo $rows['tagline'];
                    ?>
                    </td>
                    <td bgcolor="#FFFFF F">
                    <?php
                    echo $rows['author'];
                    ?>
                    </td>
                    </tr>
                    <?php

                    }
                    ?>
                    <tr>
                    <td colspan="5" align="center" bgcolor="#FFFFF F">
                    <input name="delete" type="submit" id="delete" value="Delete">
                    </td>
                    </tr>
                    </table>

                    </form>

                    <?php

                    } else {

                    if($_POST['delete'] == 'Delete'){
                    for($i=0;$i<cou nt($_POST['itemid']);$i++)
                    {
                    if(isset($_POST['checkbox'][$i]) && $_POST['checkbox'][$i]=='checked')
                    {
                    $del_id = $_POST['itemid'][$i];
                    $sql = "DELETE FROM article WHERE id='$del_id'";
                    $result = mysql_query($sq l);
                    }
                    }
                    header("Locatio n:delete.php");
                    }
                    }

                    mysql_close();
                    ?>[/PHP]

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Tell him there's no need of adding any hidden input.
                      Just change this in your second last post, and tell me if it works.[HTML]<input type="checkbox" name="checkbox[]" value="<?php
                      echo $rows['id'];
                      ?>">[/HTML]
                      Also delete this:[html]<input type="hidden" NAME="cbnbr[]" VALUE="cbnbr">[/html]

                      Comment

                      Working...