0

I have an update form that uses a loop to echo a list of records to be updated. It includes a text field , 'Sentences' and a checkbox, 'Acceptable'. My foreach efforts are not working and are gobbling columns of data so it is time to ask for help. Gentlemen and Ladies, how do I write this?

Thanks for your help.

<?php 
if( isset($_POST["Sentences_ID"]) ) {
    foreach($_POST['Sentences_ID'] as $key=>$value) {
        $updateSQL = sprintf("UPDATE Sentences SET Sentences=%s, Acceptable=%s WHERE   Sentences_ID=$value",
            GetSQLValueString($_POST['Sentences'], "text"),
            GetSQLValueString($_POST['Acceptable'], "text"),
            ($_POST['Sentences_ID']));

        mysql_select_db($database_name, $name);
        $Result1 = mysql_query($updateSQL, $name) or die(mysql_error());
    }
}
?>


<?php
do {
?>
Sentence:<input type="hidden" name="Sentences_ID[]" id="Sentences_ID[< ?php echo     $row_rsCounting['Sentences_Id'];?>]" value="<?php echo $row_rsCounting['Sentences_Id'];?>"   />

Acceptable:<input type="checkbox" name="Acceptable" id="Acceptable"     value="Acceptable"/></label> | 
<input type="text" name="Sentences" id="Sentences" value="< ?php echo     $row_rsCounting['Sentences'];?>" size="50" />
<?php
} while ($row_rsCounting = mysql_fetch_assoc($rsCounting));
?>
5
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. Commented Mar 14, 2013 at 17:08
  • Thank you. I'm planning to make the change, but the project is too big to stop and redo at the moment. On the list though... Commented Mar 14, 2013 at 17:10
  • By the way, can you point me to a good, easy-to-understand article on making the conversion to mysqli? Thanks Commented Mar 14, 2013 at 17:28
  • I would recommend using the official PHP.net manual. Commented Mar 14, 2013 at 17:33
  • Reads like a novel. :-) Thanks. Commented Mar 14, 2013 at 17:42

2 Answers 2

1

The Acceptable and Sentences inputs need to be arrays also, like name="Acceptable[]". And then you would access them in your PHP loop like $_POST['Acceptable'][$key]. However, I don't think there is a guarantee that multiple input arrays will be ordered the same, so I would recommend using the Sentence ID in the input names. Do that for all inputs.

Something like so...

<?php 
if( isset($_POST["Sentences_ID"]) ) {
    foreach($_POST['Sentences_ID'] as $key=>$value) {
        $updateSQL = sprintf("UPDATE Sentences SET Sentences=%s, Acceptable=%s WHERE   Sentences_ID=$value",
            GetSQLValueString($_POST['Sentences'][$key], "text"),
            GetSQLValueString($_POST['Acceptable'][$key], "text"),
            ($value));

        mysql_select_db($database_name, $name);
        $Result1 = mysql_query($updateSQL, $name) or die(mysql_error());
    }
}
?>


<?php
do {
?>
Sentence:<input type="hidden" name="Sentences_ID[< ?php echo  $row_rsCounting['Sentences_Id'];?>]" id="Sentences_ID[< ?php echo     $row_rsCounting['Sentences_Id'];?>]" value="<?php echo $row_rsCounting['Sentences_Id'];?>"   />

Acceptable:<input type="checkbox" name="Acceptable[< ?php echo     $row_rsCounting['Sentences_Id'];?>]" id="Acceptable" value="Acceptable"/></label> | 
<input type="text" name="Sentences[< ?php echo $row_rsCounting['Sentences_Id'];?>]" id="Sentences" value="< ?php echo $row_rsCounting['Sentences'];?>" size="50" />
<?php
} while ($row_rsCounting = mysql_fetch_assoc($rsCounting));
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks SomeSilly, let me work more with this and let you know.
DUUDE! You've killed FormZilla. ('he who eats data columns') THANKS.
0

My first suggestion would be to do a little debugging.

Before

foreach($_POST['Sentences_ID'] as $key=>$value) {

Put

print_r($_POST['Sentences_ID']);die;

This should give you some insight into the structure of the variable you're trying to iterate over

4 Comments

Thanks. I have done and it is echoing out the list of the affected records but at this point is updating each record with the initial data from the final record in the list. Rather annoying.
If you can post the output I might be able to give you a little insight as to why the foreach loop isn't working.
Array ( [Sentences_Id] => Array ( [128] => 128[197] => 197 [198] => 198 [199] => 199 [200] => 200 [201] => 201 [202] => 202 [203] => 203 [205] => 205 ) [Sentences] => We intend to call the cops. [Acceptable] => Acceptable [KT_Update1] => Save your recommendations )
We've got it figured zjd. Thank you for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.