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.

Link to comment
Share on other sites

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 ??

Link to comment
Share on other sites

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". ???

Edited by binodkumars
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.