zxcv20009 Posted August 3, 2007 Share Posted August 3, 2007 if i have arry for example $array = array(1, 2, 3, 4, 5); and i would like to updat this values in array to table called Main so every element in the arry in separate row inside the table Main Quote Link to comment https://forums.phpfreaks.com/topic/63235-help-array-with-row-in-mysql/ Share on other sites More sharing options...
wildteen88 Posted August 3, 2007 Share Posted August 3, 2007 Something like this: $arr = array('Value1', 'Value2', 'Value3'); $sql = "INSERT INTO tbl_name (`Col1`, `Col2`, `Col3`) VALUES('" . implode("','", $arr) . "')"; echo $sql; // Result: INSERT INTO tbl_name (`Col1`, `Col2`, `Col3`) VALUES('Value1','Value2','Value3') mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/63235-help-array-with-row-in-mysql/#findComment-315181 Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 For separate rows <?php $array = array(1, 2, 3, 4, 5); foreach ($array as $val) { mysql_query ("INSERT INTO table (colname) VALUES ($val)"); } Quote Link to comment https://forums.phpfreaks.com/topic/63235-help-array-with-row-in-mysql/#findComment-315216 Share on other sites More sharing options...
zxcv20009 Posted August 6, 2007 Author Share Posted August 6, 2007 thanks for replying: to all thanks for all answers but this code adding number 3 to last row i need code to upate different rows in my table i did such this code $array = array(1, 2, 3); foreach ($array as $val) { mysql_query ("UPDATE main SET State1 = ($val)"); } this code add number 3 for all the rows in my table and this not correct i need add in row1 value 1=1 row2 value2 =2 row3 value3= 3 ??? ??? ??? Quote Link to comment https://forums.phpfreaks.com/topic/63235-help-array-with-row-in-mysql/#findComment-316817 Share on other sites More sharing options...
Barand Posted August 6, 2007 Share Posted August 6, 2007 If you are updating, you need to identify the record to be update with a where clause mysql_query ("UPDATE main SET State1 = ($val) WHERE somecol = 'something' "); Quote Link to comment https://forums.phpfreaks.com/topic/63235-help-array-with-row-in-mysql/#findComment-316844 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.