Mostly any web development will require to insert or update form data into a database.
The number of fields and names of form fields will vary in each and every form in a website.
And, sometimes we need to insert the data and sometimes we need to update the data based on whether the key field is already available or not.
So, we had to write many different queries to insert/update data in many forms.
Ultimately, it will take lot of time and effort.
Below function written in php will help to save time, as we need not explicitly write the form names. Only thing is you have to give the form field names exactly same as database table field names.
The below function will insert the named $fields of the given $post array into table $table_name if $id is null.
It will update the existing row if $id is not null.
The $id of the row is always returned.
function insert_or_update_row( $table_name, $id, $fields, $post )
{
if ( $id === null )
{
// insert
$sql = "INSERT INTO $table_name (".join(", ",$fields).")
VALUES ('".join("','", map( $fields, sql_field, $post ) )."')";
}
else
{
$sql = "UPDATE $table_name SET ".
join( ", ", map( $fields, update_field, $post ) )."
WHERE id = $id";
}
if ( mysql_query( $sql ) )
{
if ($id === null) return mysql_insert_id();
return $id;
}
else
query_error( $sql );
}
function map( $orig, $fn )
{
for ( $i = 2; $i < func_num_args(); $i++ )
$arg_list[] = func_get_arg($i);
$new = array();
if ( $orig )
{
foreach( $orig as $x )
$new[] = $fn($x,$arg_list);
}
return $new;
}
Note that below functions used in map() are built-in php function.
func_num_args — Returns the number of arguments passed to the function.
func_get_arg — Return an item from the argument list.
Find below the sample usage of this function.
insert_or_update_row( "employee_table", $_POST[id],
array( 'id', 'emp_name', 'address', 'title', 'phone', 'enabled' ),
$_POST )) )
More Articles...
Trending: Do It Anyway | Websites Price List | 10 Facebook Tips | Robert Bob Moog Doodle 
Tuesday, June 2, 2009
Subscribe to:
Post Comments (Atom)
Popular Posts - This week
-
New: 2012 Plus Two Exam Results +2 Exam Results were declared in TamilNadu for the HSC (Higher Secondary)Exams held in March 201...
-
Tamilnadu Directorate of Government Examinations has announced the +2 Results today.(May 22nd, 2012) Nearly 7.5 lakhs students in Tamil N...
-
Find below MCQ (Multiple Choice) questions and Answers useful for learning Computer Hardware. You can attend our Quiz here. We have alre...
Popular Posts - All Time
-
Find below MCQ (Multiple Choice) questions and Answers useful for learning Computer Hardware. You can attend our Quiz here. We have alre...
-
Find below MCQ (Multiple Choice) questions and Answers useful for learning HTML. You can attend our Quiz here. We are working on to publ...
-
Find below MCQ (Multiple Choice) questions and Answers useful for learning Software Testing. You can attend our Quiz here. eBook for lear...
-
1)What type of devices are CDs or DVDs? a) output b) storage ...
-
Find below MCQ (Multiple Choice) questions and Answers for PHP. You can attend our Quiz here. We are working on to publish more quest...
-
In my previous post I have explained the importance of doing Link Exchange to boost Search Rank our web pages in Search Engines such as G...
-
This is the Guest post from Barbara Young . You can read the Guidelines if you are interested in writing Guest posts. Here’s a...
-
One of our Customers wants to have Auto Suggestion similar to Google search in the Search input of his website. So, I referred this ar...
-
Find below MCQ (Multiple Choice) questions and Answers useful for learning Javascript. You can attend our Quiz here. We are working on t...
-
New: 2012 Plus Two Exam Results +2 Exam Results were declared in TamilNadu for the HSC (Higher Secondary)Exams held in March 201...
Useful Links
- Randi Zuckerberg
- Memory Game
- Do it Anyway Poem
- Heello
- RtoZ Media
- Motivational Quotes
- Google One Pass
- verizon iphone manual download
- Social Networking Sites List
- Best Motivational Quotes Video
- Social Media News
- QTP Book
- QTP eBook - An eBook about Software Testing and QTP Automation
- See Latest Doodle published by Google in Home Page for representing Special days
- php reference - Get everything you need to learn in php
- Learning web in very easy and simple way. Starting point for learning web...
News
Tech News Software Testing News News about Google Business News
Medical News Tamil News News Archive








0 comments:
Post a Comment