简体   繁体   English

PHP和MySQL-删除表格行问题

[英]PHP & MySQL - Deleting table rows problem

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. 好的,我的脚本应该删除存储在2个MySQL表中的特定用户案例,但是由于某种原因,当用户删除特定案例时,它将删除所有用户案例,而我只希望它删除用户选择的案例。 I was wondering how can I fix this problem? 我想知道如何解决这个问题? Thanks in advance for helping. 在此先感谢您的帮助。

Here is the PHP & MySQL code. 这是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 = '$di'");

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

}

}

Here is the XHTML code. 这是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="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. 这是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)
);

From first glance it's probably because all that HTML is contained in the same form. 乍一看,可能是因为所有HTML都包含在同一表格中。 So, no matter which Delete Case button you click, every one of the delete_id[] hidden fields is going to be submitted to the server. 因此,无论您单击哪个Delete Case按钮,每个delete_id []隐藏字段都将提交给服务器。

So you'd need to separate each row into it's own form like... 因此,您需要将每一行分成自己的形式,例如...

<li>
<form method="POST" action="someURL.php">
<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'] . '" />
</form>
</li>

or something else like changing the Submit buttons to just plain Button and adding some javascript... 或其他类似将“ 提交”按钮更改为普通按钮并添加一些javascript ...

This may be because the id of all the cases is being passed even when the user selects one case.... 这可能是因为即使用户选择了一个案例,也会传递所有案例的ID。

The following may work to get only the values selected by the user... 以下操作可能会仅获取用户选择的值...

  1. Associate a check box with each li with an unique id 将复选框与每个具有唯一ID的li关联
  2. Let the user select the check box of the cases he wants to delete 让用户选中要删除的案例的复选框
  3. When the user presses 'Delete' or something, use javascript to collect id of the cases for which the check box is checked... 当用户按下“删除”之类的内容时,请使用javascript来收集选中该复选框的案例的ID。
  4. Then submit these ids to the next page.... 然后将这些ID提交到下一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM