Jump to content

Insert data stored in an array to a database


binodkumars

Recommended Posts

I have an array ($sm[]) which stores several decimal numbers. Now I want to insert these data present in the array to a column "SM" present under empty table "sm".

So anybody please suggest how should I do this by writing few lines of php code.

Thanks @Lemmin for the quick reply,

 

but after implementing your suggested code

$q = 'INSERT INTO sm(SM) VALUES ';
foreach ($sm as $value)
    $q .= '("'.$value.'"),';
$q = substr($q, 0, -1);
print_r($q);
mysql_query($q) or die(mysql_error());

I am getting an error " Duplicate entry '0' for key 1 ". How should I recover from this error ??

For making things easier I am explaining the problem by giving an overview to the codes which I have used previously..

 Step 1

 Calculate the membership value :

$mem_value = array();
for($i = 0 ; $i < $rows ; $i++ )
{
$mem_value[] = round((1 - ( abs($fdata - $Age[$i]) / $average)),2);
}

STEP 2

Calculate the S.M. value

$sm = array();
for($i = 0 ; $i < $rows ; $i++ )
{
$sm[] = round(sqrt($mem_value[$i]),2);
}

STEP 3

Inserting the values present in $sm into the column "sm" present in the table "sm"

 

Now I want to know that how should I export or Insert the value of $sm in the specific column present in the table "sm". ???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.