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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Location
    India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem in deleting multiple rows

    Hello everybody,
    I am having problem in deleting multiple rows using checkbox. It is showing the following error :

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in \delete.php


    Code:

    print"<form name=\"form1\" method=\"post\" action=\"delete.php\">";
    print"<tr>";
    print"<td>Designation</td>";
    print"<td>First Name</td>";
    print"<td>Last Name</td>";
    print"<td>&nbsp;</td>";
    print"</tr>";
    while ($a_row = mysql_fetch_array ($result))
    {
    print"<tr>\n";

    print "<td><center>$a_row[designation]</center></td>
    <td><center>$a_row[fname]</center></td>
    <td><center>$a_row[lname]</center></td>

    print "<td><input type=\"checkbox\" name=\"delete[]\" value=$a_row[id]></td>";
    print "</tr>";
    }
    print "</table>";
    print"<center>";
    print"<p>";
    print"<input type=\"submit\" name=\"delete\" value=\"Delete\">";
    print"<input type=\"reset\" name=\"submit2\" value=\"Cancel\">";
    print"</p>";
    print"</center>";
    print"</form></td>";

    DELETE.PHP

    <?php
    //print "$a_row[id]";
    if ( ! delete){
    echo "<b>Please choose fields to be deleted.</b><br>";
    }
    else{
    $sql = "delete * from work_committee where id in ($_POST['delete'])";
    $result = mysql_query($sql);
    if (! $result )
    echo"ERROR !!! " . mysql_error();
    else
    echo "DELETED!!!";
    } //end if
    ?>


    Where id is int type field.

    Any help would appreciated. Thanks in advance.

  • #2
    New Coder
    Join Date
    Apr 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i would try this, it worked for me

    if (is_array($_POST['delete']))
    {
    $deleted = 0;

    while(list($key, $id) =each($_POST['delete']))
    {
    if (mysql_query("DELETE FROM `table_name` WHERE `id`='$id'"))
    {
    $deleted .= mysql_affected_rows();
    }
    else
    {
    die("SQL error:<br />" . mysql_error());
    }
    }
    echo $deleted . " rows deleted<br><br>";


    }else{
    echo "No entry selected";
    }

    I would also change the name of your delete button to something other than "delete" as that might be causing it problems.
    print"<input type=\"submit\" name=\"delete\" value=\"Delete\">";

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you forgot to close the else

    PHP Code:
     else{
    $sql "delete * from work_committee where id in ($_POST['delete'])";
    $result mysql_query($sql);
    if (! 
    $result )
    echo
    "ERROR !!! " mysql_error();
    else
    echo 
    "DELETED!!!";
    //end if
    }//end else
    ?> 

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I would advise getting a syntax highlighting IDE (perhaps phpedit.com for a freebie) so you can easily tell where code errors like that lay.

    Your second print block has no terminating " btw, which explains the T_STRING return.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    note the braces around {$_POST['delete']} , they tell PHP that the enclosed value is an array and will stop your parse error.

    PHP Code:
    <?
    $sql 
    "delete * from work_committee where id in ({$_POST['delete']})";
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    New to the CF scene
    Join Date
    May 2004
    Location
    India
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks everybody .

    Gismo Your code is running as it is except - when I delete two rows it display - 1 Row Deleted.


  •  

    Posting Permissions

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