Discuss PHP and mysql Update in the PHP Development forum on Dev Shed. PHP and mysql Update PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 9
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.
Posts: 2,907
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.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
Posts: 9
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.
Posts: 2,907
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?
Posts: 9,917
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?
"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
Posts: 9
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.
Posts: 9
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
Posts: 2,907
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.
Posts: 9,917
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.
Posts: 9
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.
Posts: 2,907
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.
Posts: 9,917
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.
Posts: 8,056
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.