Re: Is there a short way to?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2003-04-15 at 06:30, Dave Carrera wrote:

> Hi All
> 
> I have a MySql table that I am trying to post data from a form (basic
> stuff but wait a minute)
> 
> It has thirty cols but what I am trying to do is not have a form with 30
> fields but split it into little relevant bits and post them bits as I
> go.
> 
> My question is do I HAVE to "insert into table
> (name,all,the,30,cols,here) values(var1,var2,var3,and,so,on)
> 
> Or is there a glamorous way of only putting the data from the form into
> the affected rows?
> 

Short answer is yes, it is possible to do.

For the inserting information into the table, if the fields of the table
are not going to change at all, then I would highly recommend just
hardcoding the updates.

for your HTML code with your form:
<form action="your_page.php" method="POST">
<input type="hidden" name="form[fields]" name="field_1">
<input type="text" name="form[values][field_1]">

do this for all 30 fields.

this will create an array called form with 2 additional arrays in it.
one called fields and another called values. we will loop through the
through the fields array twice building our query.

Now for the php
<?php
//we first check to make sure that form is populated. this is pretty
much useless withan empty array
if( !empty( $_REQUEST[ "form" ] ) )
{
	//now we start our insert query
	$query = "INSERT INTO table_name ( ";
	foreach( $_REQUEST[ "form" ][ "fields" ] as $field )
	{
		$query .= " $field ";
		//now we check to see if this is the last field in the array or not
		//we are getting the size of the array and subtracting one as the
first field of an array is 0
		
		if( $field != $_REQUEST[ "form" ][ "fields" ][ sizeof( $_REQUEST[
"form" ][ "fields" ] ) - 1 ]
		{
			//if it is not the last field in the array we can add a , 
			$query .= ", ";
		}
	}
	//now for the values
	$query .= " VALUES( ";
	//we loop through the fields again and this time pull the value from
our values array
	foreach( $_REQUEST[ "forms" ][ "fields" ] as $field )
	{
		$query .= "'". $_REQUEST[ "form" ][ "values" ][ $field ] ."' ";
		if( $field != $_REQUEST[ "form" ][ "fields" ][ sizeof( $_REQUEST[
"form" ][ "fields" ] ) - 1 ]
		{
			//if it is not the last field in the array we can add a , 
			$query .= ", ";
		}
	}
	$query .= " ) ";
	
	print "query = $query <br />";
}

?>
	
this should make you a nice pretty query

> And help, examples or online resources very much appreciated.
> 
> Thank You
> 
> Dave C
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.471 / Virus Database: 269 - Release Date: 10/04/2003
>  
> 
> 
> -- 
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux