Hi,

I have 2 form elements which are arrays.

<input type=\"text\" name=\"text[]\" value\"$text\" />
<input type=\"text\" name=\"textid[]\" value\"$textid\" />

When the user posts this form I need to update my table records with the values of $text using the id found in textid[] to find that record in my table.

I decided to use this method:

if ($_POST['text']) {

$text = $_POST['text'];
$textid = $_POST['textid'];

foreach($text as $newtext) {
    foreach($textid as $key => $value) {
        $sqlupdate = "UPDATE mytable SET textvalue = $newtext WHERE textid = $key";
     }
}

}

Unfortunately at the moment, when I echo my query, the id field in the WHERE clause is always the same ID and incidentally, it is the LAST ID of the collection of text fields.

What part of this is incorrect?

Many Thanks
K

    You nest array iterations when you're using a multidimensional array. I'm not sure that you are using a multidimensional array. Is it not a linear mapping from your textid array to your text array?

    if ($_POST['text']) {
      // map the keys (or do you want values? from textid to the values of text
      // as one array and iterate over it
      foreach (array_combine(array_keys($_POST['textid']), $_POST['text']) as $id => $value)
      {
        $sqlupdate = "UPDATE mytable SET textvalue = $value WHERE textid = $id";
      }
    }
    

    Though I may have misunderstood what you're trying to do...

      Each $text has its own $textid in my table.

      I have for example, 20 occurances of $text displayed on my form. The content of $text is different each time and each one is editable.

      When the user submits the form, I simply want to be able to update ALL occurances of $text and use it's $textid to identify which record in the database needs to be updated.

      My current code always updates the ID of the last occurance of $text on my form.

      Your code above required the array_combine function to be declared.

      Hope I'm making sense!

        I suggest that you create the form with:

        <input type="text\" name=\"text[$textid]\" value\"$text\" />

        Now, to update you would use:

        if (isset($_POST['text']) && is_array($_POST['text'])) {
        	foreach ($_POST['text'] as $textid => $text) {
        		// I am assuming that textid is an integer
        		if (ctype_digit($textid)) {
        			// ensure $text is safe for entry
        			// ...
        			$query = "UPDATE mytable SET textvalue='$text' WHERE textid=$key";
        			// run query
        			// ...
        		}
        	}
        }

          Hi Laserlight,

          Thanks for that, it is working fine now except that it always misses the very first text item in the array.

          What would you suggest?

            Thanks for that, it is working fine now except that it always misses the very first text item in the array.

            It should not. Have you checked that the form was generated correctly?

              Write a Reply...