1

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>
2
  • 2
    You really shouldn't be putting POST fields directly into your SQL queries. It can be a security issue due to SQL injection attacks. Commented Apr 29, 2010 at 4:02
  • @Dav, thanks for the advice, I will fix my code as soon as i learn whats wrong with my code. Commented Apr 29, 2010 at 4:04

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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'");
    }

}

Comments

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.