PHP & MySQL deleting multiple rows script problem.
Posted
by oReiLLy
on Stack Overflow
See other posts from Stack Overflow
or by oReiLLy
Published on 2010-04-29T03:58:58Z
Indexed on
2010/04/29
4:07 UTC
Read the original article
Hit count: 330
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>
© Stack Overflow or respective owner