Could you please tell me why this code does not put the data in a row ?
---------------

foreach ($_POST as $key => $value){

$sql = "INSERT INTO ir3 ($key) ";
$sql .= "VALUES ('$value')";

if (!mysql_query($sql)) {
die('Insert Row Failed!' . mysql_error());
}
$sql="";
}
}

Thanks for any help.

Here is a simple example:

<?php
if(!isset($_POST['send']))
{
?>
	<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
	<table width = "40%" align = "center">
		<tr>
			<td>First name</td>
			<td><input type = "text" name = "fname"/></td>
		</tr>
		<tr>
			<td></td>
			<td><input type = "submit" name = "send" value = "SEND"></td>
		</tr>
		</table>
	</form>
<?php
}
else
{
	$con = mysql_pconnect("localname", "usernam", "password") or die(mysql_error());
	mysql_select_db("database",$con) or die(mysql_error());
	
	foreach($_POST as $key => $value)
	{
		if($key != "send") //Prevent the submit button's name and value from being inserted into the db
		{
			$query = "INSERT INTO tablename ($key) VALUES ('$value')";
			mysql_query($query) or die(mysql_error());
		}
	}

}
?>

Here is a simple example:

<?php
if(!isset($_POST['send']))
{
?>
	<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
	<table width = "40%" align = "center">
		<tr>
			<td>First name</td>
			<td><input type = "text" name = "fname"/></td>
		</tr>
		<tr>
			<td></td>
			<td><input type = "submit" name = "send" value = "SEND"></td>
		</tr>
		</table>
	</form>
<?php
}
else
{
	$con = mysql_pconnect("localname", "usernam", "password") or die(mysql_error());
	mysql_select_db("database",$con) or die(mysql_error());
	
	foreach($_POST as $key => $value)
	{
		if($key != "send") //Prevent the submit button's name and value from being inserted into the db
		{
			$query = "INSERT INTO tablename ($key) VALUES ('$value')";
			mysql_query($query) or die(mysql_error());
		}
	}

}
?>

Thanks.
I used your code for each etc.
I do not get an error, but the values do not show in one row, but in different rows.
Example:
first name shows in row 1
last name shows in row 2
next value shows in row 3

Instead of: first last next in one row.

Could it be that the table is not constructed in the right way?

PB

There are more simple and effective ways of doing this but it appears you are some much in love with the foreach. With a foreach loop, the only way I think you can do this is to do something like the code below:
I created a table, test, in my database. It contains the following fname, lname, city, and country.
NOTE: IF YOU USE THIS CODE YOU MUST FORCE YOU USERS TO FILL ALL ENTRIES OF THE FORM ELSE YOU WILL MESS THIS UP IN YOU DATABASE.

<?php
if(!isset($_POST['send']))
{
?>
	<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
	<table width = "40%" align = "center">
		<tr>
			<td>First name</td>
			<td><input type = "text" name = "fname[]"/></td>
		</tr>
		<tr>
			<td>Last name</td>
			<td><input type = "text" name = "lname[]"/></td>
		</tr>
		<tr>
			<td>City</td>
			<td><input type = "text" name = "city[]"/></td>
		</tr>
		<tr>
			<td>Country</td>
			<td><input type = "text" name = "country[]"/></td>
		</tr>
		<tr>
			<td></td>
			<td><input type = "submit" name = "send" value = "SEND"></td>
		</tr>
		</table>
	</form>
<?php
}
else
{
	$con = mysql_pconnect("hostname", "username", "password") or die(mysql_error());
	mysql_select_db("database",$con) or die(mysql_error());
	$holder = "";
	foreach($_POST as $value)
	{
		
		if(is_array($value)) //Only use the post variable if it is an array
		{
			
			foreach($value as $subvalue)
			{
				//Loop through, enclosing each $subvalue variable in a quote (" ") and attaching a 
				//space to it's end and concatenate all the values and store the the result in the 
				//$holder variable
				
				$holder = $holder."\"".$subvalue."\" ";
										
			}
			
		}
		
	}

	if(!empty($holder))//The user entered something into the form, use it to query the db
	{
		//Remove white spaces from the content of the $holder
		//and break it into the $new array using a black space as a delimiter
		$new = explode(" ", trim($holder));
		
		//Reconvert the $new array into a string seperating each element with a comma(,) and 
		//reassign it to the $prepared variable
		$prepared = implode(",", $new);
		
		//Form and execute your query
		$query = "INSERT INTO test (fname, lname, city, country) VALUES($prepared)";
		//NOTE: The arrangement of the values in the query must correspond with the arrangement
		//of values in the form. That if fname comes first in the form, it must come first here too.
		//The field names, however, should not necessary be the same.
		
		mysql_query($query) or die(mysql_error());
		
	}
	else
	{
		//Display an error message to the user if the $holder variable happens to be empty
		echo "Sorry, your data could not be sent because your form was empty";
	}
	
}
?>

