Forum Moderators: coopster
I generate a form using a while statement like so...
$rs = mysql_query ("SELECT * FROM table");
while ($row = mysql_fetch_array($rs))
{
echo "<tr>
<input type='hidden' name='id[]' value='".$row['id']."'>
<td><input type='text' name='field1[]' value='".$row['field1']."'></td>
<td><input type='text' name='field2[]' value='".$row['field2']."'></td>... etc etc
And then there is one submit button at the bottom which needs to update the whole table.
The closest I've come so far is with this:
$id_req = $_REQUEST['id'];
if (!empty($id_req))
{
foreach($id_req as $id_rows)
{
$query = ("UPDATE allinfo SET
field1 = '".$_POST['field1']."', etc etc
But that just enters "Array" into all of the fields. I've tried a variation of things but can't get it to work. Any help would be appreciated.
echo '<pre>';
print_r($_POST);
echo '</pre>';
that will show you the whole post array
also, don't use $_REQUEST, stick to specifying the proper method, you should be able to get the same thing from $_POST['id']
Array
(
[id] => Array
(
[0] => 2
[1] => 1
)
[field1] => Array
(
[0] => hello
[1] => my
)
[field2] => Array
(
[0] => there
[1] => name
)
[field3] => Array
(
[0] => mr
[1] => is
)
[field4] => Array
(
[0] => world
[1] => rob
)
...
[Update] => Update
)
So this is all looks hunky dory to me.
$rs = mysql_query ("SELECT foo as name, bar as value FROM table");
while ($row = mysql_fetch_array($rs))
{
echo "<tr>
<input type='hidden' name='.$row['name'].' value='".$row['value']."'>
...
checking what that sql is returning using a SQL devenvironment would help to in case your mysql database has problems.
you aren't referencing the elements correctly
field1 will contain an element from each row, as will all of the following elements
so do you want to issue an update query for each individual row? That way you would, in this case, be sending 2 update queries because there are 2 rows.
I want to be sure before I write any code ;)
example.com/editrow.php?id=98798798
and then load the data for only that specific row and do a single update but we can get this working now.
maybe see if this works, it will just output the constructed queries so we can look at them
$outer = 0;
$query = '';
while (isset($_POST['id'][$outer])) {
$query = "UPDATE allinfo SET ";
$inner = 1;
while (isset($_POST['field' . $inner])) {
$query .= "field" . $inner . " = '" . $_POST['field' . $inner] . "',";
$inner++;
}
$query = substr($query,0,strlen($query)-1);
$query .= ' where id=' . $_POST['id'][$outer];
$outer++;
echo '<br>',$query;
//mysql_query($query);
}
that's off the top of my head so just post what the output is, not sure if I got it all right
$outer = 0;
$query = '';
while (isset($_POST['id'][$outer])) {
$query = "UPDATE allinfo SET ";
$inner = 1;
while (isset($_POST['field' . $inner])) {
$query .= "field" . $inner . " = '" . $_POST['field' . $inner][$outer] . "',";
$inner++;
}
$query = substr($query,0,strlen($query)-1);
$query .= ' where id=' . $_POST['id'][$outer];
$outer++;
echo '<br>',$query;
//mysql_query($query);
}
and check your sticky ;)
now that I have seen the actual field names try this one
$thefields = array('orderdate', 'address', 'client', 'processdate', 'deaused', 'searchcoused', 'dateepcreceived', 'dateepcgraphsent', 'datelrreceived', 'datelandreceived', 'datedrainagereceived', 'datepackassembled', 'turnaround', 'clientnotified', 'solicitornotified', 'number', 'description', 'notes');
$outer = 0;
$query = '';
while (isset($_POST['id'][$outer])) {
$query = "UPDATE allinfo SET ";
$inner = 1;
while (isset($_POST[$thefields[$inner]])) {
$query .= $thefields[$inner] . " = '" . $_POST[$thefields[$inner]][$outer] . "',";
$inner++;
}
$query = substr($query,0,strlen($query)-1);
$query .= ' where id=' . $_POST['id'][$outer];
$outer++;
echo '<br>',$query;
//mysql_query($query);
}
UPDATE allinfo SET where id=3
UPDATE allinfo SET where id=2
UPDATE allinfo SET where id=1
The closest I've come so far is with this...
$outer = 0;
$query = '';
while (isset($_POST['id'][$outer]))
{
$query = "UPDATE allinfo SET
orderdate = '".$_POST['orderdate'][$outer]."',
address = '".$_POST['address'][$outer]."',
client = '".$_POST['client'][$outer]."', ...
and I thought that was working but it turned out however many columns you updated (say two) it would apply the changes to entries with id's 1 and 2, even if you applied the changes to say rows 8 and 13.
I've tried your new version and the SQL and at first glance it looks like it's outputting the right stuff but then I ran an error check on it and tried to execute it and got this:
UPDATE allinfo SET address = 'Auriol (NR99 9RA)', client = 'Company Name', processdate = '', deaused = '', searchcoused = '', dateepcreceived = '', dateepcgraphsent = '', datelrreceived = '', datelandreceived = '', datedrainagereceived = '', datepackassembled = '', turnaround = '', clientnotified = 'on', solicitornotified = 'on', number = '', description = '', notes = '', where id='123')An error has ocured: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id='123')' at line 1:1064
<?php
$theFields = array('orderdate', 'address', 'client', 'processdate', 'deaused', 'searchcoused', 'dateepcreceived', 'dateepcgraphsent', 'datelrreceived', 'datelandreceived', 'datedrainagereceived', 'datepackassembled', 'turnaround', 'clientnotified', 'solicitornotified', 'number', 'description', 'notes');
if(isset($_POST[$theFields[0]]) && is_array($_POST[$theFields[0]])){
$totalRows = count($_POST[$theFields[0]]);
for($i=0; $i<$totalRows; $i++){
$insertFields = array(); // clear the array
foreach($theFields as $key){
if(isset($_POST[$key][$i])){
$insertFields[] = "`".$key."`='".mysql_real_escape_string($_POST[$key][$i])."'";
}
}
echo "UPDATE `allinfo` SET ".implode(', ', $insertFields)." WHERE `id`='".$_POST['id'][$i]."' LIMIT 1;".'<br />';
}
}
?>
"You can use isset() to determine whether or not a checkbox value was submitted via your form by checking for it's index in your $_POST superglobal array. "
I guess I need to pick out the two that are checkboxes and assign them blank values if they are not set, and then use this value in the mysql statement.
The second problem is that when you check some boxes they don't get applied to the right rows in the sql statement, so if I checked the checkboxes in say rows 8 and 10 then the sql statement will apply the value "on" to rows 1 and 2. I'm not sure what this is all about.
Just to confirm, this is where I'm at right now:
$thefields = array('orderdate', 'address', 'client', 'processdate', 'deaused', 'searchcoused', 'dateepcreceived', 'dateepcgraphsent', 'datelrreceived', 'datelandreceived', 'datedrainagereceived', 'datepackassembled', 'turnaround', 'clientnotified', 'solicitornotified', 'number', 'description', 'notes');
$outer = 0;
$query = '';
while (isset($_POST['id'][$outer]))
{
$query = "UPDATE allinfo SET ";
$inner = 1;
while (isset($_POST[$thefields[$inner]]))
{
$query .= $thefields[$inner] . " = '" . $_POST[$thefields[$inner]][$outer] . "', ";
$inner++;
}
$query = substr($query,0,strlen($query)-2);
$query .= " where id=" . $_POST['id'][$outer] . "";
$outer++;
echo '<br>',$query;
mysql_query($query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
(The checkboxes are 'clientnotified' and 'solicitornotified')
Then after getting them to apply to the correct column in the table there will just be the matter of getting them to apply to the correct row in the table!
Take look and see what you think... This is where I'm at right now.
$thefields = array('orderdate', 'address', 'client', 'processdate', 'deaused', 'searchcoused', 'dateepcreceived', 'dateepcgraphsent', 'datelrreceived', 'datelandreceived', 'datedrainagereceived', 'datepackassembled', 'turnaround', 'number', 'description', 'notes');
$outer = 0;
$query = '';
while (isset($_POST['id'][$outer]))
{
$query = "UPDATE allinfo SET ";
// Test to see if checkboxes are checked
if(isset($_POST['clientnotified'][$outer]))
{
$clientnotified = $_POST['clientnotified'][$outer];
}
else
{
$clientnotified = "";
}
if(isset($_POST['solicitornotified'][$outer]))
{
$solicitornotified = $_POST['solicitornotified'][$outer];
}
else
{
$solicitornotified = "";
}
$inner = 1;
while (isset($_POST[$thefields[$inner]]))
{
$query .= $thefields[$inner] . " = '" . $_POST[$thefields[$inner]][$outer] . "', ";
$inner++;
}
//$query = substr($query,0,strlen($query)-2);
$query .= "clientnotified = '".$clientnotified."', solicitornotified = '".$solicitornotified."' where id=" . $_POST['id'][$outer] . "";
$outer++;
echo '<br>',$query;
mysql_query($query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());
try switching those checkboxes to yes/no dropdowns, this way they will always have a value sent and won't screw up the order of values.
then you could put them back in the control array and not worry about special cases.