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...
Search This Blog
Blog Archive
-
▼
2009
(257)
-
▼
June
(31)
- Google Voice gives One number for all your phones
- CellPhones will act as Credit Card by next Year.
- Get Tweet to know when ISS (International Space St...
- Google announces Dashboard for providing detailed ...
- Google focuses more on Mobile devices (iPhone and ...
- Take care of letter case when specifying colSpan a...
- How to force FireFox to open new window instead of...
- Some useful php functions
- Essential Steps for Software Development
- How to get last row from database table?
- javascript code for preventing data loss in web form.
- How to count number of times the user clicks on a...
- Removing white spaces entered in TextArea tag of H...
- Workaround for inconsistent issue with mySql query...
- Need for having Eval function in javascript
- PHP code for listing files in a Folder
- Reverse Auction and finding best price among multi...
- Comments/Suggestions from QA Expert to improve Quiz
- Google is experimenting Squared which will return ...
- Twitter - Are you hearing this word first time?
- Google is taking steps for preventing Plagiarism i...
- Resolved IE specific issue related to calling func...
- foreach loop equivalent in javascript.
- Is it possible to use same domain name for two ser...
- Use Gravatar to display your icon image when addin...
- Use of tinyurl in Twitter
- Introduction to Ajax and sample code for learning ...
- Advantages of php over asp and converting asp site...
- Bing - Mircrosoft's Decision Engine. Will it perfo...
- Free online Quiz for learning php, javascript, mys...
- PHP function for inserting or updating row into da...
-
▼
June
(31)

AI Course | Bundle Offer | Unlocking AI | Dream Big | Listen to Dream Big
Today's Deals | Timesheet | Products | SQL ebook | Earn 50% commission
About | Privacy | Follow | TOS | WhatsApp | Contact
I may earn a commission from Amazon affiliate links
No comments:
Post a Comment