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 8 of 8

Thread: Deleting form

  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts

    Deleting form

    What im trying to do is delete data from my SQL database from a SQL query list. Then select from that list what you would like to delete, i am able to generate the list but i cant manage to delete what i select.

    I have 2 files, the list and the delete file.

    List file ->
    PHP Code:
    <?php

    /**
     * @author Killermud
     * @copyright 2009
     */

    require './connect.php';

    if(!
    $con){
        die(
    'Could not connect: ' mysql_error());
    }

    $tbl_name="forum_question";
    mysql_select_db("$dbname")or die("cannot select DB");
    $sql="SELECT * FROM $tbl_name";
    $result mysql_query($sql);
    ?>
    <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td width="10%" bgcolor="#F8F7F1"><strong>Select</strong></td>
    <td width="15%" bgcolor="#F8F7F1"><strong>Topic</strong></td>



    <?php
    while($rows mysql_fetch_array($result)){ ?>
    <tr>
    <td width="10%" bgcolor="#F8F7F1"><form action="delete.php">
    <input type=checkbox name="<?php $rows['id']; ?>"> 
    </td>
    <td width="15%" bgcolor="#F8F7F1"><?php echo $rows['topic']; ?></td>
    </tr>

     <?php
        
    }
        
    ?>
        </tr>
    </tr>
    </table>
        </table>
        <input type="submit" name="submit" value="Delete">
    </form>
    Then this is where it goes wrong when i hit submit. Here is my delete file.

    PHP Code:
    <?php

    /**
     * @author Killermud
     * @copyright 2009
     */

    require './connect.php';

    if(!
    $con){
        die(
    'Could not connect: ' mysql_error());
    }
    mysql_select_db("$dbname")or die("cannot select DB");
    $sql="SELECT id FROM users where id={$rows['id']}";
       
    $result mysql_query($sql) or die('Delete album failed. ' mysql_error());
                 
       if (
    mysql_num_rows($result) == 1) {
          
    $row mysql_fetch_assoc($result);
    $sql "DELETE FROM $tbl_name WHERE id ={$rows['id']}";

          
    mysql_query($sql) or die('Delete album failed. ' mysql_error()); 
    }
    if(
    mysql_num_rows($results)){?>
    <html>
    <head>
    <title>Go Back to previous Page</title>

    <script language="javascript">
    <!--

    function function1() {

      history.back(1);

    }
    //-->
    </script>


    </head>

    <body>

    <a href="javascript:function1()">Back</a><br>

    </body>

    </html>
    <?php
    }else
    {
        
    $msg mysql_error();
    echo 
    $msg;
    }
    ?>
    The error i get is :

    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\domains\opjeplaats.nl\wwwroot\darkness\webtemp\delete.php
    Help please.

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,389
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    Your delete.php file does not know what this is .. (in red)

    $sql = "DELETE FROM $tbl_name WHERE id ={$rows['id']}";

    You define it in "list.php", but not in "delete.php".

    In the list.php form, use a hidden variable with the table name:
    <input type="hidden" name="table_name" value="$tbl_name">

    Then in "delete.php" ...

    $tbl_name=$_POST['table_name'];

    Now it knows the name of the table.

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    Its got rid of that error to bring a new one,

    Delete album failed. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    I made a few changes to my delete.php as well to :

    PHP Code:
    <?php

    /**
     * @author Killermud
     * @copyright 2009
     */

    require './connect.php';

    if(!
    $con){
        die(
    'Could not connect: ' mysql_error());
    }
    $tbl_name=$_POST['table_name'];
    mysql_select_db("$dbname")or die("cannot select DB");
    $sql="SELECT id FROM users where id={$rows['id']}";
       
    $result mysql_query($sql) or die('Delete album failed. ' mysql_error());
                 
       if (
    mysql_num_rows($result) == 1) {
          
    $row mysql_fetch_assoc($result);
    $sql "DELETE FROM $tbl_name WHERE id ={$rows['id']}";
    }
    if(@
    mysql_query($sql)){
        echo 
    "Deleted";
    }
    else
    {
        
    $msg mysql_error();
    echo 
    $msg;
    }
    ?>

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Not sure if it matters too much, but try this.

    PHP Code:
    $sql mysql_query("SELECT id FROM users WHERE id='".$rows['id']."'"); 

  • #5
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    Hmm keep getting new errors ok now the error is :

    Delete album failed. Query was empty

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,960
    Thanks
    2
    Thanked 304 Times in 296 Posts
    Your delete.php code is using $rows['id'] in the query(ies). Your form is not setting that variable when it submits. You should be developing and debugging code on a system where error_reporting is set to E_ALL and display_errors is set to ON so that undefined variables, like $rows['id'] will be reported.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    Is there any other way of doing it?

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,960
    Thanks
    2
    Thanked 304 Times in 296 Posts
    I don't know, it's your code. What method did you define and design it to use to get the id from the form to the form processing code?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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