Like I said earlier you could simply achieve the same thing using the code below:

<?php
if(!isset($_POST['send']))
{
?>
	<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
	<table width = "40%" align = "center">
		<tr>
			<td>First name</td>
			<td><input type = "text" name = "fname"/></td>
		</tr>
		<tr>
			<td>Last name</td>
			<td><input type = "text" name = "lname"/></td>
		</tr>
		<tr>
			<td>City</td>
			<td><input type = "text" name = "city"/></td>
		</tr>
		<tr>
			<td>Country</td>
			<td><input type = "text" name = "country"/></td>
		</tr>
		<tr>
			<td></td>
			<td><input type = "submit" name = "send" value = "SEND"></td>
		</tr>
		</table>
	</form>
<?php
}
else
{
	$con = mysql_pconnect("hostname", "username", "password") or die(mysql_error());
	mysql_select_db("database",$con) or die(mysql_error());
	
	//Prepare user inputs
	$fname = mysql_real_escape_string($_POST['fname']);
	$lname = mysql_real_escape_string($_POST['lname']);
	$city = mysql_real_escape_string($_POST['city']);
	$country = mysql_real_escape_string($_POST['country']);
	
	//Form your query
	$query = "INSERT INTO test (fname, lname, city, country) VALUES('$fname', '$lname', '$city', '$country')";
	mysql_query($query) or die(mysql_error());
	
}
?>

Thanks very much again for your efforts.
If it were only a couple of fields, there are ways known to me how to insert the data.
However,it is a form with more than 300 text boxes, so I am trying to find a way to
do the inserts a bit more automatically.
PB

What's in the 300 fields? Are they repeated sets of data? Surely you don't have a table with 300 columns.

If you can post your schema we may be able to help better.

You can use the code with the foreach loop, but don't you think MadCoder is right? I think there may be better ways of doing what want to do. Kindly tell us what you actually want do so we can find you a better solution for problem. You said more than 300 text fields, but you don't have to hard code all the 300 fields. You only need one for loop.

Thanks all,

To give you an idea where I am wrestling with, you could have a look at the form.
You can download the form from www.pebesoft.co.nz/ir3.htm
Thanks in advance for your help.

I went through the trouble to create insertion code which also does not work.

