Jump to content

Recommended Posts

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 ",";
	}
}
)


");

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965937
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/183019-php-loop-in-mysql/#findComment-965962
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.