Forum Moderators: coopster
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.
"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.
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...
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'");
}
}
$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! :(
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'])
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. :)
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.
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?
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.