$sql="INSERT INTO ir3
(name,first,ratio,last,discount,address1,partfrom,address2,partto,address3,reason,irdnumber,rights,year,
R3_11A,R3_11B,R3_11D,R3_11C,R3_11E,R3_11,R3_12A,R3_12B,R3_13A,R3_13B,R3_14,R3_14A,R3_14B,R3_15A,R3_15B,
R3_16A,R3_16B,R3_16C,R3_17A,R3_17B,R3_18A,R3_18B,R3_19A,R3_20,R3_21,R3_28A,R3_22,R3_23,R3_30A,R3_30B,
R3_24,R3_31A,R3_31B,R3_25,R3_32A,R3_26,R3_32B,R3_27,R3_32C,R3_32,R3_28B,R3_33,R3_29,R3_34,R3_313233,
R3_STANDARD,R3_34A,R3_ESTIMATE,R3_OSTAX,R3_TRANSFER,R3_34B,R3_PREPAID,R3_IMPTOT,R3_PROVTOPAY,
R3_34C,R3_TOT19A,R3_34D,R3_IDATE1,R3_INST1,R3_34E,R3_IDATE2,R3_INST2,R3_TERMINAL,R3_IDATE,R3_INST3,
R3_IDATE4,R3_INST4,bank1,WT1,IN1,R3_36,bank2,WT2,IN2,R3_36A,bank3,WT3,IN3,R3_36B,bank4,WT4,IN4,R3_36C,
bank5,WT5,IN5,R3_36D,bank6,WT6,IN6,R3_36E,bank7,WT7,IN7,R3_36F,bank8,WT8,IN8,R3_36EASS,bank9,WT9,IN9,
R3_36G,bank10,WT10,IN10,R3_36H,bank11,WT11,IN11,R3_36I,bank12,WT12,IN12,R3_36HASS,bank13,WT13,IN13,
R3_36J,bank14,WT14,IN14,R3_36K,bank15,WT15,IN15,R3_CHEQUE,WT16,IN16,other1,type1,otheramt1,comp1,
im1,ct1,di1,other2,type2,otheramt2,comp2,im2,ct2,di2,other3,type3,otheramt3,comp3,im3,ct3,di3,other4,
type4,otheramt4,comp4.im4,ct4,di4,otheramt5,comp5,im5,ct5,di5omp6,im6,ct6,di6,comp7,im7,ct7,di7,expense1,
expamt1,comp8,im8,ct8,di8,expense2,expamt2,comp9,im9,ct9,di9,expense3,expamt3,comp10,im10,ct10,di10,
expamt4,comp11,im11,ct11,di11,comp12,im12,ct12,di12,comp13,im13,ct13,di13,os1,ostax1,osamt1,comp14,im14,
ct14,di14,os2,ostax2,osamt2,comp15,im15,ct15,di15,os3,ostax3,osamt3,comp16,im16,ct16,di16,os4,ostax4,osamt4,
comp17,im17,ct17,di17,os5,ostax5,osamt5,comp18,im18,ct18,di18,os6,ostax6,osamt6,comp19,im19,ct19,di19,os7,
ostax7,osamt7,comp20,im20,ct20,di20,os8,ostax8,osamt8,comp21,im21,ct21,di21,os9,ostax9,osamt9,comp22,im22,
ct22,di22,ostax10,osamt10,im23,ct23,di23,R10_1,R10_30,R10_2,R10_31,R10_3,R10_32,R10_4,R10_33,R10_5,R10_34,
R10_6,R10_35,R10_7,R10_36,R10_8,R10_37,R10_9,R10_38,R10_10,R10_39,R10_11,R10_40,R10_41,R10_12,R10_42,R10_13,
R10_43,R10_14,R10_44,R10_15,R10_45,R10_16,R10_46,R10_17,R10_47,R10_18,R10_48,R10_19,R10_49,R10_20,R10_50,
R10_21,R10_51,R10_22,R10_52,R10_23,R10_53,R10_24,R10_54,R10_25,R10_55,R10_26,R10_56,R10_27,R10_57,R10_28,
R10_58,R10_29,R10_59)
VALUES


