#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    PHP and mysql Update


    Hello to all. I have made an edit page. In this page the user can change some details. When the user press submit this is my code:
    PHP Code:
    if (isset($_POST['Submit'])) 
    {
    if (
    is_numeric($_POST['id']))
    {
    $id $_POST['id'];
    $product_id$_POST['product_id'];
    $i $product_id;
    do {
    $str[] =  "(
                    '
    $i',
                    '
    {$_POST['description_'][$i]}',
                    '
    {$_POST['description2_'][$i]}',
                    '
    {$_POST['amount_'][$i]}',
                    '
    {$_POST['Unit_'][$i]}',
                    '
    {$_POST['Unit_Price_'][$i]}',
                    '
    {$_POST['Sum_'][$i]}',
                    '
    {$_POST['Code1_'][$i]}',
                    '
    {$_POST['Code2_'][$i]}')";
        
    $i++;
    }
    while (isset(
    $_POST['Unit_'][$i]));
    $s implode(',',$str);
    $pedit "INSERT INTO products(product,description,description2,amount,Unit,Unit_Price,Sum,Code1,Code2) VALUES {$s} ON DUPLICATE KEY UPDATE id='$id' ";
    }
    if (!
    mysql_query($pedit,$con))
      {
      die(
    'Error: ' mysql_error());
      }

    With this code I want to update my database when the user press submit in the edit page.But instead of update my database, adds 2 more products. What am I doing wrong?
    Thank you all for your answers.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,233
    Rep Power
    593
    You are using the wrong query. INSERT adds rows. You want to update rows so you need to use UPDATE. In addition, you are using the deprecated MySQL extensions. You should be using PDO.

    Comments on this post

    • Jacques1 agrees : That "deprecated" joke is getting old. ;-)
    • ManiacDan agrees : Seriously, you're spelling deprecated wrong.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    Hello again.Thank you for the suggestion with the PDO. I'm currently reading about the PDO. But this script is for my university and they wanted this way. I try with the update but it didn't work. In my database I have a table discription and a tble products. The tables related with the id. In the table products I have the colum id ,the colum product and some other colums. The colum products counts the products that related with the id. So I have in ID= 1 , 2 products. Product 0 and 1. When the user press edit in ID=1 , sees the 2 products that related with the ID=1. So when the user change this 2 products I want to update this products in my database. So I made an arry from the POST with the changes in 2 products and I want to update this products. With the UPDATE doesn't do that, because of this I try to use the INSERT.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,233
    Rep Power
    593
    Your university wants you to use obsolete programming??????? I wish you would tell me who that is so I can be sure not to recommend it to anyone.

    I cannot really tell from your description what you are saying. The concept is very simple. If you want to change an existing row you need to use UPDATE and if you want to add a new row you use INSERT. So what is not working?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,696
    Rep Power
    6351
    Go back to using UPDATE, that's the way you update rows. Once you go back, show what code you have.

    Also, this bothers me:
    In the table products I have the colum id ,the colum product and some other colums. The colum products counts the products that related with the id. So I have in ID= 1 , 2 products. Product 0 and 1.
    Are you somehow storing multiple things in the same row?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    I want to use the Update but I do something wrong.
    My table product_details is:
    id----details
    1----something

    and my table products is:
    id---product_number---amount---e.t.c
    1-----------0------------10-----e.t.c
    1-----------1------------20-----e.t.c

    So I want to update the table products where id=1. When the user in the edit pade press submit, I create an array with the POST of product cells. With this arry I try to update the table products. I hope to be more understandable.
    Sorry for my bad English. I'm from Greece.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    And one last thing. I use Javacript to clone the products. So in the page add product, when the user press add pruduct with javacript clone the first product. So the first product have product_number 0 and the second poduct have product_number 1..e.t.c
    Thank you again for your answers
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,233
    Rep Power
    593
    Originally Posted by qwert678
    So I want to update the table products where id=1. When the user in the edit pade press submit, I create an array with the POST of product cells. With this arry I try to update the table products.
    You use UPDATE for that. It will be difficult to help you any further unless you post your code and tell us what the error is or what is happening/not happening.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,696
    Rep Power
    6351
    Why do you have an ID and a product_number. Isn't product_number unique? there's no reason to have TWO IDs on the same table, that's why it's confusing.

    You still need to rewrite this (or revert it) back to when you were using UPDATE. Right now it's just wrong, you're using insert instead of update. That's your bug, that's where we can stop helping.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0
    I Updated my code..The code Is:

    PHP Code:
    if (isset($_POST['Edit_Pol'])) 
    {
    if (
    is_numeric($_POST['id']))
    {
    $id $_POST['id'];
    $Product$_POST['product'];
    $description$_POST['Description1_'];
    $s implode(',',$description);
    mysql_query("UPDATE products SET description1='$s' WHERE id='$id'")
    or die(
    mysql_error());

    }

    My table was:
    id---product_number---description---e.t.c
    1-----------0------------test1-----e.t.c
    1-----------1------------test2-----e.t.c

    After the update (I change the test1 with red and the test2 with blue) my table is:
    id---product_number---description---e.t.c
    1-----------0------------red,blue-----e.t.c
    1-----------1------------red,blue-----e.t.c

    So the error is in the colum descritopn. It puts the description of product 0 and 1 in the product 0.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,233
    Rep Power
    593
    Which is exactly what you told it to do. I am guessing when you explode Description you are getting it for both product 1 and 2. If that is the case you want this.
    PHP Code:
    mysql_query("UPDATE products SET description1='$s[0]' WHERE id='$id'")
    or die(
    mysql_error()); 
    Instead of putting your query as a literal in the call, you should build it as a string and pass it. That way you can echo the resulting query string and see exactly what you are doing wrong.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,696
    Rep Power
    6351
    This is something you really could be debugging on your own. Your problem description is "this 8 line snippet of code updates the wrong row." Find out why. Print out the query, I bet the ID is different than you think. If so, where does it come from? Why would that source be wrong?

    Debugging is a very simple process:

    1) Find the problem (your update statement)

    2) Print out the variables associated with the problem (the statement itself, and the ID)

    3) If either of them are wrong, they become the new problem. Goto 1.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  24. #13
  25. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Your university wants you to use obsolete programming???????
    Unfortunately not that uncommon. When I was in school, part of a databases class I took covered integrating MySQL with Java, C and PHP. The professor was a databases person; she knew set theory and SQL extraordinarily well, but she wasn't a programmer. The extent of her knowledge about PHP probably came from Googling PHP + MySQL tutorial. (PHP wasn't a language the university covered normally anyway, it just happens to be a language that is incredibly easy to integrate with MySQL)

    On the flip side, the professor in my OpenGL class knew OpenGL really well and actually had time set aside to cover upcoming changes to the API.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo