Page 1 of 1

php array into sql

Posted: Fri Dec 03, 2010 6:26 pm
by lord

Code: Select all

	        $i = 0;
		foreach ($my_array as $value)
		{
			mysql_query("INSERT INTO bookmarks (FolderName)
				VALUES ('$my_array[$i]')");
			$i++;
		}
I'm trying to put the array values into the database. However no data is going in... any help as to why? Thank you.

Re: php array into sql

Posted: Fri Dec 03, 2010 6:54 pm
by requinix
Change the code to

Code: Select all

mysql_query("INSERT INTO bookmarks (FolderName)
    VALUES ('$my_array[$i]')") or die(mysql_error());
Then read the error message. If there is no error message then your array is probably empty or associative.

But question: you're using a foreach loop so why do you bother with $i?

Re: php array into sql

Posted: Fri Dec 03, 2010 7:07 pm
by lord
Thanks for the help tasairis. I tried what you said:

Code: Select all

                $i = 0;
		foreach ($my_array as $value)
		{
			mysql_query("INSERT INTO bookmarks (FolderName)
			VALUES ('$my_array[$i]')") or die(mysql_error());
			$i++;
			
		}
and I received the error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Augmented Reality SDK')' at line 2 -- which is the name of a bookmark.

I also removed the i and just used:

Code: Select all

		foreach ($my_array as $value)
		{
			mysql_query("INSERT INTO bookmarks (FolderName)
			VALUES ('$my_array')") or die(mysql_error());
			
		}
and no data went into the database.

Thank you.

Re: php array into sql

Posted: Fri Dec 03, 2010 7:18 pm
by lord
thanks tasairis

I got it...