1

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>
i alarmed alien
  • 9,412
  • 3
  • 27
  • 40
tru
  • 117
  • 1
  • 2
  • 9
  • 2
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 10 '14 at 15:26
  • 2
    delete can use a `where` clause, which will function exactly the same as it would in a `select`. Plus, you have an sql error in the delete. `WHERE $id=$results`? `id` is a field in your table, not a PHP variable. And if it IS supposed to be a php variable, then it's an undefined variable and you're effectively doing `WHERE =$results` – Marc B Oct 10 '14 at 15:27
  • @JayBlanchard I know but I am just learning and I want to learn this as well... hopefully I ill be done with this site in the next cuople of weeks. I then plan on updating with PDO MYSQLI – tru Oct 10 '14 at 15:29
  • @MarcB sorry about the sql error that was me playing around with the code trying to get it to work... I had a $results variable in there at 1 point, but i have corrected the error.... I tried using WHERE but in the WHERE function I still cant target the specific row. As you see in the form the only user data that is submitted is the table name and the style name. so with a WHERE added – tru Oct 10 '14 at 15:36
  • 1
    you'd have to fix your form code so it DOES submit enough data to uniquely identify a row. if your form is basically saying the equivalent of "delete something, I don't care what", then it should be up to your deletion code to try and GUESS what the user was thinking. – Marc B Oct 10 '14 at 15:38
  • @MarcB I though so as well to add more data to the form but the only pheaseable data to add would be the number_of_styles data.. And again thats where I am having the trouble. Even with a SELECT statement I still do not know how to target the number_of_styles data. I cant do it by id because any user will have more than 1 style added and hence the same id in multiple rows.. Another user can have the same style name and hence I can not select by the style name, same with cost and same with time... Which only leaves number_of_styles... – tru Oct 10 '14 at 15:45
  • @MarcB but for the same reason I am having trouble with DELETE would be the same reason I am having trouble with SELECT... Another question would be can i subquery?. Can DELETE FROM or WHERE have a subquery? – tru Oct 10 '14 at 15:45
  • so add a primary key int/auto_increment field to your replies table. boom, instant unique identifier for every row in that table, and you simply pass THAT id around in stead of a bunch of different field values. – Marc B Oct 10 '14 at 15:46
  • theoretically, you can have subqueries in delete queries, but mysql in particular prevents you from modifying a table (insert/delete) while you're selecting from it. – Marc B Oct 10 '14 at 15:46
  • @MarcB the number_of_styles is the primary key – tru Oct 10 '14 at 15:48
  • so put that number into your form. `` – Marc B Oct 10 '14 at 15:48
  • @marc yes I would put number_of_styles in my form but to taget the correct number_of_styles I would have to input the correct incremented number... so if I wanted to target the style_name : wash....with the id 1...with the number_of_style 6... 6 being the unique number thats only generated when the user add a new style... Its not like in the users table where I can target by the WHERE the id=$_SESSION['user_id'].. – tru Oct 10 '14 at 16:00
  • @marc my problem is trying to extract that data from that specific number_of_styles row.. With WHERE the id=$_SESSION['user_id'], there are multiple rows with that same id.. So I cant SELECT from number_of_styles FROM short_style WHERE the id=$_SESSION['user_id'].. – tru Oct 10 '14 at 16:03
  • `id` and `style_name` in combination seams to be unique for me ^^ you could use onley these two columns in your delete statement – Dwza Oct 10 '14 at 21:57

1 Answers1

1

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.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • Thank you so very much @Adelphia, the number_of_styles is the auto increment.... I did not know I could use AND... thats much better than a subquery.. I tired your way in PhpMyAdmin and it works no problem.. tried it in the php page and it works wonderfully I targeted the id and the style_name as the 2 combined are unique. You understood my problem very well. I wish I coud upvite but i do not have enough reputation points... So if anyone could upvote his solution that would be a welcomed assist. Thank you everyone for your e=responses. Cheers – tru Oct 10 '14 at 16:58
  • @tru if my answer helped you please click on the checkmark to select it. I get points if you do ;) Glad I could help. – I wrestled a bear once. Oct 10 '14 at 17:44
  • 1
    I checkmarked your 1st answer. Thank you again.. The second answer would not work. Even though number_of_styles is Primary and AUTO INCREMENTED there is no way for me to access the value without a 2nd condition... In your updated answer there are 7 values, the query would not know which $number_of_styles value to select... a 2nd condition is very necessary. Your first response was golden. Thank you again... – tru Oct 10 '14 at 20:22
  • Also I see you do a very good job on here, so can you answer a question. The above commenter said I can be banned for asking questions as this is not a teaching website. For me to start a post, the button says Ask A Question, and the majority of the post I see on here are of people asking questions for problems they are having. Im not understanding – tru Oct 10 '14 at 20:24
  • @tru It depends on the question :) If your question is about the Rules of StackOverflow and not about programming you could be penalized, but probably not banned (LOL). If you have questions about the rules you should ask them here: http://meta.stackoverflow.com/ – I wrestled a bear once. Oct 10 '14 at 20:31
  • 1
    `didn't not realize ` means you realized ? :D – Dwza Oct 10 '14 at 21:53