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. Link to comment https://forums.phpfreaks.com/topic/277072-insert-data-stored-in-an-array-to-a-database/ 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. Link to comment https://forums.phpfreaks.com/topic/277072-insert-data-stored-in-an-array-to-a-database/#findComment-1425431 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 ?? Link to comment https://forums.phpfreaks.com/topic/277072-insert-data-stored-in-an-array-to-a-database/#findComment-1425520 Share on other sites More sharing options...
binodkumars Posted April 18, 2013 Author Share Posted April 18, 2013 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". ??? Link to comment https://forums.phpfreaks.com/topic/277072-insert-data-stored-in-an-array-to-a-database/#findComment-1425521 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 Link to comment https://forums.phpfreaks.com/topic/277072-insert-data-stored-in-an-array-to-a-database/#findComment-1425548 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.