hi

I'm a relatively new php developer without much experience in comercial development so please excuse me if this questions seems novice. but i am really struggling to get my head around this problem. please help.

my problem is:
i need to display a list of records from mysql table each with a checkbox along the side of each record. i want to allow the user to select 1 or more records and delete these from the mysql db.

my script stores the recordid associated with each checkbox into $Post [delete];
i then use a while loop to extract each element of the array from post varible. from here i can echo each one out to the screen etc. inside this while loop i have inserted the delete statement which work fine. my problem is this is a really inefienct way of doing things as its executes the mysql statement many times instead of just once. therefore the mysql delete statement needs to to be outside of the while loop. my problem is then that only one of the array records is present.

how do i get the values from the a array in one loop and use them to create a single mysql delete statement.

Please excuse my explaination here is the code which might help explain.

$box= $_POST['delete'];

while (list ($key,$val) = @each ($box)) {
//echo "$val,";
//echo "$key,";

$del_id = $val ;
$sql = "DELETE FROM brochure WHERE requestid='$del_id'";
$result = mysql_query($sql);
echo $del_id;

}

i would like to use mysql query IN something like :
if ($POST["deleted_items"]) { $deleted_items = join(', ', $POST["deleted_items"]);

$query = "DELETE FROM $table_name WHERE $table_pk IN ($deleted_items)"; $result = mysql_query($query);

this would only execute mysql statment once and would delete where ids where the IN condition applied.

    There are two things you can do:
    1st use a prepared statement and simply call it many times - you only pass the parameters and the query is "waiting" already on the server.

    2nd concatenate a string and pass it into your query like you suggested yourself:

    for example simply by imploding the array:

    $delete_items = implode(",",$box);

      pull your data from the while into an array, crudely.
      {
      $array[] =datarowfromtable;
      }
      once u close your loop
      $newarray = array_unique($array);
      and away you go!

        Write a Reply...