php array into sql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lord
Forum Newbie
Posts: 5
Joined: Sun Nov 07, 2010 12:30 pm

php array into sql

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php array into sql

Post 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?
lord
Forum Newbie
Posts: 5
Joined: Sun Nov 07, 2010 12:30 pm

Re: php array into sql

Post 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.
lord
Forum Newbie
Posts: 5
Joined: Sun Nov 07, 2010 12:30 pm

Re: php array into sql

Post by lord »

thanks tasairis

I got it...
Post Reply