How to insert checkbox data into mysql?

Thread Solved

Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #11
May 27th, 2009
Happy to hear that ur problem was partially solved
Try this
  1. $insertSQL = sprintf("INSERT INTO $table (id,name) VALUES (%d, %s)", $ch[0], $var);
  2.  
  3. $Result = mysql_query($insertSQL) or die(mysql_error());

otherwise u may need to use some other variable.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 77
Reputation: Tulsa is an unknown quantity at this point 
Solved Threads: 15
Tulsa's Avatar
Tulsa Tulsa is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #12
May 27th, 2009
hi

you can do this in the loop like
  1. foreach($array as $val)
  2. {
  3. echo $val;
  4. $query="insert into table (filed) value ($val)";
  5. $result=mysql_query($query);
  6. }
if you want to insert selected value in to diffrent row then you can do like this

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 257
Reputation: BzzBee is an unknown quantity at this point 
Solved Threads: 37
BzzBee BzzBee is offline Offline
Posting Whiz in Training

Re: How to insert checkbox data into mysql?

 
0
  #13
May 27th, 2009
you can use foreach loop for it. If you just required values then the
  1. foreach($array as $val)
is enough or if you want to use index of array as well you can use
  1. foreach($array as $key=>$val)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #14
May 27th, 2009
Originally Posted by danishbacker View Post
Happy to hear that ur problem was partially solved
Try this
  1. $insertSQL = sprintf("INSERT INTO $table (id,name) VALUES (%d, %s)", $ch[0], $var);
  2.  
  3. $Result = mysql_query($insertSQL) or die(mysql_error());

otherwise u may need to use some other variable.
Hi,
Thanks for helping. Actually, this is wat i got:
Notice: Undefined variable: var in C:\wamp\www\test\checkbox.php on line 44
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 '(id,name) VALUES (3, )' at line 1

Why its a SQL syntax error, due to different php version/style of coding is it? I created two fields for the table called table which are:
id(int)
name(varchar)

If i can insert the values into database, then my problem is considered solved. Hope for kind reply. Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #15
May 28th, 2009
Originally Posted by Tulsa View Post
hi

you can do this in the loop like
  1. foreach($array as $val)
  2. {
  3. echo $val;
  4. $query="insert into table (filed) value ($val)";
  5. $result=mysql_query($query);
  6. }
if you want to insert selected value in to diffrent row then you can do like this

Thanks
Hi Tulsa,

Thanks for the explanations on multiple values selection. How do i insert the selected values in one row under the same field?

thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 77
Reputation: Tulsa is an unknown quantity at this point 
Solved Threads: 15
Tulsa's Avatar
Tulsa Tulsa is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #16
May 28th, 2009
hi

you can insert multiple value in one row as comma separated value.
  1. $array=$_post['list_box'];
  2. $string=implode(',',$array);
so now your selected option stored in $string variable as a comma separated ok

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #17
May 29th, 2009
Hi,

Could anyone tell me where did i go wrong with this code, i managed to connect the php with sql server but i couldnt insert the values into database. I get this error:

INSERT INTO table (name) VALUES ('FT')Error: 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 'table (name) VALUES ('FT')' at line 1

<form action="write.php" method="post">

PRODUCT TYPE:
<input type="checkbox" value="FT" name="tile[]">:FLOOR TILES
<input type="checkbox" value="HT" name="tile[]">HOMOGENEOUS TILES
<input type="checkbox" value="WT" name="tile[]">:WALL TILES
<input type="checkbox" value="PT" name="tile[]">:POLISHED TILES
<br>

<input name="confirm" type=submit id="confirm" style='border:1px solid #000000;font-family:Verdana, Arial, Helvetica, sans-serif ;font-size:9px;' value='Confirm'>

</form>

This is write.php

<?php

$con = mysql_connect("localhost","root");

if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("test_venus", $con);

if(isset($_POST['tile']))
{      
	foreach($_POST['tile'] as $value) 
	{         
 		$insert="INSERT INTO table (name) VALUES ('$value')";     
 		mysql_query($insert);    	
		echo $insert;
 	}
}

if (!mysql_query($insert, $con))
{
  die('Error: ' . mysql_error());
}

mysql_close($con) 

?>

I tried this for a long time for an answer to this problem, and I couldn't find anything that really helped. I feel that there is no wrong in my codings, but yet i couldnt solve it or may be i dont get to realise. So, any guidance would be gratefully recieved.
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #18
May 29th, 2009
try this.
  1. $insert="INSERT INTO `table` (name) VALUES ('$value')";


U have given "table" as table name and "name" as field name.
Both have special meaning in sql. So if u are using it u need to add `` like
eg :-`$tableName`

Better try to avoid using these names. Also i recommend you to use SQLyog.
The community version is free. You can get it from http://www.webyog.com

It is better first try ur sql statements in SQLyog. This will help u t o avoid these type of mistakes.
Last edited by danishbacker; May 29th, 2009 at 1:07 am.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 77
Reputation: Tulsa is an unknown quantity at this point 
Solved Threads: 15
Tulsa's Avatar
Tulsa Tulsa is offline Offline
Junior Poster in Training

Re: How to insert checkbox data into mysql?

 
0
  #19
May 29th, 2009
hi

you have test the value of list box by the echo right ?
so now problem only insert query ok.
  1. $insert="INSERT INTO table (name) VALUES ('$value')";
table is the table name right.
so as per my view mysql not allow to use this reserved key word use to us.

so you need to use table name diffrent rather than 'table' ok
i hope this will solve your problem

Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 13
Reputation: shena is an unknown quantity at this point 
Solved Threads: 0
shena shena is offline Offline
Newbie Poster

Re: How to insert checkbox data into mysql?

 
0
  #20
May 29th, 2009
Hi danish,
Thanks a lot for the answer. It did work. But, i juz want to clarify that what i did (insertion method - without `` for the table name), did work for other scripts, why cant i do the same for this script?
Is this an alternate way?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 3648 | Replies: 25
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC