Nuv Posted April 11, 2008 Share Posted April 11, 2008 Hey guys, I am inserting an array in mysql table $num = count($data); for ($c=0; $c < $num; $c++) { $insert = mysql_query("INSERT into `$table` VALUES('$data[$c]')"); } where $data is just an element in a line which will constitute fields in mysql Just wanted to know if my sql query is right Link to comment https://forums.phpfreaks.com/topic/100673-sql-inserting-an-array/ Share on other sites More sharing options...
p2grace Posted April 11, 2008 Share Posted April 11, 2008 Typically you add the field name in the query in which the data is for. <?php $num = count($data); for ($c=0; $c < $num; $c++) { $insert = mysql_query("INSERT INTO `$table` (`fieldname`) VALUES ('".$data[$c]."')"); } ?> Link to comment https://forums.phpfreaks.com/topic/100673-sql-inserting-an-array/#findComment-514860 Share on other sites More sharing options...
Nuv Posted April 11, 2008 Author Share Posted April 11, 2008 I cant as i dont know what that array would be,how many fields will be there Link to comment https://forums.phpfreaks.com/topic/100673-sql-inserting-an-array/#findComment-514865 Share on other sites More sharing options...
p2grace Posted April 11, 2008 Share Posted April 11, 2008 Couldn't you store the field names in the array as well, or convert the data array to be multidimensional? Link to comment https://forums.phpfreaks.com/topic/100673-sql-inserting-an-array/#findComment-514868 Share on other sites More sharing options...
Nuv Posted April 11, 2008 Author Share Posted April 11, 2008 Well i can but i just want to insert elements into mysql as it comes Link to comment https://forums.phpfreaks.com/topic/100673-sql-inserting-an-array/#findComment-514948 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.