($_POST[name],$_POST[first],$_POST[ratio],$_POST[last],$_POST[discount],$_POST[address1],$_POST[partfrom],

$_POST[address2],$_POST[partto],$_POST[address3],$_POST[reason],$_POST[irdnumber],$_POST[rights],$_POST[year],

$_POST[R3_11A],$_POST[R3_11B],$_POST[R3_11D],$_POST[R3_11C],$_POST[R3_11E],$_POST[R3_11],$_POST[R3_12A],$_POST

[R3_12B],$_POST[R3_13A],$_POST[R3_13B],$_POST[R3_14],$_POST[R3_14A],$_POST[R3_14B],$_POST[R3_15A],$_POST

[R3_15B],$_POST[R3_16A],$_POST[R3_16B],$_POST[R3_16C],$_POST[R3_17A],$_POST[R3_17B],$_POST[R3_18A],$_POST

[R3_18B],$_POST[R3_19A],$_POST[R3_20],$_POST[R3_21],$_POST[R3_28A],$_POST[R3_22],$_POST[R3_23],$_POST[R3_30A],

$_POST[R3_30B],$_POST[R3_24],$_POST[R3_31A],$_POST[R3_31B],$_POST[R3_25],$_POST[R3_32A],$_POST[R3_26],$_POST

[R3_32B],$_POST[R3_27],$_POST[R3_32C],$_POST[R3_32],$_POST[R3_28B],$_POST[R3_33],$_POST[R3_29],$_POST[R3_34],

$_POST[R3_313233],$_POST[R3_STANDARD],$_POST[R3_34A],$_POST[R3_ESTIMATE],$_POST[R3_OSTAX],$_POST[R3_TRANSFER],

$_POST[R3_34B],$_POST[R3_PREPAID],$_POST[R3_IMPTOT],$_POST[R3_PROVTOPAY],$_POST[R3_34C],$_POST[R3_TOT19A],

$_POST[R3_34D],$_POST[R3_IDATE1],$_POST[R3_INST1],$_POST[R3_34E],$_POST[R3_IDATE2],$_POST[R3_INST2],
$_POST[R3_TERMINAL],$_POST[R3_IDATE],$_POST[R3_INST3],$_POST[R3_IDATE4],$_POST[R3_INST4],$_POST[bank1],
$_POST[WT1],$_POST[IN1],$_POST[R3_36],$_POST[bank2],$_POST[WT2],$_POST[IN2],$_POST[R3_36A],$_POST[bank3],$_POST

[WT3],$_POST[IN3],$_POST[R3_36B],$_POST[bank4],$_POST[WT4],$_POST[IN4],$_POST[R3_36C],$_POST[bank5],
$_POST[WT5],$_POST[IN5],$_POST[R3_36D],$_POST[bank6],$_POST[WT6],$_POST[IN6],$_POST[R3_36E],$_POST[bank7],

$_POST[WT7],$_POST[IN7],$_POST[R3_36F],$_POST[bank8],$_POST[WT8],$_POST[IN8],$_POST[R3_36EASS],$_POST[bank9],

$_POST[WT9],$_POST[IN9],$_POST[R3_36G],$_POST[bank10],$_POST[WT10],$_POST[IN10],$_POST[R3_36H],$_POST[bank11],

$_POST[WT11],$_POST[IN11],$_POST[R3_36I],$_POST[bank12],$_POST[WT12],$_POST[IN12],$_POST[R3_36HASS],$_POST

[bank13],$_POST[WT13],$_POST[IN13],$_POST[R3_36J],$_POST[bank14],$_POST[WT14],$_POST[IN14],$_POST[R3_36K],

$_POST[bank15],$_POST[WT15],$_POST[IN15],$_POST[R3_CHEQUE],$_POST[WT16],$_POST[IN16],$_POST[other1],
$_POST[type1],$_POST[otheramt1],$_POST[comp1],$_POST[im1],$_POST[ct1],$_POST[di1],$_POST[other2],$_POST[type2],

$_POST[otheramt2],$_POST[comp2],$_POST[im2],$_POST[ct2],$_POST[di2],$_POST[other3],$_POST[type3],
$_POST[otheramt3],$_POST[comp3],$_POST[im3],$_POST[ct3],$_POST[di3],$_POST[other4],$_POST[type4],
$_POST[otheramt4],$_POST[comp4],$_POST[im4],$_POST[ct4],$_POST[di4],$_POST[otheramt5],$_POST[comp5],$_POST[im5],
$_POST[ct5],$_POST[di5omp6],$_POST[im6],$_POST[ct6],$_POST[di6],$_POST[comp7],$_POST[im7],$_POST[ct7],
$_POST[di7],$_POST[expense1],$_POST[expamt1],$_POST[comp8],$_POST[im8],$_POST[ct8],$_POST[di8],$_POST

[expense2],$_POST[expamt2],$_POST[comp9],$_POST[im9],$_POST[ct9],$_POST[di9],$_POST[expense3],$_POST[expamt3],

$_POST[comp10],$_POST[im10],$_POST[ct10],$_POST[di10],$_POST[expamt4],$_POST[comp11],$_POST[im11],$_POST[ct11],

$_POST[di11],$_POST[comp12],$_POST[im12],$_POST[ct12],$_POST[di12],$_POST[comp13],$_POST[im13],$_POST[ct13],

$_POST[di13],$_POST[os1],$_POST[ostax1],$_POST[osamt1],$_POST[comp14],$_POST[im14],$_POST[ct14],$_POST[di14],

$_POST[os2],$_POST[ostax2],$_POST[osamt2],$_POST[comp15],$_POST[im15],$_POST[ct15],$_POST[di15],$_POST[os3],

$_POST[ostax3],$_POST[osamt3],$_POST[comp16],$_POST[im16],$_POST[ct16],$_POST[di16],$_POST[os4],$_POST[ostax4],

$_POST[osamt4],$_POST[comp17],$_POST[im17],$_POST[ct17],$_POST[di17],$_POST[os5],$_POST[ostax5],$_POST[osamt5],

$_POST[comp18],$_POST[im18],$_POST[ct18],$_POST[di18],$_POST[os6],$_POST[ostax6],$_POST[osamt6],$_POST[comp19],

$_POST[im19],$_POST[ct19],$_POST[di19],$_POST[os7],$_POST[ostax7],$_POST[osamt7],$_POST[comp20],$_POST[im20],

$_POST[ct20],$_POST[di20],$_POST[os8],$_POST[ostax8],$_POST[osamt8],$_POST[comp21],$_POST[im21],$_POST[ct21],

$_POST[di21],$_POST[os9],$_POST[ostax9],$_POST[osamt9],$_POST[comp22],$_POST[im22],$_POST[ct22],$_POST[di22],

$_POST[ostax10],$_POST[osamt10],$_POST[im23],$_POST[ct23],$_POST[di23],$_POST[R10_1],$_POST[R10_30],
$_POST[R10_2],$_POST[R10_31],$_POST[R10_3],$_POST[R10_32],$_POST[R10_4],$_POST[R10_33],$_POST[R10_5],
$_POST[R10_34],$_POST[R10_6],$_POST[R10_35],$_POST[R10_7],$_POST[R10_36],$_POST[R10_8],$_POST[R10_37],
$_POST[R10_9],$_POST[R10_38],$_POST[R10_10],$_POST[R10_39],$_POST[R10_11],$_POST[R10_40],$_POST[R10_41],
$_POST[R10_12],$_POST[R10_42],$_POST[R10_13],$_POST[R10_43],$_POST[R10_14],$_POST[R10_44],$_POST[R10_15],
$_POST[R10_45],$_POST[R10_16],$_POST[R10_46],$_POST[R10_17],$_POST[R10_47],$_POST[R10_18],$_POST[R10_48],
$_POST[R10_19],$_POST[R10_49],$_POST[R10_20],$_POST[R10_50],$_POST[R10_21],$_POST[R10_51],$_POST[R10_22],
$_POST[R10_52],$_POST[R10_23],$_POST[R10_53],$_POST[R10_24],$_POST[R10_54],$_POST[R10_25],$_POST[R10_55],
$_POST[R10_26],$_POST[R10_56],$_POST[R10_27],$_POST[R10_57],$_POST[R10_28],$_POST[
R10_58],$_POST[R10_29],$_POST[R10_59])

