Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    delete a row from a table

    I'm trying to delete a row from my table using a checkbox, but the problem is my code doesn't seem to work. Any ideas would be great.

    Code:
    <?php
    $db = pg_connect("host=database.dcs.aber.ac.uk
    
    port=5432 dbname=***** user=***** password=*********");
    ?>
    <?php
    $sql="SELECT * FROM quiz";
    $result=pg_query($sql);
    
    $count=pg_num_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="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in sql</strong> </td>
    </tr>
    <tr>
    
    <td align="center" bgcolor="#FFFFFF"><strong>Quiz No</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Created By</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Delete</strong></td>
    </tr>
    <?php
    while($rows=pg_fetch_array($result)){
    ?>
    <tr>
    
    <td bgcolor="#FFFFFF"><? echo $rows['quizref']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['createdby']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['quizref']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?
     
    if($delete){
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM quiz WHERE quizref='$del_id'";
    $result = pg_query($sql);
    }
    
    
    if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=quizlisttest.php\">";
    }
    }
    pg_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Code:
    $del_id = $checkbox[$i];
    You are relying on register_globals being on. This is a bad idea as it has been off by default for several versions and will be completely removed in PHP 6.

    You will need to retrieve the variables from the $_POST array before using them in your code.

    During development, it's also a good idea to turn on error reporting - let php tell you where the problem is.
    John

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not too sure about the pg_connect and so forth but try to get the logics and change it to suit yourself


    PHP Code:
    <?php

    if (isset($_POST['delete'])) {
        
    /////////////////////////////////////////
    $db pg_connect("host=database.dcs.aber.ac.uk

    port=5432 dbname=***** user=***** password=*********"
    );
    /////////////////////////////////////////

    $chkbox $_POST['chkbox'];
    $count count($chkbox);
    $i 0;
    while (
    $i $count) {
    $sql="DELETE FROM quiz WHERE quizref ='$chkbox[$i]'"
    $result=pg_query($sql);
    $i++;
    }
    }
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in sql</strong> </td>
    </tr>
    <tr>

    <td align="center" bgcolor="#FFFFFF"><strong>Quiz No</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Title</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Created By</strong></td>
    <td align="center" bgcolor="#FFFFFF"><strong>Delete</strong></td>
    </tr>
    <?php
    while($rows=pg_fetch_array($result)){
    ?>
    <tr>

    <td bgcolor="#FFFFFF"><? echo $rows['quizref']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
    <td bgcolor="#FFFFFF"><? echo $rows['createdby']; ?></td>
    <td align="center" bgcolor="#FFFFFF"><input name="chkbox[]" type="checkbox" id="chkbox[]" value="<? echo $rows['quizref']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?php
    if($result){
    echo 
    "<meta http-equiv=\"refresh\" content=\"0;URL=quizlisttest.php\">";
    }
    }
    pg_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
    Last edited by c0d3pRaDa; 01-28-2010 at 10:03 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •