Reputation: 117
I have a 2 tables, users
and short_style
.
Fields of table users
:
id int primary not null auto increment
username
password
firstname
lastname
data inserted by users to table users
:
users
id username password firstname lastname
1 jsmith md5hash john smith
2 jbrown md5hash jane brown
data inserted by users to table 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
I can have the users add a new style, that code works perfect.
I can have the users update their data as well, that code works perfect.
I'm stuck at deleting user data as I have no idea how to target the number_of_styles
data, as that is the only unique data.
From what I have learned (in this short time) the DELETE
only take 2 parameters, the Table name and the Table row (I'm assuming).
How can I make this work?
Sorry for the long html, I still haven't figured out how to show html in the comments. What I have:
<?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>
Upvotes: 1
Views: 2005
Reputation: 23409
You should ALWAYS use an auto-increment field for every table you create so that you always have a unique ID to use when deleting rows.
If that's not an option for you, you'll have to modify your delete query to make sure you're deleting the correct row:
$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";
Edit
I didn't not realize your number_of_styles
was auto increment. In that case you can simply:
$dquery = "DELETE FROM $category WHERE number_of_styles = $numberofstyles LIMIT 1";
Since it's unique there would be no need to mention all the other fields.
Upvotes: 1