Wow, that is a lot of fields!

Some of it you can store in separate relational tables, such as Other Income Schedule, Dividend Schedule, Interest Schedule, etc. where there is repetition and a variable number of expected responses. It would also be easier, in the long run, to have a small table that just stores the client's basic details if there is a chance the same client will need to have this form filled out again as a separate instance.

Your query is breaking because you need to wrap the values in single quotes if the value is a varchar or text.

<?php
$first = mysql_real_escape_string($_POST['first_name']);
$last = mysql_real_escape_string($_POST['last_name']);
$comment = mysql_real_escape_string($_POST['comment']);

$query = "INSERT INTO test_table (id, first_name, last_name, comment)
VALUES (null, '$first', '$last', '$comment')";
?>

You can loop through the $_POST and escape everything -- you really MUST escape your values to ensure the query doesn't break when it encounters a single quote AND to prevent your database from being hacked.

You could create a loop that builds the sql query but don't rely on the fields in $_POST arriving in any particular order. There is no guarantee that any web browser must send them to you in a specific order.

Your best bet on that is to create an array with the names of the columns that must be inserted (in the correct order, of course) and loop over that with a foreach loop. The following code assumes the form field names match the database column names.

<?php
echo "\n\n\n";
// quick test:
$_POST['first_name'] = 'John';  // just for testing.
$_POST['last_name'] = 'Doe';  // just for testing.
$_POST['comment'] =  'The quick brown fox jumps over the lazy dog.';  // just for testing.

