Delicious!….hmmmm, yes!!!
This video explains what the title means. Anyway, one of the things I hate about PHP is how redundant the MySQL statements are. Each one is the same thing, but different only by a little bit. Experiencing the same thing? Well, this function I just wrote will help lighten the redundancy!
<?php
function add_mysql_entry($info)
{
if(is_array($info) and sizeof($info) > 0)
{
// Python: mysql_values,mysql_tables = {},{}
$mysql_values = array();
$mysql_tables = array();
if(is_array($info['post']))
{
foreach($info['post'] as $post)
{
// You need single quotes, or else it’s a suntax error.
$mysql_values[] = “‘”.htmlspecialchars($_POST[$post[0]]).”‘”;
$mysql_tables[] = $post[1];
}
}
if(is_array($info['background'])) {
foreach($info['background'] as $background_info)
{
$mysql_values[] = “‘”.$background_info['value'].”‘”;
$mysql_tables[] = $background_info['column'];
}
}
$mysql_values = implode(’,', $mysql_values);
$mysql_tables = implode(’,', $mysql_tables);
$query = mysql_query(”
INSERT
INTO “.$info['table'].”(”.$mysql_tables.”)
VALUES(”.$mysql_values.”)”) or die(mysql_error());
}
}
?>
This function is basically a simple MySQL INSERT statement. It makes it so that you don’t have to declare each $_POST variable, preform all precautions, execute the query, and then make sure that the query did what you wanted it to do. Here’s an example:
<?php
$info = array(
'table' => 'shoes_tb',
'post' => array(
array('add_shoes_name', 'shoe_name'),
array('add_shoes_desc', 'shoe_desc'),
array('add_shoes_price', 'shoe_price')
),
'background' => array(
array('value' => date("d/m/Y"), 'column' => 'shoe_date_added')
)
);
$add_shoe = add_mysql_entry($info);
?>
In this example, there are three $_POST variables that the programmer wants to use: add_shoes_name, add_shoes_desc, and add_shoes_price. He wants insert them in the columns shoe_name, shoe_desc, and shoe_price in the table shoes_tb. Of course, getting something such as the date and not letting the user see it can’t be done through a $_POST variable (other than using the hidden input type). This is where background comes in. The syntax of the $info array is:
<?php
$info = array(
'table' => the MySQL table's name,
'post' => array(
array(input_field_1, column_name_1),
array(input_field_2, column_name_2),
...
array(input_field_n, column_name_n)
),
'background' => array(
array('value' => value_1, 'column' => column_name_1),
array('value' => value_2, 'column' => column_name_2),
...
array('value' => value_n, 'column' => column_name_n)
)
);
?>
All in all, this is function is delicious.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!









October 20th, 2008 at 11:49 am
anybody here know of a good site to find more info on php declare array? I’ve got this site bookmarked and im gonna keep checking it out, but i still would like to find a site that covers php declare array a little more thoroughly..thanks
October 20th, 2008 at 11:07 pm
Do you mean *declaring arrays* or the *declare* construct? I’m somewhat confused here, because *declaring arrays* is quite simple. (They’re very different things). If you can be a bit more specific, I’ll see if I can help. :]