Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update multiple rows from multiple <input type="text"> 2

Status
Not open for further replies.

smashing

Programmer
Joined
Oct 15, 2002
Messages
170
Location
US
I have gotten a page (lets call it script1.php) to select many rows from a table, and display it in a HTML table, the last column of which I display a form text box (<input type="text">) on each row. I'm wondering how to tell the next script (script2.php) to dynamically UPDATE the table and SET each row with the new information from script 1. Each row has a unique column called ID. I'm thinking of naming each text box in scrpt1.php dynamically like this <input type=\"text\" name='comments" . $rowd[comm]. "' value='" . $rowd[comm]. "'>

so that each text box will be calledcomments with the suffix of the id of that row, or I think I've once read I can just call each text box like this:
comments[]
and I'll get it into an aray?

My main problem is with the second form, I hope you understand what I mean
 
Yes, if you name a series of form inputs like:

<input type="text" name="foo[1]">
<input type="text" name="foo[2]">
<input type="text" name="foo[3]">
<input type="text" name="foo[4]">

Then in the script to which the form submits, $_POST['foo'] (assuming POST-method form) will itself be an array.

I recommend that you output your text to be edited in your input fields, but also output the text in hidden fields:

<input type="text" name="foo[1]" value="somevalue1">
<input type="hidden" name="hidden_foo[1]" value="somevalue1">
<input type="text" name="foo[2]" value="somevalue2">
<input type="hidden" name="hidden_foo[2]" value="somevalue2">
<input type="text" name="foo[3]" value="somevalue3">
<input type="hidden" name="hidden_foo[3]" value="somevalue3">

Then in the PHP script to which the form submits (again, assuming POST-method input), you can simply compare $_POST['foo'][1] to $_POST['hidden_foo'][1], etc, to see what values have been changed.

Then just update the database with only the new data.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks I'll give that a shot. If I recall correctly, i was having problems in the past with anything like $_POST['foo'][1] I was under the impression that you can't have 2 sets of [] but obviously I'm wrong
 
I've having trouble implementing this. I tried:
Code:
//update comments
$b="$_POST[thisone][]";
$c="$_POST[comments][]";
//the next line is line # 74
foreach (($b as $id ) && ($c as $comm)) {

$upd = "UPDATE testres SET comm = '$comm' WHERE id ='$id'";
@mysql_query($upd, $connection)or die(mysql_error());
}
but get error message about parse error on line 74

so then I tried:
Code:
foreach ($b as $id) {
foreach ($c as $comm) {

etc..
but get warning message: : Invalid argument supplied for foreach()

What's the right way to do this?
 
On your assignments to $b and $c:

The double quotes shouldn't be there. You're assigning values from a variable, not from a string literal, and PHP can get confused when you have associative array references inside quotes.

Also, $_POST['thisone'][] is a shorthand construct that only makes sense on the LHS of an assignment statement. PHP uses this to add a new element to the end of an array.

Assigning an array to a variable is done just by referencing the entire array:

$b=$_POST['thisone'];
$c=$_POST['comments'];



On looping through both arrays:

Try:
for ($counter = 0; $counter < count($b); $counter++)
{
if ($b[$counter] != $c[$counter])
{
//do something
}
}


or try:
foreach ($b as $key => $value)
{
if ($b[$key] != $c[$key])
{
// do something
}
}

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks again sleipnir214, no error messages any more, but db is not being updated. Here is the code: (currently I'm always updating, once I get it to work I'll fix it to only update when values have been changed, like you suggested)

Code:
//update comments
$id=$_POST[thisone];
$comm=$_POST[comments];

for ($a=0; $a < count($id); $a++) {
$upd = "UPDATE testres SET comm ='$comm[$a]' WHERE id ='$id[$a]'";
@mysql_query($upd, $connection)or die(mysql_error());
}

I also tried :
Code:
$upd .= "UPDATE testres SET comm = '". $comm[$a] ."' WHERE id ='". $id[$a] ."'";
but to no avail
 
For one thing, this line:

@mysql_query($upd, $connection)or die(mysql_error());
}

makes no sense to me. You're both using the "@" error-suppression operator and using an "or die()" statement. I recommend dropping the "@".


You may also be having a problem with the construction of your update queries.

If you output the queries, is the output reasonable SQL queries? If you copy them to a MySQL admin tool's ad-hoc query app and run them, do they update the database?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks sleipnir214, had a stupid typo in my code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top