function validEntry($key, $value)
{
  //do validation here.  Valid is true, invalid should return false.
  return true;
}
$columns = array('first_name', 'last_name', 'comment');
$data = array_fill_keys($columns, 'NULL');
print_r($data);  // just for testing.
foreach($data as $key => $value) {
echo "*$key:= $value\n";  // just for testing.
	if(validEntry($key, $_POST[$key]))
	{
//		$data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_real_escape_string($_POST[$key])."'";  // Use this version on a live server.
		$data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_escape_string($_POST[$key])."'";
	}
}
echo "\n\n";  // just for testing.
$query = "INSERT INTO test_table (id, ".implode(", ",$columns).")
VALUES (null, ".implode(",",$data).')';
echo $query;
?>

I have taken a look at your form at http://www.pebesoft.co.nz/ir3.htm and I must say thate it is a bit scaring. But one good thing I noticed about your form is how you have grouped related information under headings. This means that you know these sets of information must be grouped differently. So, why not simply create different tables to house these separate information and provide separate form to collect each set of information? For example you could create different tables for Personal Information, Income, Provisional Tax, Installments, etc, and provide separate forms for their collection. Try this and when you are done, let us know so we can help you better.

Wow, that is a lot of fields!

Some of it you can store in separate relational tables, such as Other Income Schedule, Dividend Schedule, Interest Schedule, etc. where there is repetition and a variable number of expected responses. It would also be easier, in the long run, to have a small table that just stores the client's basic details if there is a chance the same client will need to have this form filled out again as a separate instance.

Your query is breaking because you need to wrap the values in single quotes if the value is a varchar or text.

<?php
$first = mysql_real_escape_string($_POST['first_name']);
$last = mysql_real_escape_string($_POST['last_name']);
$comment = mysql_real_escape_string($_POST['comment']);

$query = "INSERT INTO test_table (id, first_name, last_name, comment)
VALUES (null, '$first', '$last', '$comment')";
?>

You can loop through the $_POST and escape everything -- you really MUST escape your values to ensure the query doesn't break when it encounters a single quote AND to prevent your database from being hacked.

You could create a loop that builds the sql query but don't rely on the fields in $_POST arriving in any particular order. There is no guarantee that any web browser must send them to you in a specific order.

Your best bet on that is to create an array with the names of the columns that must be inserted (in the correct order, of course) and loop over that with a foreach loop. The following code assumes the form field names match the database column names.

<?php
echo "\n\n\n";
// quick test:
$_POST['first_name'] = 'John';  // just for testing.
$_POST['last_name'] = 'Doe';  // just for testing.
$_POST['comment'] =  'The quick brown fox jumps over the lazy dog.';  // just for testing.

function validEntry($key, $value)
{
  //do validation here.  Valid is true, invalid should return false.
  return true;
}
$columns = array('first_name', 'last_name', 'comment');
$data = array_fill_keys($columns, 'NULL');
print_r($data);  // just for testing.
foreach($data as $key => $value) {
echo "*$key:= $value\n";  // just for testing.
	if(validEntry($key, $_POST[$key]))
	{
//		$data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_real_escape_string($_POST[$key])."'";  // Use this version on a live server.
		$data[$key] = empty($_POST[$key]) ? 'NULL' : "'".mysql_escape_string($_POST[$key])."'";
	}
}
echo "\n\n";  // just for testing.
$query = "INSERT INTO test_table (id, ".implode(", ",$columns).")
VALUES (null, ".implode(",",$data).')';
echo $query;
?>
Member Avatar for diafol

Personally, I'd use a step wizard or tabbed interface for separating the sections. Have all your sections joined to a user table by a common 'user_id'.

In addition, some of these look like calculated fields, which may not need to be stored.

Also you have repeating entries for some sections, e.g. 'Dividend schedule'.
These should their own tables, again joined to the user_id.

The more of it I see, I would definitely use a tab interface. You can then split up your form into a number of different ones, saving each as convenient.

Thanks all of you.
I am going to work on your suggestions but that will take a while.
One thing I would like to say.
The form as it is, gives the client immediate response to anything he/she changes.
That will not be possible if I cut the form in a number of different (sub) forms.
PB

Member Avatar for diafol

What response? The client can see that they've added or changed a field??

If you're talking about calculated totals, etc. You can have a totals section or %complete (whatever) above a tabs section, which is visible at all times. As you change values, the totals/%complete will also change (ajax).

The form 'as is', is frightening, as mentioned previously by others. If this is for others to use, perhaps you'd better get some user feedback.

I think this will do the trick.
Note that the posted input names must be exactly the same as the database field names.

<?php
if($_POST['kaydet']) {
    unset($_POST['kaydet']);
    $baglan = mysql_connect("localhost","root","");
    $vt = mysql_select_db("test_db",$baglan);

    foreach($_POST as $name=>$value) {
        if(!$myID) {
            mysql_query("insert into testalanlari set $name = '$value'");
            $myID = mysql_insert_id();
        } else {
            mysql_query("update testalanlari set $name = '".urlencode($value)."' where id = ".$myID);
        }

    }
}

?>
<form method="post">
<input type="text" name="testAlani1" /><br />
<input type="text" name="testAlani2" /><br />
<input type="text" name="testAlani3" /><br />
<input type="text" name="testAlani4" /><br />
<input type="text" name="testAlani5" /><br />
<input type="text" name="testAlani6" /><br />
<input type="submit" name="kaydet" value="Kaydet" />
</form>

I know too late, your code does not work because of the dot i think in the line:

//remove the dot
$sql .= "VALUES ('$value')";

Here is a similar code that works like a charm.

foreach($_POST as $key => $value) 
{
    if (strpos($key, 'somedata') === 0) 
{

mysql_query("INSERT INTO tablename VALUES ('','$value','','','')")or die("Some text Error: ".mysql_error());

}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.