PHP & MySQL - Deleting table rows problem.

Posted by oReiLLy on Stack Overflow See other posts from Stack Overflow or by oReiLLy
Published on 2010-04-29T06:27:32Z Indexed on 2010/04/29 6:37 UTC
Read the original article Hit count: 425

Filed under:
|
|

Okay my script is supposed to delete a specific users case which is stored in 2 MySQL tables but for some reason when the user deletes the specific case it deletes all the users cases I only want it to delete the case the user selects. I was wondering how can I fix this problem? Thanks in advance for helping.

Here is the PHP & MySQL code.

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 = '$di'");

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

}

}

Here is the XHTML code.

<li>
<input type="text" name="file[]" size="25" />
<input type="text" name="case[]" size="25" />
<input type="text" name="name[]" size="25" />
<input type="submit" name="delete_case" id="delete_case" value="Delete Case" />
<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="submit" name="delete_case" id="delete_case" value="Delete Case" />
<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="submit" name="delete_case" id="delete_case" value="Delete Case" />
<input type="hidden" name="delete_id[]" value="' . $row['cases_id'] . '" /> 
</li>

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)
);

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql