binodkumars Posted April 17, 2013 Share Posted April 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 17, 2013 Share Posted April 17, 2013 Assuming your table just has the one field for these values: $q = 'INSERT INTO table VALUES '; foreach ($sm as $value) $q .= '("'.$value.'"),'; $q = substr($q, 0, -1); mysql_query($q); You only need the quotes if the data is a string. Quote Link to comment Share on other sites More sharing options...
binodkumars Posted April 18, 2013 Author Share Posted April 18, 2013 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 ?? Quote Link to comment Share on other sites More sharing options...
binodkumars Posted April 18, 2013 Author Share Posted April 18, 2013 (edited) 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 April 18, 2013 by binodkumars Quote Link to comment Share on other sites More sharing options...
Barand Posted April 18, 2013 Share Posted April 18, 2013 Set AUTO_INCREMENT on whichever column is defined as key 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.