Custom function for inserting values into MySQL

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

 



Okay friends, I have been wondering about writing a simple function that
will help me with my MySQL inserting. Not because I need to save time and
space, but because I wanted to.

I wrote a function for inserting 10 values (I have not been able to come up
with an idea how to make the number of values I'm inserting variable, so I'm
sticking with ten).

This function takes 22 parameters: #1 is the table name, #2-21 are the row
names and the values, and #22 is the "integar string".

The first 21 parameters are self-explanatory, the 22nd is a string of values
that need to be inserted as an integar, basically, not adding single quotes
around the value. Eg. $value2 = 5, not $value2 = '5'.

I am very hesitant to try this one out on my database, I've got tables of
important information and don't want to, I don't know, inadvertantly throw a
wrench into the works, AND I want to open up a dialoug about custom PHP
functions for working with MySQL, for the fun of it!

Here is my 10 value function for inserting data into a MySQL database table.

function insertinto10($table, $field1, $value1, $field2, $value2, $field3,
$value3, $field4, $value4, $field5, $value5, $field6, $value6, $field7,
$value7, $field8, $value8, $field9, $value9, $field10, $value10, $int =
NULL)
{
 if (isset($int))
 {
  $sPattern = '/\s*/m';
  $sReplace = '';
  $int = preg_replace($sPattern, $sReplace, $int);
  $pieces = explode(",", $int); // $pieces[0], $pieces[1] - each equal to
value numbers that are integars
  $length = count($pieces);
  // call custom function to create associative array eg. $newarray[2] = 1,
$newarray[4] = 1, $newarray[5] = 1 . . .
  $integarArray = strtoarray($length, $int);
 }

 $valuesArray = array($value1, $value2, $value3, $value4, $value5, $value6,
$value7, $value8, $value9, $value10);

 foreach ($valuesArray as $key => $value)
 {
  if (isset($integarArray[$key]) && $integarArray[$key] == 1)
  {
   // INTEGAR VALUE
   $valuesArray[$key] = mysql_real_escape_string(stripslashes($value));
  }
  else
  {
   // STRING VALUE
   $cleanValue = mysql_real_escape_string(stripslashes($value));
   $valuesArray[$key] = "'{$cleanValue}'";
  }
 }

 $result = mysql_query("INSERT INTO `{$table}` (`{$field1}`, `{$field2}`,
`{$field3}`, `{$field4}`) VALUES ({$valuesArray[1]}, {$valuesArray[2]},
{$valuesArray[3]}, {$valuesArray[4]}, {$valuesArray[5]}, {$valuesArray[6]},
{$valuesArray[7]}, {$valuesArray[8]}, {$valuesArray[9]},
{$valuesArray[10]})");
 return $result;
}


You may find copying/pasting into your favorite code-editor helps make it
more readable.

Do you see any major hangups or screwups on first glance? And is my fear of
trying this out on my database unfounded? Does this even seem that useful?

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux