当多行具有相同的id时,如何删除一行


How do I delete a row when multiple rows have the same id

我有两个表,usersshort_style

users的字段:

id int  primary  not null auto increment
username
password
firstname
lastname

用户插入表users:的数据

users
id     username     password     firstname     lastname   
1      jsmith       md5hash      john          smith 
2      jbrown       md5hash      jane          brown 

用户插入表short_style:的数据

short_style
id     style_name     style_cost     style_time     number_of_styles   
1      bald           10             30             1
2      wash           5              15             2   
1      shave          12             30             3   
2      line           8              15             4   
1      wash           Free           15             6   
2      color          20             30             7   

我可以让用户添加一个新的样式,代码工作得很完美。

我也可以让用户更新他们的数据,代码运行得很好。

我一直在删除用户数据,因为我不知道如何针对number_of_styles数据,因为这是唯一唯一的数据。

根据我所了解到的(在这么短的时间内),DELETE只接受两个参数,表名和表行(我假设)。

我该如何完成此操作

很抱歉html太长了,我还没想好如何在评论中显示html。我所拥有的:

<?php
if (isset($_POST['delete_servicename'])&&
isset($_POST['update_category'])) {
$delete_servicename = $_POST['delete_servicename'];
$category = $_POST['update_category'];
$atta = '_name';
$delete = "$category$atta";
$query = "SELECT $delete  FROM $category     WHERE `id`='".$_SESSION['user_id']."'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)==1) {
$dquery = "DELETE FROM $category WHERE $id = '".$_SESSION['user_id']."'";
 }
}
?>
<form action="services_update.php" method="POST">
  <div class="a_u_d_sort">
    <ul>
      <li class="a_u_d_sort_left">
        <p>Category:</p>
      </li>
      <li class="a_u_d_sort_right">
        <select name="update_category">
          <option value="">select</option>
          <option value="short_style">Short Style</option>
          <option value="medium_style">Medium Style</option>
          <option value="long_style">Long Style</option>
          <option value="other_services">Other Service</option>
        </select>
      </li>
     </ul>
   </div>
   <div class="a_u_d_sort">
    <ul>
      <li class="a_u_d_sort_left">
        <p>Service Name:</p>
      </li>
      <li class="a_u_d_sort_right">
        <input type="text" name="delete_servicename"></input>
      </li>
     </ul>
    </div>
    <button class="add" type="submit">Delete</button>
  </form>

您应该始终为创建的每个表使用一个自动递增字段,以便在删除行时始终使用一个唯一的ID。

如果这不是你的选择,你必须修改你的删除查询,以确保你删除了正确的行:

$dquery = "DELETE FROM $category WHERE $id = '".$_SESSION['user_id']."' AND `style_name` = $stylename AND style_cost = $stylecost AND style_time = $styletime AND number_of_styles = $numberofstyles LIMIT 1";

编辑

我没有意识到你的number_of_styles是自动递增的。在这种情况下,您可以简单地:

$dquery = "DELETE FROM $category WHERE number_of_styles = $numberofstyles LIMIT 1";

由于它是独一无二的,所以没有必要提及所有其他领域。