Page 1 of 1

dynamic adding record with dynamic forms

Posted: Fri Feb 18, 2005 4:16 am
by reiqwan
Hi all;

I have a dynamically created form which updates a table using the following code I can almost achieve it:

Code: Select all

<?php
for($y=0; $y < $dbfield_num; $y++) &#123;
      $field_name = mysql_field_name($dbfields, $y);
     
      echo("<tr><td><strong>$field_name</strong></td><td><input type='text' name='Field&#1111;]' id='Field&#1111;]'></td></tr>");
    &#125;
	?>

but the problem is when I try and run an update using this form with the following code:

Code: Select all

if ((isset($_POST&#1111;"MM_insert"])) && ($_POST&#1111;"MM_insert"] == "form1")) &#123;
//************************************************
 //list all fileds for user and write to database
  $dbfields = mysql_list_fields($database_RespondNow, $tbl, $RespondNow);
    $dbfield_num = mysql_num_fields($dbfields);
	//echo $dbfield_num;
	$y=0;
	$fieldArray='';
	 foreach ($_POST&#1111;'Field'] as $Field_num => $Field_value) &#123;
	 	$y++;
	&#1111;b]LINE 55: $field_name = mysql_field_name($dbfields, $y);&#1111;/b]//echo $field_name;
$fieldArray .="'$Field_value',";
//$EmailBody .="$field_name:  $Field_value\n";

    &#125;
	$fieldArray=rtrim($fieldArray,',');
	
	$sql = mysql_query("INSERT INTO $tbl VALUES (NULL,$fieldArray)");
	echo $sql;
	if (!$sql) &#123;
echo ('error'.mysql_error());
&#125;
I then getting the following error:
Warning: mysql_field_name() [function.mysql-field-name]: Field 5 is invalid for MySQL result index 11 in C:\Program Files\Apache Group\Apache2\htdocs\respondnow\Admin\addinfo.php on line 55
errorColumn count doesn't match value count at row 1
I think it has something to do with the amount of rows b'cos when I echo the column headings out it doesn't give me the ID column so thats a mismatch with the insert row column amount :?

Anyways any help would be appreciated.

THanks

Posted: Fri Feb 18, 2005 4:22 am
by Wayne
move your incrementor ($y++) to the end of that foreach loop!

great

Posted: Fri Feb 18, 2005 4:46 am
by reiqwan
Most appreciated I can't believe I overlooked something so small;

thanks again :wink: