[PHP] Insert an array into MySQL
Hi, I've seen a few post for this same thing but I can't seem to get it working. What I'm doing is taking a form with check boxes and putting them into an array. Then I'm attempting to INSERT them into the db, one checkbox value per row. Okay, well I've tries 2 ways that I think should work and they only insert the first value. They both produce this error "Duplicate entry '0' for key 1" (which I don't understand) Anyway here is the code I'm trying... (I'm not trying to use both blocks of code, just one or the other) // First code while(list($key,$value)=each($pagetype)){ //echo $value."";//test $links="INSERT INTO $username_links (links) VALUES ('$value')"; $insert=mysql_query($links,$connection); } // Second code attempt $number=count($pagetype); for($a=0;$a<$number;$a++){ $links="INSERT INTO $username_links (links) VALUES ('$pagetype[$a]')"; $insert=mysql_query($links) or die(mysql_error()); //echo $pagetype[$a];//test } Thanks in advance...Robert -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Insert an array into MySQL
Your second method should work, but it's dependent on what your form looks like and how your table was created. You obviously have a key set for this column and you're trying to insert the same value twice, which makes an error. Show us what your form looks like, not the whole thing, just for the checkboxes. Also, what does your CREATE TABLE look like for the table your trying to insert this data into? ---John Holmes... > -Original Message- > From: Rob Packer [mailto:[EMAIL PROTECTED]] > Sent: Sunday, June 23, 2002 7:50 AM > To: [EMAIL PROTECTED] > Subject: [PHP] Insert an array into MySQL > > Hi, I've seen a few post for this same thing but I can't seem to get it > working. What I'm doing is taking a form with check boxes and putting them > into an array. Then I'm attempting to INSERT them into the db, one > checkbox > value per row. > > Okay, well I've tries 2 ways that I think should work and they only > insert > the first value. They both produce this error "Duplicate entry '0' for key > 1" (which I don't understand) Anyway here is the code I'm trying... > > (I'm not trying to use both blocks of code, just one or the other) > > // First code > while(list($key,$value)=each($pagetype)){ > //echo $value."";//test > $links="INSERT INTO $username_links (links) VALUES ('$value')"; > $insert=mysql_query($links,$connection); > > } > > // Second code attempt > $number=count($pagetype); > for($a=0;$a<$number;$a++){ > $links="INSERT INTO $username_links (links) VALUES ('$pagetype[$a]')"; > $insert=mysql_query($links) or die(mysql_error()); > //echo $pagetype[$a];//test > } > > > > > Thanks in advance...Robert > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Insert an array into MySQL
"John Holmes" <[EMAIL PROTECTED]> wrote in message 001201c21ab5$0df073a0$b402a8c0@mango">news:001201c21ab5$0df073a0$b402a8c0@mango... > Your second method should work, but it's dependent on what your form > looks like and how your table was created. You obviously have a key set > for this column and you're trying to insert the same value twice, which > makes an error. > > Show us what your form looks like, not the whole thing, just for the > checkboxes. Also, what does your CREATE TABLE look like for the table > your trying to insert this data into? > > ---John Holmes... > > > -Original Message- > > From: Rob Packer [mailto:[EMAIL PROTECTED]] > > Sent: Sunday, June 23, 2002 7:50 AM > > To: [EMAIL PROTECTED] > > Subject: [PHP] Insert an array into MySQL > > > > Hi, I've seen a few post for this same thing but I can't seem to get > it > > working. What I'm doing is taking a form with check boxes and putting > them > > into an array. Then I'm attempting to INSERT them into the db, one > > checkbox > > value per row. > > > > Okay, well I've tries 2 ways that I think should work and they only > > insert > > the first value. They both produce this error "Duplicate entry '0' for > key > > 1" (which I don't understand) Anyway here is the code I'm trying... > > > > (I'm not trying to use both blocks of code, just one or the other) > > > > // First code > > while(list($key,$value)=each($pagetype)){ > > //echo $value."";//test > > $links="INSERT INTO $username_links (links) VALUES ('$value')"; > > $insert=mysql_query($links,$connection); > > > > } > > > > // Second code attempt > > $number=count($pagetype); > > for($a=0;$a<$number;$a++){ > > $links="INSERT INTO $username_links (links) VALUES ('$pagetype[$a]')"; > > $insert=mysql_query($links) or die(mysql_error()); > > //echo $pagetype[$a];//test > > } > > > > > > > > > > Thanks in advance...Robert > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Insert an array into MySQL
oops, sorry about the other post (I've got a baby that likes the keyboard)... Hi, This is the form, which I had doubts about, but it seemed to put things into the array correctly when I tested it by printing it out. I can't see how, it will print key=0 value=index.html key=1 value=about.html key=2 value=whats_new.html when I test it and then have a duplicate key. It's saying that I have 2 $pagetype[0] correct? Anyhow, here's the form... Home Page About Page What's New Page Contact Page Favorite Links The CREATE TABLE is this: I define the table name from user input. $query_links="CREATE TABLE $username_links (id INT(4) not null, links VARCHAR(20) not null, UNIQUE (id))"; if(mysql_query($query_links,$connection)){ echo "Links table created!"; }else{ echo "no links table made"; } thanks, Rob "John Holmes" <[EMAIL PROTECTED]> wrote in message 001201c21ab5$0df073a0$b402a8c0@mango">news:001201c21ab5$0df073a0$b402a8c0@mango... > Your second method should work, but it's dependent on what your form > looks like and how your table was created. You obviously have a key set > for this column and you're trying to insert the same value twice, which > makes an error. > > Show us what your form looks like, not the whole thing, just for the > checkboxes. Also, what does your CREATE TABLE look like for the table > your trying to insert this data into? > > ---John Holmes... > > > -Original Message- > > From: Rob Packer [mailto:[EMAIL PROTECTED]] > > Sent: Sunday, June 23, 2002 7:50 AM > > To: [EMAIL PROTECTED] > > Subject: [PHP] Insert an array into MySQL > > > > Hi, I've seen a few post for this same thing but I can't seem to get > it > > working. What I'm doing is taking a form with check boxes and putting > them > > into an array. Then I'm attempting to INSERT them into the db, one > > checkbox > > value per row. > > > > Okay, well I've tries 2 ways that I think should work and they only > > insert > > the first value. They both produce this error "Duplicate entry '0' for > key > > 1" (which I don't understand) Anyway here is the code I'm trying... > > > > (I'm not trying to use both blocks of code, just one or the other) > > > > // First code > > while(list($key,$value)=each($pagetype)){ > > //echo $value."";//test > > $links="INSERT INTO $username_links (links) VALUES ('$value')"; > > $insert=mysql_query($links,$connection); > > > > } > > > > // Second code attempt > > $number=count($pagetype); > > for($a=0;$a<$number;$a++){ > > $links="INSERT INTO $username_links (links) VALUES ('$pagetype[$a]')"; > > $insert=mysql_query($links) or die(mysql_error()); > > //echo $pagetype[$a];//test > > } > > > > > > > > > > Thanks in advance...Robert > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Insert an array into MySQL
Nevermind, I got it. I had Unique for a field which made it mad. "Rob Packer" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > oops, sorry about the other post (I've got a baby that likes the > keyboard)... > > Hi, > > This is the form, which I had doubts about, but it seemed to put things into > the array correctly when I tested it by printing it out. > I can't see how, it will print > key=0 > value=index.html > key=1 > value=about.html > key=2 > value=whats_new.html > when I test it and then have a duplicate key. It's saying that I have 2 > $pagetype[0] correct? Anyhow, here's the form... > > Home Page > About Page > What's New > Page > Contact Page > Favorite > Links > > > The CREATE TABLE is this: > I define the table name from user input. > > $query_links="CREATE TABLE $username_links (id INT(4) not null, links > VARCHAR(20) not null, UNIQUE (id))"; > if(mysql_query($query_links,$connection)){ > echo "Links table created!"; > }else{ > echo "no links table made"; > } > > > > > thanks, > Rob > > > > > "John Holmes" <[EMAIL PROTECTED]> wrote in message > 001201c21ab5$0df073a0$b402a8c0@mango">news:001201c21ab5$0df073a0$b402a8c0@mango... > > Your second method should work, but it's dependent on what your form > > looks like and how your table was created. You obviously have a key set > > for this column and you're trying to insert the same value twice, which > > makes an error. > > > > Show us what your form looks like, not the whole thing, just for the > > checkboxes. Also, what does your CREATE TABLE look like for the table > > your trying to insert this data into? > > > > ---John Holmes... > > > > > -Original Message- > > > From: Rob Packer [mailto:[EMAIL PROTECTED]] > > > Sent: Sunday, June 23, 2002 7:50 AM > > > To: [EMAIL PROTECTED] > > > Subject: [PHP] Insert an array into MySQL > > > > > > Hi, I've seen a few post for this same thing but I can't seem to get > > it > > > working. What I'm doing is taking a form with check boxes and putting > > them > > > into an array. Then I'm attempting to INSERT them into the db, one > > > checkbox > > > value per row. > > > > > > Okay, well I've tries 2 ways that I think should work and they only > > > insert > > > the first value. They both produce this error "Duplicate entry '0' for > > key > > > 1" (which I don't understand) Anyway here is the code I'm trying... > > > > > > (I'm not trying to use both blocks of code, just one or the other) > > > > > > // First code > > > while(list($key,$value)=each($pagetype)){ > > > //echo $value."";//test > > > $links="INSERT INTO $username_links (links) VALUES ('$value')"; > > > $insert=mysql_query($links,$connection); > > > > > > } > > > > > > // Second code attempt > > > $number=count($pagetype); > > > for($a=0;$a<$number;$a++){ > > > $links="INSERT INTO $username_links (links) VALUES ('$pagetype[$a]')"; > > > $insert=mysql_query($links) or die(mysql_error()); > > > //echo $pagetype[$a];//test > > > } > > > > > > > > > > > > > > > Thanks in advance...Robert > > > > > > > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php