Hello,

I was wondering if anyone could help as this has been puzzling me. I have a form with a few checkboxes on it and i need to store them in a database table taking on the following structure:

user_id / c_id
user_id being the users profile id and c_id being the category id from the checkbox on the form, so if i selected 2 checkboxes the stored information should be;

user_id / c_id
1 / 2
1 / 4
and so on.

I have tried a foreach but can't seam to get it functioning correctly, i have included my code below;

<?php

include('global.php');
dbConn();

if(isset($_POST['team']))
{
	foreach($_POST['team'] as $value){
		$insert=mysql_query("INSERT INTO mod_mem_cuser('user_id, c_id') VALUES ('1','$value')");
	}
}

?>

<html>
<body>
<form method="post" action="add.php">
<input type="checkbox" name="team[1]" value="1"> Yoga <br />
<input type="checkbox" name="team[2]" value="2"> Blag <br />
<input type="checkbox" name="team[3]" value="3"> Shreedies <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Thanks!
Zack.

Recommended Answers

try to echo your $_post before the line6 like -

echo '<pre>';
print_r($_POST);
echo '</pre>';

It may help you, because 'team' is array also

Jump to Post

The problem appears to be your query:

$insert=mysql_query("INSERT INTO mod_mem_cuser('user_id, c_id') VALUES ('1','$value')");
//should be:
$insert=mysql_query("INSERT INTO mod_mem_cuser(user_id, c_id) VALUES ('1','$value')");
// notice I removed the single quotes
Jump to Post

All 6 Replies

try to echo your $_post before the line6 like -

echo '<pre>';
print_r($_POST);
echo '</pre>';

It may help you, because 'team' is array also

Hi,

That gives me this:

Array
(
[team] => Array
(
[1] => 1
[2] => 2
)

[submit] => submit
)
Yoga
Blag
Shreedies

and echo's the result when i click submit. But how can I now store this in the database as it's not storing at present.

Thanks!
Zack

The problem appears to be your query:

$insert=mysql_query("INSERT INTO mod_mem_cuser('user_id, c_id') VALUES ('1','$value')");
//should be:
$insert=mysql_query("INSERT INTO mod_mem_cuser(user_id, c_id) VALUES ('1','$value')");
// notice I removed the single quotes

Sorry, duplicate post.

I dont find anything wrong in above code, i got it fine for

foreach($_POST['team'] as $value){
	echo "=>".$value;
		//$insert=mysql_query("INSERT INTO mod_mem_cuser('user_id, c_id') VALUES ('1','$value')");

So if you check two checkboxes you will fire two queries after the form submit and so on.
But why you would like to do this, i cant understand.
Because for each user(which you have hardcoded here as '1' , you are going to have to seperate rows..instead try having coulmns..or see below posts - )http://www.daniweb.com/forums/thread138801.html

I dont find anything wrong in above code, i got it fine for

foreach($_POST['team'] as $value){
	echo "=>".$value;
		//$insert=mysql_query("INSERT INTO mod_mem_cuser('user_id, c_id') VALUES ('1','$value')");

So if you check two checkboxes you will fire two queries after the form submit and so on.
But why you would like to do this, i cant understand.
Because for each user(which you have hardcoded here as '1' , you are going to have to seperate rows..instead try having coulmns..or see below posts - )http://www.daniweb.com/forums/thread138801.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.