problem with check boxes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepika1
    New Member
    • Apr 2008
    • 1

    problem with check boxes.

    hi ,


    i wrote a code for to delete the particular rows in a table using check boxes.But in that i select the particular rows and click on the delete button these rows didn't delete.plz check it what's the problem in that code.


    <?php
    $host="localhos t"; // Host name
    $username="root "; // Mysql username
    $password=" "; // Mysql password
    $db_name=" db1"; // Database name
    $tbl_name="test _mysql"; // Table name

    // Connect to server and select databse.
    mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
    mysql_select_db ("$db_name") or die("cannot select DB");

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

    $count=mysql_nu m_rows($result) ;

    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0" >
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
    <tr>
    <td bgcolor="#FFFFF F">&nbsp;</td>
    <td colspan="4" bgcolor="#FFFFF F"><strong>Dele te multiple rows in mysql</strong> </td>
    </tr>
    <tr>
    <td align="center" bgcolor="#FFFFF F">#</td>
    <td align="center" bgcolor="#FFFFF F"><strong>I d</strong></td>
    <td align="center" bgcolor="#FFFFF F"><strong>Name </strong></td>
    <td align="center" bgcolor="#FFFFF F"><strong>Last name</strong></td>
    <td align="center" bgcolor="#FFFFF F"><strong>Emai l</strong></td>
    </tr>
    <?php
    while($rows=mys ql_fetch_array( $result)){
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFF F"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
    <td bgcolor="#FFFFF F"><?php echo $rows['id']; ?></td>
    <td bgcolor="#FFFFF F"><?php echo $rows['name']; ?></td>
    <td bgcolor="#FFFFF F"><?php echo $rows['lastname']; ?></td>
    <td bgcolor="#FFFFF F"><?php echo $rows['email']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFF F"><input name="delete" type="submit" id="delete" value="Delete"> </td>
    </tr>
    <?php
    echo "hello";
    // Check if delete button active, start this

    if($delete) {
    echo "hello";

    for($i=0;$i<$co unt;$i++){
    $del_id = $checkbox[$i];
    echo "$del_id";
    echo "hello";
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $result = mysql_query($sq l);
    }

    // if successful redirect to delete_multiple .php
    if($result){
    echo "<meta http-equiv=\"refresh \" content=\"0;URL =checkk.php\">" ;
    }
    }
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Does $result return true or false?

    It is strongly recommended that you do not use register globals - php.net.

    Regards,

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Originally posted by deepika1
      hi ,


      i wrote a code for to delete the particular rows in a table using check boxes.But in that i select the particular rows and click on the delete button these rows didn't delete.plz check it what's the problem in that code.


      <?php
      $host="localhos t"; // Host name
      $username="root "; // Mysql username
      $password=" "; // Mysql password
      $db_name=" db1"; // Database name
      $tbl_name="test _mysql"; // Table name

      // Connect to server and select databse.
      mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
      mysql_select_db ("$db_name") or die("cannot select DB");

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

      $count=mysql_nu m_rows($result) ;

      ?>
      <table width="400" border="0" cellspacing="1" cellpadding="0" >
      <tr>
      <td><form name="form1" method="post" action="">
      <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
      <tr>
      <td bgcolor="#FFFFF F">&nbsp;</td>
      <td colspan="4" bgcolor="#FFFFF F"><strong>Dele te multiple rows in mysql</strong> </td>
      </tr>
      <tr>
      <td align="center" bgcolor="#FFFFF F">#</td>
      <td align="center" bgcolor="#FFFFF F"><strong>I d</strong></td>
      <td align="center" bgcolor="#FFFFF F"><strong>Name </strong></td>
      <td align="center" bgcolor="#FFFFF F"><strong>Last name</strong></td>
      <td align="center" bgcolor="#FFFFF F"><strong>Emai l</strong></td>
      </tr>
      <?php
      while($rows=mys ql_fetch_array( $result)){
      ?>
      <tr>
      <td align="center" bgcolor="#FFFFF F"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['id']; ?></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['name']; ?></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['lastname']; ?></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['email']; ?></td>
      </tr>
      <?php
      }
      ?>
      <tr>
      <td colspan="5" align="center" bgcolor="#FFFFF F"><input name="delete" type="submit" id="delete" value="Delete"> </td>
      </tr>
      <?php
      echo "hello";
      // Check if delete button active, start this

      if($delete) {
      echo "hello";

      for($i=0;$i<$co unt;$i++){
      $del_id = $checkbox[$i];
      echo "$del_id";
      echo "hello";
      $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
      $result = mysql_query($sq l);
      }

      // if successful redirect to delete_multiple .php
      if($result){
      echo "<meta http-equiv=\"refresh \" content=\"0;URL =checkk.php\">" ;
      }
      }
      mysql_close();
      ?>
      </table>
      </form>
      </td>
      </tr>
      </table>
      first, your logic is backwards. why delete AFTER you've displayed the page. and you need to turn of register_global s and access your page variables with POST[] (if that's the method for the form)

      Here's how it should happend. Display list, mark and submit, display the list again.

      This is done in one page refresh.

      I've modified your code, but not checked, you'll get the point::

      [PHP]


      <?php
      $host="localhos t"; // Host name
      $username="root "; // Mysql username
      $password=" "; // Mysql password
      $db_name=" db1"; // Database name
      $tbl_name="test _mysql"; // Table name
      // Connect to server and select databse.
      mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
      mysql_select_db ("$db_name") or die("cannot select DB");
      // if delete is set, delete the values
      if(isset($_POST['delete']))
      {
      foreach ($_POST['checkbox'] as $del_id)
      {
      $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
      $result = mysql_query($sq l) or die("I made a boo boo: " . mysql_error());

      }
      }
      // get all records from DB from display (will not include those deleted above)
      $sql="SELECT * FROM $tbl_name";
      $result=mysql_q uery($sql);
      mysql_close();

      ?>
      <table width="400" border="0" cellspacing="1" cellpadding="0" >
      <tr>
      <td><form name="form1" method="post" action="">
      <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
      <tr>
      <td bgcolor="#FFFFF F">&nbsp;</td>
      <td colspan="4" bgcolor="#FFFFF F"><strong>Dele te multiple rows in mysql</strong> </td>
      </tr>
      <tr>
      <td align="center" bgcolor="#FFFFF F">#</td>
      <td align="center" bgcolor="#FFFFF F"><strong>I d</strong></td>
      <td align="center" bgcolor="#FFFFF F"><strong>Name </strong></td>
      <td align="center" bgcolor="#FFFFF F"><strong>Last name</strong></td>
      <td align="center" bgcolor="#FFFFF F"><strong>Emai l</strong></td>
      </tr>
      <?php
      while($rows=mys ql_fetch_assoc( $result)){ //<-- changed this to fetch_assoc
      ?>
      <tr>
      <td align="center" bgcolor="#FFFFF F"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['id']; ?></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['name']; ?></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['lastname']; ?></td>
      <td bgcolor="#FFFFF F"><?php echo $rows['email']; ?></td>
      </tr>
      <?php
      }
      ?>
      <tr>
      <td colspan="5" align="center" bgcolor="#FFFFF F"><input name="delete" type="submit" id="delete" value="Delete"> </td>
      </tr>
      </table>

      [/PHP]

      Let me know if any questions. Sorry about any mistakes.

      Comment

      Working...