| | |
How to insert checkbox data into mysql?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Happy to hear that ur problem was partially solved
Try this
otherwise u may need to use some other variable.
Try this
PHP Syntax (Toggle Plain Text)
$insertSQL = sprintf("INSERT INTO $table (id,name) VALUES (%d, %s)", $ch[0], $var); $Result = mysql_query($insertSQL) or die(mysql_error());
otherwise u may need to use some other variable.
hi
you can do this in the loop like
if you want to insert selected value in to diffrent row then you can do like this
Thanks
you can do this in the loop like
PHP Syntax (Toggle Plain Text)
foreach($array as $val) { echo $val; $query="insert into table (filed) value ($val)"; $result=mysql_query($query); }
Thanks
•
•
Join Date: Apr 2009
Posts: 257
Reputation:
Solved Threads: 37
you can use foreach loop for it. If you just required values then the
is enough or if you want to use index of array as well you can use
PHP Syntax (Toggle Plain Text)
foreach($array as $val)
PHP Syntax (Toggle Plain Text)
foreach($array as $key=>$val)
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
Happy to hear that ur problem was partially solved
Try this
PHP Syntax (Toggle Plain Text)
$insertSQL = sprintf("INSERT INTO $table (id,name) VALUES (%d, %s)", $ch[0], $var); $Result = mysql_query($insertSQL) or die(mysql_error());
otherwise u may need to use some other variable.
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!
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
hi
you can do this in the loop like
if you want to insert selected value in to diffrent row then you can do like thisPHP Syntax (Toggle Plain Text)
foreach($array as $val) { echo $val; $query="insert into table (filed) value ($val)"; $result=mysql_query($query); }
Thanks
Thanks for the explanations on multiple values selection. How do i insert the selected values in one row under the same field?
thanks!
hi
you can insert multiple value in one row as comma separated value.
so now your selected option stored in $string variable as a comma separated ok
Thanks
you can insert multiple value in one row as comma separated value.
PHP Syntax (Toggle Plain Text)
$array=$_post['list_box']; $string=implode(',',$array);
Thanks
•
•
Join Date: May 2009
Posts: 13
Reputation:
Solved Threads: 0
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
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!
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!
try this.
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.
PHP Syntax (Toggle Plain Text)
$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.
hi
you have test the value of list box by the echo right ?
so now problem only insert query ok.
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
you have test the value of list box by the echo right ?
so now problem only insert query ok.
PHP Syntax (Toggle Plain Text)
$insert="INSERT INTO table (name) VALUES ('$value')";
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
![]() |
Similar Threads
- using session variables to insert data to MYSQL Database (PHP)
- How do I insert multiple checkbox data into MySQL (PHP)
- inserting checkbox values in mysql +PHP (PHP)
Other Threads in the PHP Forum
- Previous Thread: access database from php program
- Next Thread: delete or clear array??
Views: 3648 | Replies: 25
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cookies cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla js limit link login loop mail mediawiki menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube





