Give each textbox a name that has and ID of the record in it (hopefully you have a primary key in the table, you can use it for this purpose). If you have any code yet I can show you what I mean. Or if I put it in a made-up example:
// reading the rows from database
while($row = $result->fetch_row()) {
$input_name = $row['id'];
$input_value = $row['score'];
...
echo "</td><input type=\"text\" name=\"input_name\" value=\"$input_value\" /></td>";
...
}
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
Sure. It is good to have a firm plan before you start coding. Some of us do not have this privilege and have to code for living without a detailed plan (or specifications). These days they call it agile programming :-).
Anyway, come back if you are stuck with coding.
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
Sorry to not point it out:
In the example I have used mysqli extension which is newer than mysql extension and is preffered over mysql. See this topic.
mysqli extension comes in two favours: procedural style and object oriented style in other words all functions have both variants and it is up to you to choose which one to use. Here you can see examples how to use mysqli to query a database procedural way and object oriented way. I used the later in my post above.
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
This is a bit tricky. Simple thing would be to update all displayed rows (either changed or not changed) but that is a bit redundant:
if (isset($_POST['submit'])) {
// ditch the submit element from $_POST
unset($_POST['submit']);
// loop through values in $_POST and shoot the query for each value
// (it would be more efficient to use prepared queries here)
foreach($_POST as $key => $val)
mysql_query("UPDATE members SET handicap='$val' WHERE id=$key LIMIT 1");
}
}
Above code does to mich work for nothing.
So you have to know which row has been changed. You can do that with a hidden field added to each row. The hidden field would contain the id of the row in it's name and would have a value of the existing textbox. Then when processing form you could compare the text input value with the hidden field value and if they differ update that row. Another approach would be using ajax to update a row upon leaving textbox (using an onblur javascript event). To give you examples I would need more time (and is half past midnight here). So if any other posters do not respond I'll be back tomorrow.
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
cheers for all your help mate.
It is much appreciated.
No worries.
One way of doing the updating only the changed values would be as follows (see comments in the code). I added a hidden field in each row to hold the value (same as input) and which can be then compared to see if changed.
<?php
while($row = mysql_fetch_array($result)){
$text_name = 't-' . $row['id'];
$hidden_name = 'h-' . $row['id'];
$value = $row['handicap'];
echo"<tr>";
echo"<td>" . $row['ID'] . "</td>";
echo"<td>" . $row['First'] . "</td>";
echo"<td>" . $row['Last'] . "</td>";
echo"<td>" . $row['handicap'] . "</td>";
echo "<input type=\"text\" name=\"$text_name\" value=\"$input_value\" />";
// hidden field added here; the value is the same as the input field's value
echo "<td><input type=\"hidden\" name=\"$hidden_name\" value=\"$input_value\" /></td>";
}
echo"</table>";
echo"<input type=\"submit\" name=\"submit\"/>";
//endwhile;
if (isset($_POST['submit'])) {
// initialize arrays for the text input values
// the $text_values array will contain all the values from text input boxes
// the $hidden_values array will contain all the values from hidden input boxes
// these values were initially equal for each row; if user changes the value
// in the input box, the corresponding value in the hidden input will differ
// which will trigger the update query for that row
$text_values = array();
$hidden_values = array();
// now let's go through the $_POST array where:
// $key is the name attribute of the input box
// $val is the value of that input box
foreach($_POST as $key => $val) {
// if $key starts with 't-' it is the value of the text input box
if(substr($key, 0, 2) == 't-') {
// the value will be added to the $text_values array and
// the key will be the name of the input field stripped of 't-'
$new_key = substr($key, 2);
$text_values[$new_key] = $val;
// no need to process this iterration so might as well skip the rest
// of the foreach loop
continue;
}
if(substr($key, 0, 2) == 'h-') {
// the value will be added to the $hidden_values array and
// the key will be the name of the input field stripped of 'h-'
$new_key = substr($key, 2);
$hidden_values[$new_key] = $val;
// no need to process this iterration so might as well skip the rest
// of the foreach loop
continue;
}
// if there are no values in either of arrays something must have gone wrong
if(empty($text_values) || empty($hidden_values)) {
die('Unknown error');
}
}
// now we have two equaly structured arrays: $text_values with values
// (changed or unchanged) and $hidden_values with unchanged values
// both arrays have same associative indexes
// we will go through each of the value in $text_values array and compare
// it with the value of the same index in the $hidden_values array
// if there is a difference we will run the update query for that row
foreach($text_values as $key => $val) {
// to avoid errors also check for the existence of array elements
// and then compare them
if(isset($hidden_values[$key]) && ($val != $hidden_values[$key])) {
mysql_query("UPDATE members SET handicap='$val' WHERE id=$key");
}
}
}
mysql_close($con);
?>
I haven't actually tested the code above since lack of other information. It is just a concept thing so you can have look at how I would do it. Nevertheless I would still prefer to use ajax and update each row immediately upon change.
broj1
Nearly a Posting Virtuoso
1,212 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13