PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 19th, 2012, 07:50 PM
qwert678 qwert678 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 qwert678 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 31 sec
Reputation 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.

Reply With Quote
  #2  
Old December 19th, 2012, 08:16 PM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 16 h 53 m 22 sec
Reputation Power: 581
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.

Reply With Quote
  #3  
Old December 20th, 2012, 09:04 AM
qwert678 qwert678 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 qwert678 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 31 sec
Reputation 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.

Reply With Quote
  #4  
Old December 20th, 2012, 09:12 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 16 h 53 m 22 sec
Reputation Power: 581
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?

Reply With Quote
  #5  
Old December 20th, 2012, 09:43 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Sarcky
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,917 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 1 Day 10 h 25 m 30 sec
Reputation Power: 6113
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:
Quote:
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.

Reply With Quote
  #6  
Old December 20th, 2012, 10:07 AM
qwert678 qwert678 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 qwert678 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 31 sec
Reputation 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.

Reply With Quote
  #7  
Old December 20th, 2012, 10:14 AM
qwert678 qwert678 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 qwert678 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 31 sec
Reputation 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

Reply With Quote
  #8  
Old December 20th, 2012, 10:35 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 16 h 53 m 22 sec
Reputation Power: 581
Quote:
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.

Reply With Quote
  #9  
Old December 20th, 2012, 10:44 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Sarcky
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,917 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 1 Day 10 h 25 m 30 sec
Reputation Power: 6113
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.

Reply With Quote
  #10  
Old December 20th, 2012, 10:51 AM
qwert678 qwert678 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 9 qwert678 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 56 m 31 sec
Reputation 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.

Reply With Quote
  #11  
Old December 20th, 2012, 11:10 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,907 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Month 16 h 53 m 22 sec
Reputation Power: 581
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.

Reply With Quote
  #12  
Old December 20th, 2012, 11:34 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Sarcky
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,917 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 1 Day 10 h 25 m 30 sec
Reputation Power: 6113
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.

Reply With Quote
  #13  
Old December 23rd, 2012, 11:16 PM
E-Oreo's Avatar
E-Oreo E-Oreo is online now
Lost in code
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Dec 2004
Posts: 8,056 E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)E-Oreo User rank is General 92nd Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 1 Day 5 h 42 m 13 sec
Reputation Power: 7104
Quote:
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
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP and mysql Update

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap