943,568 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 9998
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
May 27th, 2009
0

Re: How to insert checkbox data into mysql?

Happy to hear that ur problem was partially solved
Try this
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
May 27th, 2009
0

Re: How to insert checkbox data into mysql?

hi

you can do this in the loop like
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 15
Junior Poster in Training
Tulsa is offline Offline
77 posts
since May 2009
May 27th, 2009
0

Re: How to insert checkbox data into mysql?

you can use foreach loop for it. If you just required values then the
PHP Syntax (Toggle Plain Text)
  1. foreach($array as $val)
is enough or if you want to use index of array as well you can use
PHP Syntax (Toggle Plain Text)
  1. foreach($array as $key=>$val)
Reputation Points: 16
Solved Threads: 48
Posting Whiz
BzzBee is offline Offline
327 posts
since Apr 2009
May 27th, 2009
0

Re: How to insert checkbox data into mysql?

Happy to hear that ur problem was partially solved
Try this
PHP Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 28th, 2009
0

Re: How to insert checkbox data into mysql?

Click to Expand / Collapse  Quote originally posted by Tulsa ...
hi

you can do this in the loop like
PHP Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 28th, 2009
0

Re: How to insert checkbox data into mysql?

hi

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

Thanks
Reputation Points: 13
Solved Threads: 15
Junior Poster in Training
Tulsa is offline Offline
77 posts
since May 2009
May 29th, 2009
0

Re: How to insert checkbox data into mysql?

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!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009
May 29th, 2009
0

Re: How to insert checkbox data into mysql?

try this.
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
May 29th, 2009
0

Re: How to insert checkbox data into mysql?

hi

you have test the value of list box by the echo right ?
so now problem only insert query ok.
PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 13
Solved Threads: 15
Junior Poster in Training
Tulsa is offline Offline
77 posts
since May 2009
May 29th, 2009
0

Re: How to insert checkbox data into mysql?

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
shena is offline Offline
57 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: access database from php program
Next Thread in PHP Forum Timeline: delete or clear array??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC