Forum Moderators: coopster

Message Too Old, No Replies

update database with textbox array w/ PEAR

updating a mysql database through a form of textboxes

         

svmf20

6:48 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



I am having slight problems with trying to get my page to work properly. Right now i have a form which displays the information of the rows in the database through textboxes. I have two textbox arrays: name[] and price[].

first i connect to database...

if form was submitted it goes through this code:
______________________________________________________

if (!empty($_POST['name'])) {
foreach ($_POST['name'] as $name) {
$nameup = $db_object->query("UPDATE name FROM products SET name = '".$_POST['name']."' WHERE id = '$id'");
}
}

if (!empty($_POST['price'])) {
foreach ($_POST['price'] as $price) {
$priceup = $db_object->query("UPDATE price FROM products SET price = '".$_POST['price']."' WHERE id = '$id'");
}
}
______________________________________________________

else it displays my page which contains the textbox array statements:
______________________________________________________

$total = $db_object->query("SELECT * FROM products ORDER BY id ASC");
$num = $total->numRows();
if($num > 0) {
while($n = $total->fetchRow()) {
$id = $n["id"];
$name = $n["name"];
$price = $n["price"];
echo ("
<tr>
<td align=\"center\"><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td>
<td><input type=\"text\" name=\"name[]\" value=\"$name\" class=\"m\" size=\"30\"></td>
<td><input type=\"text\" name=\"price[]\" value=\"$price\" class=\"m\" size=\"5\"></td>
</tr>");
}
}
______________________________________________________

I feel like i am doing everything right but it just is not updateing any information to the database. Please give me some tips because i am stuck.

timster

7:32 pm on Aug 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shouldn't this:

"UPDATE name FROM products SET name = '".$_POST['name']."' WHERE id = '$id'

be this...

UPDATE products SET name = '".$_POST['name']."' WHERE id = '$id'

You may want to test whether your SQL queries are succeeding. If they are, test whether they are affecting any records.

if (DB::iserror($nameup)) die ('Query failed: ' . $nameup->getMessage() ); # debugging code 

You may also want to put your SQL statement into a variable, and then echo the variable. That often makes a tricky problem obvious.

svmf20

8:05 pm on Aug 10, 2004 (gmt 0)

10+ Year Member



ok i will try that.. thanks

after trying it, it now comes up with a text value of 'Array' if it is different from the database's value and the price becomes 0. I new the cause of this was the $_POST variable in my UPDATE query. I took it out and that solved that problem however still no update. I also put the error in there but however, nothing shows.

I need a way to access the value in the text box that was edited before submission and bring it through the query so i replace the $_POST['name'] with $name instead but that did not do anything.

I don't know...

svmf20

8:04 pm on Aug 13, 2004 (gmt 0)

10+ Year Member



ok i updated my code. i now have made hidden inputs to display the oldinformation before the user edits it.

if form was submitted it goes through this code:

if (!empty($_POST['name'])) {
foreach ($_POST['name'] as $nameup) {
$nameupdt = $db_object->query("UPDATE products SET name = '$nameup' WHERE name = '$nameold'");
}
}
if (!empty($_POST['price'])) {
foreach ($_POST['price'] as $priceup) {
$priceupdt = $db_object->query("UPDATE products SET price = '$priceup' WHERE price = '$priceold'");
}
}

else it displays my page which contains the textbox array statements (now with my hidden values):

$total = $db_object->query("SELECT * FROM products ORDER BY id ASC");
$num = $total->numRows();
if($num > 0) {
while($n = $total->fetchRow()) {
$id = $n["id"];
$nameup = $n["name"];
$nameold = $n["name"];
$priceup = $n["price"];
$priceold = $n["price"];
echo ("
<tr>
<td align=\"center\"><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td>
<input type=\"hidden\" name=\"nameold[]\" value=\"$nameold\">
<td><input type=\"text\" name=\"name[]\" value=\"$nameup\" class=\"m\" size=\"30\"></td>
<input type=\"hidden\" name=\"priceold[]\" value=\"$priceold\">
<td><input type=\"text\" name=\"price[]\" value=\"$priceup\" class=\"m\" size=\"5\"></td>
</tr>");
}
}

If anyone knows why it is not updating my data, please help me out. i am in desprite need of some help right now after spending countless hours just on this page! :(

timster

1:47 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This line looks fishy:

foreach ($_POST['name'] as $nameup)

$_POST is an array but $_POST['name'] is not, so I don't think this will work. You may be able to accomplish what you are trying with just:

$nameup = $_POST['name']

You'll probably need to escape your string, like so:

$nameup = mysql_escape_string($_POST['name'])

Warboss Alex

2:44 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



I think you're trying to make things a bit too difficult..

Do something like this at the top of your script:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

exit(print_r($_POST));

}

Then fill out a sample form, and submit it. The page should then just display all the posted information. This should give you a better idea of what actually gets sent to the server!

There's no need for the foreach as timster correctly pointed out. As he suggested, you should also escape your $_POST information with addslashes or mysql_escape_string - check to see if your server has magic quotes set and act accordingly. :)

svmf20

7:39 pm on Aug 19, 2004 (gmt 0)

10+ Year Member



thanks for your help...

after trying it.. nothing would happen to the datebase after updating the records..

is there an easier way to get the query to update every record regardless of whether is was changed or not?

cause if so i will just use that if a way exists.

after putting the:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

exit(print_r($_POST));

}

at the top of my code, the server recognized that the value changed, which is what i want it to do. but i seem to still have a problem with trying to get it onto the database.

for example:

lets say i have three values:

¦ id ¦ name ¦
-------------------------
1 tree
2 king
3 person
-------------------------

they would display in that order in the form. now lets say i change the second one to 'dude' instead of 'king'

the print_r would say the following:

Array ( [nameold] => Array ( [0] => tree [1] => king [2] => person ) [name] => Array ( [0] => tree [1] => dude [2] => person ) [name_add] => [submit] => Update ) 1

so thats all there is to it... the server displays changes but nothing is sent to the database.

here is my updated code:


$nameup = mysql_escape_string($_POST['name']);
$nameupdt = $db_object->query("UPDATE products SET name = '$nameup' WHERE name = '$nameold'");

and


$total = $db_object->query("SELECT * FROM products ORDER BY id ASC");
$num = $total->numRows();
if($num > 0) {
while($n = $total->fetchRow()) {
$id = $n["id"];
$nameold = $n["nameold"];
$nameup = $n["name"];
echo ("
<tr>
<td align=\"center\"><input type=\"checkbox\" name=\"delete[]\" value=\"$id\"></td>
<input type=\"hidden\" name=\"nameold[]\" value=\"$nameold\">
<td><input type=\"text\" name=\"name[]\" value=\"$nameup\" class=\"m\" size=\"80\"></td>
</tr>");
}
}

***note: i got rid of the prices array to make it easier for me.

Warboss Alex

11:41 am on Aug 20, 2004 (gmt 0)

10+ Year Member



If you want to update EVERY record, leave out the 'WHERE' clause in your query ..

svmf20

1:52 am on Aug 24, 2004 (gmt 0)

10+ Year Member



I could, but wouldn't that prevent multiple different values from being edited?

For example (using the previous one),

i have the three textboxes with values, tree king and person. lets say i update person to dude and it actually works. wouldn't leaving the WHERE out in the query cause all of the values become the same one - in this case they would all become dude - because you aren't telling it which row or record in the database to update to?

svmf20

2:15 am on Aug 24, 2004 (gmt 0)

10+ Year Member



After taking the WHERE out of my query, now i have another problem.

After trying to update the different textboxes, all that displays after reloading the page from submission is the value "Array" and i think it is due to the fact that i am using the $_POST['name'] variable because i have had this problem before. Now i don't know what to put. i have the


$nameup = mysql_escape_string($_POST['name']);

but it is comming up and uploading the database saying that the value is Array.

At least it is updating the database, but not in the way i want it to.