PHP& MySQL删除多行脚本问题

时间:2010-04-29 03:58:58

标签: php mysql xhtml

当用户点击删除按钮时,我试图一次从两个不同的表中删除两个表行,但由于某种原因,我无法将表行删除,有些人可以帮我弄清楚我的错误是什么脚本?感谢

这是MySQL表。

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

这是PHP& MySQL脚本。

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

}

}

这是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 个答案:

答案 0 :(得分:1)

也许我在这里弄错了,你将每个其他表单元素设置为一个数组EXCEPT delete_id。 delete_id应该是这样的形式的数组吗?

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

答案 1 :(得分:0)

请检查此部分,

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

}