0

i'm having some trouble passing Form checkbox array as mysql_query in order to delete multiple rows from table.

The structure is as follows:

HTML

<form action="usunogrod.php" method="POST" enctype="multipart/form-data">
    <?php
     $ogrodysql = "SELECT id_ogrodu, nazwa FROM ogrody";
     $result = mysqli_query($con, $ogrodysql);

     if (mysqli_num_rows($result) > 0) {

         while($row = mysqli_fetch_assoc($result)) {
              echo "• " . $row["id_ogrodu"]. " " . $row["nazwa"]. "<input type='checkbox' name='removegarden[]' value=" .$row["id_ogrodu"]." <br><br>";
         }
     } 
     else {
         echo "0 results";
     }
     ?>

    <br><br>
    <input type="submit" value="Usuń zaznaczony ogród."/>
</form>

PHP for processing form in usunogrod.php

<?php

$db_host = 'xxxxx';
$db_user = 'xxxxx';
$db_pwd = 'xxxxx';
$con = mysqli_connect($db_host, $db_user, $db_pwd);  
$database = 'xxxxx';

if (!mysqli_connect($db_host, $db_user, $db_pwd))
    die("Brak połączenia z bazą danych.");

if (!mysqli_select_db($con, $database))
    die("Nie można wybrać bazy danych.");

function sql_safe($s)
{
    if (get_magic_quotes_gpc())
        $s = stripslashes($s);
    global $con;
    return mysqli_real_escape_string($con, $s);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {   

    $ogrod_id = trim(sql_safe($_POST['removegarden[]']));

    if (isset($_POST['removegarden[]'])) {

                mysqli_query($con, "DELETE FROM ogrody WHERE id_ogrodu='$ogrod_id'");

                $msg = 'Ogród został usunięty.';
    }
    elseif (isset($_GET['removegarden[]']))
        $msg = 'Nie udało się usunąć ogrodu.';
};
?>

MySQL table

ogrody

#      id_ogrodu      nazwa
       1              garden1

How may i process an array from checkboxes form so that i will be able to pass a query to delete all checked elements?

EDIT:

I have been able to make it work to a moment where it only deleted one of the checked positions, or the other time just got an error saying i can't pass and array to mysqli_query.

2 Answers 2

3

I think this should help you:

Change this line:

echo "• " . $row["id_ogrodu"]. " " . $row["nazwa"]. "<input type='checkbox' name='removegarden[]' value=" .$row["id_ogrodu"]." <br><br>";

For this one:

echo '• ' . $row["id_ogrodu"]. ' ' . $row["nazwa"]. '<input type="checkbox" name="removegarden['.$row["id_ogrodu"].']" value="'.$row["id_ogrodu"].'" /> <br/><br/>';

Then this one:

if (isset($_POST['removegarden[]'])) {

To

if (isset($_POST['removegarden'])) {

And finally your query:

$gardens = implode(',',$_POST['removegarden']);
mysqli_query($con, "DELETE FROM ogrody WHERE id_ogrodu IN($gardens)");
Sign up to request clarification or add additional context in comments.

3 Comments

Works perfect, same method as B. Desai proposed - you were just one minute later! :) Thanks for explaining in detail, apart from that i cannot change the value in html/php markup, since it needs to be used for further processing, your answer was of great value, thanks!!!
I forgot to change that sorry. It should be the id of course that you are concatenating before your query.. edited the second snippet paste.
i changed top answer to yours since it provides more in-detail way of thinking on this problem, therefore might be a little bit more useful for any1 to come over this. have a nice day, thanks again!
3

You can get your data in $_POST['removegarden']. no need [] at last.

Then convert this array to ',' seperated string which can be then used in query

if (isset($_POST['removegarden'])) {
$ids_to_delete = implode(",",$_POST['removegarden']);
                mysqli_query($con, "DELETE FROM ogrody WHERE id_ogrodu IN ($ids_to_delete)");

                $msg = 'Ogród został usunięty.';
    }

1 Comment

works perfect, even though i had to remove sql_safe function it works like a charm, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.