dizzy1 Posted November 26, 2009 Share Posted November 26, 2009 Hi im using phpMyAdmin - 2.11.9.5 and trying to insert into MYSQl using a PHP array: foreach( $array as $key => $Value){ echo "'".$Value."'"; if ($key != $sizeofarray){ echo ","; } } ) Thanks In Advance DIZZY Quote Link to comment https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/ Share on other sites More sharing options...
jamesxg1 Posted November 26, 2009 Share Posted November 26, 2009 Can you elaberate in more detail please. Cheers James. Quote Link to comment https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965926 Share on other sites More sharing options...
dizzy1 Posted November 26, 2009 Author Share Posted November 26, 2009 I am trying to loop an array of results into the VALUES of a MySql, and then insert into the DB. I think my syntax is wrong around echo "'".{'$Value'}."'"; More of the MYSQL Code: $mysql = mysql_query( " INSERT INTO 'TestTable' ( foreach( $Array1 as $key => $Value){ echo $Value; if ($key != $SizeOfArray1){ echo ","; } } ) VALUES ( foreach( $Array2 as $key => $Value){ echo "'".{'$Value'}."'"; if ($key != $SizeOfArray2){ echo ","; } } ) "); Quote Link to comment https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965937 Share on other sites More sharing options...
premiso Posted November 26, 2009 Share Posted November 26, 2009 You are way off on your syntax: <?php $cols = "" foreach( $Array1 as $key => $Value){ $cols .= $Value; if ($key != $SizeOfArray1){ $cols .= echo ","; } } $vals = "" foreach( $Array2 as $key => $Value){ $vals .= "'".{'$Value'}."'"; if ($key != $SizeOfArray2){ $vals .= ","; } } $sql = mysql_query(" INSERT INTO TestTable ($cols) VALUES ($vals)"); ?> Notice in the mysql_query statement you had single quotes ( ' ) around TestTable, in MySQL it should be backticks ( ` ) around column names and tables which are really only needed if you are using a reserved word. Questions let me know.[/code] Quote Link to comment https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965962 Share on other sites More sharing options...
dizzy1 Posted November 26, 2009 Author Share Posted November 26, 2009 wow i cant believe i didn't think of this, its brilliant. Thanks, i had to add a ; at the end of $cols = "" and $vals = "" but it works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965989 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.