tags:

views:

51

answers:

2

I'm trying to delete two tables rows from two different tables at once when a user clicks the delete button, but for some reason I cant get the table rows to delete can some one help me figure out what is wrong with my script? Thanks

Here is the MySQL tables.

CREATE TABLE cases (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
file VARCHAR(255) NOT NULL,
case VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE users_cases (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
cases_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
PRIMARY KEY (id)
);

Here is the PHP & MySQL script.

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

$cases_ids = array();

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT cases.*, users_cases.* FROM cases INNER JOIN users_cases ON users_cases.cases_id = cases.id WHERE users_cases.user_id='$user_id'");

if (!$dbc) {
    print mysqli_error($mysqli);
}  else {
    while($row = mysqli_fetch_array($dbc)){ 
        $cases_ids[] = $row["cases_id"];
    }
}

foreach($_POST['delete_id'] as $di) {
    if(in_array($di, $cases_ids)) {
        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        $dbc = mysqli_query($mysqli,"DELETE FROM users_cases WHERE cases_id = '$delete_id'");

        $dbc2 = mysqli_query($mysqli,"DELETE FROM cases WHERE id = '$delete_id'");
    }

}

}

Here is the XHTML.

<li>
<input type="text" name="file[]" size="25" />
<input type="text" name="case[]" size="25" />
<input type="text" name="name[]" size="25" />
<input type="hidden" name="delete_id" value="' . $row['cases_id'] . '" />
</li>

<li>
<input type="text" name="file[]" size="25" />
<input type="text" name="case[]" size="25" />
<input type="text" name="name[]" size="25" />
<input type="hidden" name="delete_id" value="' . $row['cases_id'] . '" />
</li>

<li>
<input type="text" name="file[]" size="25" />
<input type="text" name="case[]" size="25" />
<input type="text" name="name[]" size="25" />
<input type="hidden" name="delete_id" value="' . $row['cases_id'] . '" />
</li>
A: 

Please check this part,

foreach($_POST['delete_id'] as $di) {
    if(in_array($di, $cases_ids)) {
        $mysqli = mysqli_connect("localhost", "root", "", "sitename");
        $dbc = mysqli_query($mysqli,"DELETE FROM users_cases WHERE cases_id = '$di'");

        $dbc2 = mysqli_query($mysqli,"DELETE FROM cases WHERE id = '$di'");
    }

}
Karthik
the file did not delete :( thanks for the help by the way
oReiLLy
+1  A: 

Maybe I'm mistaken here, you set every other form element to be put into an array EXCEPT delete_id. Is delete_id supposed to be an array in the form like so?

<li>
    <input type="text" name="file[]" size="25" />
    <input type="text" name="case[]" size="25" />
    <input type="text" name="name[]" size="25" />
    <input type="hidden" name="delete_id[]" value="' . $row['cases_id'] . '" />
</li>

<li>
    <input type="text" name="file[]" size="25" />
    <input type="text" name="case[]" size="25" />
    <input type="text" name="name[]" size="25" />
    <input type="hidden" name="delete_id[]" value="' . $row['cases_id'] . '" />
</li>

<li>
    <input type="text" name="file[]" size="25" />
    <input type="text" name="case[]" size="25" />
    <input type="text" name="name[]" size="25" />
    <input type="hidden" name="delete_id[]" value="' . $row['cases_id'] . '" />
</li>
AlReece45