Looping INSERT from checkbox info

Looping INSERT from checkbox info

am 15.03.2006 02:21:34 von Aaron Reimann

I am trying to insert info if a checkbox is selected. The checkboxes
are created from this code (pulled from mysql):

##########
$ministry_info_query = "SELECT * FROM ministries WHERE inactive = 0
ORDER BY title"; // selecting all ministries that are active

$ministry_info_results = mysql_query($ministry_info_query, $mysql_link)
or die("Bad query: ".mysql_error());

// if the results are 0, the print this
if (mysql_num_rows($ministry_info_results) == "0") {
print "
No ministries are in the
database.
";

} else {

print "

\n";

while ($row = mysql_fetch_array($ministry_info_results)) {
print "\n";
echo "\t\n";
echo "\t\n";
print "\n";
// ending the loop (while)
}

print "
$row[title] type=\"checkbox\"
value=\"$row[id]\">
\n";

}

mysql_free_result($ministry_info_results);
##########

Once the form is submitted, I need the ones that are checked to insert
the info into the "comments" table....i am kind of new at this...this
is kind of what I am thinking the code needs to look like:

$sql = "INSERT INTO comments ($row[people_id], id_ministries) VALUES(";

for($i=0;$i // DON'T KNOW WHAT TO DO

}

Does that make sense?

thank you for any advise.

Re: Looping INSERT from checkbox info

am 15.03.2006 05:41:00 von Robert Stearns

Aaron Reimann wrote:
> I am trying to insert info if a checkbox is selected. The checkboxes
> are created from this code (pulled from mysql):
>
> ##########
> $ministry_info_query = "SELECT * FROM ministries WHERE inactive = 0
> ORDER BY title"; // selecting all ministries that are active
>
> $ministry_info_results = mysql_query($ministry_info_query, $mysql_link)
> or die("Bad query: ".mysql_error());
>
> // if the results are 0, the print this
> if (mysql_num_rows($ministry_info_results) == "0") {
> print "
No ministries are in the
> database.
";
>
> } else {
>
> print "

\n";
>
> while ($row = mysql_fetch_array($ministry_info_results)) {
> print "\n";
> echo "\t\n";
> echo "\t\n";
> print "\n";
> // ending the loop (while)
> }
>
> print "
$row[title] > type=\"checkbox\"
> value=\"$row[id]\">
\n";
>
> }
>
> mysql_free_result($ministry_info_results);
> ##########
>
> Once the form is submitted, I need the ones that are checked to insert
> the info into the "comments" table....i am kind of new at this...this
> is kind of what I am thinking the code needs to look like:
>
> $sql = "INSERT INTO comments ($row[people_id], id_ministries) VALUES(";
>
> for($i=0;$i > // DON'T KNOW WHAT TO DO
>
> }
>
> Does that make sense?
>
> thank you for any advise.
>
A little ddl would help here. What if the person already has a comment?
Does the new one replace the old one?

What does $row[reople_id] give you in the INSERT statement? At that
point the parens are supposed to contain column names from the table
comments.

Re: Looping INSERT from checkbox info

am 15.03.2006 14:26:14 von Aaron Reimann

This is used for my church. We call it Communication Cards. People
fill out the form (pen and paper) and write down info like:

fname1, lname1, fname2, lname2, address1, address2

and in the form there are actually checkboxes. if you want info about
a ministry you check the box...for instance, if you want info about
"High School" you fill out your info and check "High School." You can
check as many as you want.

We have 3 tables in the database, here are the fields in each:
people.id, people.fname1, people.fname1, etc
ministries.id, ministries.title
comments.id, comments.id_people, comments.id_ministries,
comments.comments

the 3rd table, comments, "joins" people and ministires and comments
together.

$row[reople_id] (typo), it should be $row[people_id] is the people.id

does that make sense?

Re: Looping INSERT from checkbox info

am 15.03.2006 17:04:15 von Aaron Reimann

This is kind of what I need (I think):

if (is_array($_POST['comments']['id'])):
foreach ($_POST['comments']['id'] as $id):
$insert = "INSERT INTO people (username, id_people,
id_ministry) VALUES ($_SESSION[valid_user], $people_id, $id";

$mysql_insert = mysql_query($insert, $mysql_link)
or die("Bad query: ".mysql_error());
endforeach;
endif;

Re: Looping INSERT from checkbox info

am 15.03.2006 19:54:11 von Robert Stearns

Aaron Reimann wrote:
> This is used for my church. We call it Communication Cards. People
> fill out the form (pen and paper) and write down info like:
>
> fname1, lname1, fname2, lname2, address1, address2
>
> and in the form there are actually checkboxes. if you want info about
> a ministry you check the box...for instance, if you want info about
> "High School" you fill out your info and check "High School." You can
> check as many as you want.
>
> We have 3 tables in the database, here are the fields in each:
> people.id, people.fname1, people.fname1, etc
> ministries.id, ministries.title
> comments.id, comments.id_people, comments.id_ministries,
> comments.comments
>
> the 3rd table, comments, "joins" people and ministires and comments
> together.
>
> $row[reople_id] (typo), it should be $row[people_id] is the people.id
>
> does that make sense?
>
What are fname2, lname2?

What does the screen contain?
Person id
ministry_id1 comment1
ministry_id2 comment2
ministry_id3 comment3
.
.
.

Re: Looping INSERT from checkbox info

am 15.03.2006 19:54:26 von Robert Stearns

Aaron Reimann wrote:

> This is kind of what I need (I think):
>
> if (is_array($_POST['comments']['id'])):
> foreach ($_POST['comments']['id'] as $id):
> $insert = "INSERT INTO people (username, id_people,
> id_ministry) VALUES ($_SESSION[valid_user], $people_id, $id";
>
> $mysql_insert = mysql_query($insert, $mysql_link)
> or die("Bad query: ".mysql_error());
> endforeach;
> endif;
>
That is much closer to what you want. Where is $people_id coming from at
this time? Why aren't you inserting/updating comments? Are all of your
id columns (from the previous post) auto incrementing?