dynamic adding record with dynamic forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
reiqwan
Forum Commoner
Posts: 25
Joined: Fri Jan 07, 2005 2:27 am

dynamic adding record with dynamic forms

Post 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
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

move your incrementor ($y++) to the end of that foreach loop!
User avatar
reiqwan
Forum Commoner
Posts: 25
Joined: Fri Jan 07, 2005 2:27 am

great

Post by reiqwan »

Most appreciated I can't believe I overlooked something so small;

thanks again :wink:
Post Reply