how to save all dynamic checkboxes value in one table.(mysq)

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
phpkets
Forum Newbie
Posts: 2
Joined: Fri Apr 20, 2007 6:56 am

how to save all dynamic checkboxes value in one table.(mysq)

Post by phpkets »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi everyone,
i have no. of dynamic checboxes in my form.
now not able to figure out in this code that how can i save all checkbox values in one table.

[b]my DB table's format is :[/b]

[syntax="sql"]mas_id int(11) NOT NULL auto_increment,
name varchar(20) NOT NULL default '',
que_id text NOT NULL,
php code looks like :[/syntax]

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$que_id = '';
foreach($_POST["que_id"] AS $key => $value){
$insertSQL = sprintf("INSERT INTO ts_masterset (que_id, name) VALUES (%s, %s)",
GetSQLValueString($_POST['que_id'], "text"),
GetSQLValueString($_POST['name'], "text"));}
and my html code looks like, where i am generating all questions :

Code: Select all

<?php $questionsInclude = mysql_query("SELECT * FROM ts_questions WHERE setid='".$row_tsset['id']."'");
while($aquestionsInclude = mysql_fetch_array($questionsInclude)){
$qd = $aquestionsInclude['id'];
echo "<input name='que_id[]' type='checkbox' value='$qd'>";
echo $aquestionsInclude['question'];
echo "<br>"; }?>
but with this code i am getting "Array" as a value in database.

anyidea, where i am doing wrong !
thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

for starters i think the $questionsInclude = line should look something like this:

Code: Select all

$questionsInclude = mysql_query("SELECT * FROM ts_questions WHERE setid='$row_tsset['id']'");
Give that a try (changed select statement slightly).

Wayne
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

You SQL look OK

you have an array in the database because $_POST['que_id'] is an array

try this

Code: Select all

foreach($_POST["que_id"] AS $key => $value) {
    $insertSQL = sprintf("INSERT INTO ts_masterset (que_id, name) VALUES (%s, %s)",
    GetSQLValueString($value, "text"),
    GetSQLValueString($_POST['name'], "text"));
}
I don't know what function GetSQLValueString does but I assume it is just some security validation.
phpkets
Forum Newbie
Posts: 2
Joined: Fri Apr 20, 2007 6:56 am

Post by phpkets »

arturm wrote:You SQL look OK

you have an array in the database because $_POST['que_id'] is an array

try this

Code: Select all

foreach($_POST["que_id"] AS $key => $value) {
    $insertSQL = sprintf("INSERT INTO ts_masterset (que_id, name) VALUES (%s, %s)",
    GetSQLValueString($value, "text"),
    GetSQLValueString($_POST['name'], "text"));
}
thanks for your reply.

but adding just $value, its saving just last checkbox value not all.

any other solution for this ?
thanks.
Post Reply