Every question have 3 answers with the score of 0, 1, or 2 assigned in the database.
How can i save each answer with the question id and answer id with the name and the email?
It's about 50 questions in the questions row in the database that is displaying with this script:
<?php
include "config.php";
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Testar uppkoppling:
if (mysqli_connect_errno()){
// Databaskoppling error
exit("Couldn't connect to the database: ".mysqli_connect_error());
}
$result = mysqli_query($db,"SELECT * FROM que");
echo '<form action="taemot.php" method="post" id="MyForm">';
while($row = mysqli_fetch_array($result)) {
echo '' . $row['que_question'] . '
<input type="radio" name="' . $row['que_answer0'] . '" value="' . $row['que_answer0'] . '">
<input type="radio" name="' . $row['que_answer1'] . '" value="' . $row['que_answer1'] . '">
<input type="radio" name="' . $row['que_answer2'] . '" value="' . $row['que_answer2'] . '"><br>';
}
echo ' <input type="text" name="name" value="Namn"><br>
<input type="text" name = "email" value="Epostadress"><br>
<input type="submit" value="Submit"> </form>';
?>
taemot.php
<?
$name=$_POST['name'];
$email=$_POST['email'];
mysql_connect("XXXX", "XXXX", "XXXX")
or die(mysql_error()); mysql_select_db("database") or die(mysql_error());
mysql_query("INSERT INTO `data` VALUES ('$name', '$email')");
Print "Your information has been successfully added to the database."; ?>
Try have a input name as Array
// Assign $i to define which question number is on
<input type="radio" name="ans[$i]" value="'. $row['que_answer0'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer1'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer2'] .'">
$i++;
for taemont.php
$aryAns = $_POST['ans'];
foreach( $aryAns AS $key => $value ) { //$key is the question number, $value is the question answers
mysql_query("INSERT INTO data ....");
}
With this solutions $key row in database posts the value $i instead of question id?
How can this solution work with multiple questions, since there is about 50 questions when:
echo '' . $row['que_question'] . '
Right now only 1 value is clickable of 50 questions.
See this image to understand: http://s29.postimg.org/d4llvjajr/questions.jpg
$result = mysqli_query($db,"SELECT * FROM que");
echo '<form action="taemot.php" method="post" id="MyForm">';
while($row = mysqli_fetch_array($result)) {
echo '' . $row['que_question'] . '
<input type="radio" name="ans[$i]" value="'. $row['que_answer0'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer1'] .'">
<input type="radio" name="ans[$i]" value="'. $row['que_answer2'] .'">
';$i++;
}
echo ' <input type="text" name="name" value="Namn"><br>
<input type="text" name = "email" value="Epostadress"><br>
<input type="submit" value="Submit"> </form>';
taemot.php:
$aryAns = $_POST['ans'];
foreach( $aryAns AS $key => $value ) { //$key is the question number, $value is the question answers
mysql_query("INSERT INTO `data` VALUES ('$name', '$email', '$key', '$value')");
}
Print "Your information has been successfully added to the database."; ?>
Related
I have two little bits of code doing the work here:
HTML:
<form action="http://mypage.php">
<input type="text" name="AnswerArea" ID="AnswerArea" value="' . $this_answer . '" />
<input type="hidden" name="Q" value="' . $NextQ . '" />
<input type="hidden" name="P" value="' . $quest . '" />
<input type="hidden" name="ref" value="' . $ref . '" /><br><br>
<input class="button" name="submit" type="submit" value="Next" />
</form>
And the PHP:
<?php if ($AStyle != 'None'){
if(array_key_exists('AnswerArea', $_GET)) {
include 'connection.php';
$newanswer = $_GET['AnswerArea'];
$newSQL = 'UPDATE answers SET Q' . $prev . '="' . $newanswer . '" WHERE strRef="' . $ref . '"';
if (mysqli_query($mysqli, $newSQL)) {
echo 'Successful, comment out when ready';
}
mysqli_close($mysqli);
}
}
?>
The intention is that when submit is clicked the Q, P & ref values are in the URL and the new value of $this_answer is written to the database. However, the input submit and AnswerArea variables are appearing in there as well. I know you can't have GET and POST in the same form, so how should I go about doing this?
The AnswerArea variable could be longer than the permissible URL length so I want to avoid it getting passed as an address.
Any help gratefully received!
I am attempting to create a dynamic survey, in the sense that the user/admin can create the questions which will appear when the survey is answered from the application instead of hard-coding.
My code so far:
<div class="surveybox">
<ol>
<?php
$result = mysqli_query($db, "SELECT QuestionGroup FROM surveyquestions");
$unique = array();
while ($row = $result->fetch_assoc()) {
$header = $row["QuestionGroup"];
$unique[] = $header;
}
foreach (array_unique($unique) as $name) {
$questionsget = mysqli_query($db, "SELECT * FROM surveyquestions WHERE QuestionGroup = '$name'") or die(mysql_error());
echo('<form action="GET" method="storesurveyresponse.php">');
echo '<div class="questionheader"><h2>' . $name . '</h2></div><br>';
while ($row = $questionsget->fetch_assoc()) {
$id = $row["QuestionID"];
$questiongroup = $row["QuestionGroup"];
$questiontext = $row["Question"];
$responsefield = $row["ResponseField"];
$questionID = $row["QuestionID"];
echo '<div class="questionbody"><li>' . $questiontext . '</li><br><br>';
if ($responsefield == "Radio Button") {
echo '<input type="radio" name="' . $questionID . '" value="Excellent">Excellent<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Good<br>
<input type="radio" name="radio' . $questionID . '" value="Average">Average<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Below Average<br>
<input type="radio" name="radio' . $questionID . '" value="Poor">Poor ';
$answer = $_GET["radio".$questionID]; //filter_input(INPUT_POST, 'radio' . $questionID);
} elseif ($responsefield == "Comment Box") {
echo '<textarea class="questionarea" name="question" placeholder="Enter Answer here..."></textarea></div>';
}
echo '<div class="responsebutton"> <button class="submit"><a class="button" href="storesurveyresponse.php?ownerID=0&questionID=' . $questionID . '&questiontext=' . $questiontext . '&questiongroup=' . $questiongroup . '&responsefield=' . $responsefield . '&answer=' . $answer . '"=>Submit</a></button></div><br><br><br><br></div>';
echo'</form>';
}
}
?>
</ol>
</div>
The code above works perfectly when laying out the questions from the database and inserting all the other required database row values on submit, except for the selected radio button value. The radio button value column is always blank.
I also get the following error on page load "Notice: Undefined index: radio38 in C:\xampp\htdocs....on line 52" under all the questions that load, but the errors disappear after I submit a reply on one of the questions, I don't know if they are related.
As you can see I use the $_GET method to submit the values and my processing code is as follows:
<?php
include 'session.php';
$ownerID = filter_input(INPUT_GET, ownerID);
$questionsID = filter_input(INPUT_GET, questionID);
$questionsText = filter_input(INPUT_GET, questiontext);
$questionsgroup = filter_input(INPUT_GET, questiongroup);
$replyfield = filter_input(INPUT_GET, responsefield);
$answer = filter_input(INPUT_GET, answer);
$date = date("Y/m/d");
mysqli_query($db, "INSERT INTO surveyanswers
(OwnerID, UserID,ParticipantName, DateStart,
DateEnd, QuestionID, QuestionText, QuestionGroup,
ResponseField, Answer)
VALUES ('$ownerID', 'aaa', 'aaa', '$date',
'$date','$questionsID', '$questionsText', '$questionsgroup',
'$replyfield','$answer')")or die(mysqli_error($db));
header("Location: takesurvey.php");
I have looked at related questions and searched on the net, I haven't been able to find any specific questions/examples dealing with this exact problem.
I made a few alterations and finally got it to work based on your pointers and suggestions. I added the get method to form declaration and put the processing code on the same page as the form.
Here's the code:
foreach (array_unique($unique) as $name) {
$questionsget = mysqli_query($db, "SELECT * FROM surveyquestions WHERE QuestionGroup = '$name'") or die(mysql_error());
echo '<div class="questionheader"><h2>' . $name . '</h2></div><br>';
while ($row = $questionsget->fetch_assoc()) {
$id = $row["QuestionID"];
$questiongroup = $row["QuestionGroup"];
$questiontext = $row["Question"];
$responsefield = $row["ResponseField"];
$questionID = $row["QuestionID"];
echo '<form action="" method="get">';
echo '<div class="questionbody"><li>' . $questiontext . '</li><br><br>';
if ($responsefield == "Radio Button") {
echo '<input type="radio" name="radio'. $questionID . '" value="Excellent">Excellent<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Good<br>
<input type="radio" name="radio' . $questionID . '" value="Average">Average<br>
<input type="radio" name="radio' . $questionID . '" value="Good">Below Average<br>
<input type="radio" name="radio' . $questionID . '" value="Poor">Poor ';
$answer = "radio" . $questionID; //filter_input(INPUT_POST, 'radio' . $questionID);
echo '<input type="submit" name="submit" value="Submit">';
if (isset($_GET['submit'])) {
if (isset($_GET['radio' . $questionID])) {
$answer = filter_input(INPUT_GET, 'radio' . $questionID);
$ownerID = "aaa";
$questionsID = $questionID;
$questionsText = $questiontext;
$questionsgroup = $questiongroup;
$replyfield = $responsefield;
$date = date("Y/m/d");
mysqli_query($db, "INSERT INTO surveyanswers(OwnerID, UserID, ParticipantName, DateStart, DateEnd, QuestionID, QuestionText, QuestionGroup, ResponseField, Answer) VALUES ('$ownerID', 'aaa', 'aaa', '$date', '$date', '$questionsID', '$questionsText', '$questionsgroup', '$replyfield', '$answer')")or die(mysqli_error($db));
}
}
} elseif ($responsefield == "Comment Box") {
echo '<textarea class="questionarea" name="question" placeholder="Enter Answer here..."></textarea></div>';
}
echo '</form>';
}
}
Thanks!
I have created this php code for a search in a mysql database. However, I have problems with the checkboxes part. Without the checkbox part it works fine, but with it says "no data found". The part for the checkboxes is called tarif-typ.
`
<?php
include "db_connect.inc.php";
$sql = "SELECT * FROM praemien";
$sql .= " where kanton like '" . $_POST["kanton"] . "' and franchise = ". $_POST["franchise"] ." and ";
switch($_POST["unfall"])
{ case 1:
$sql .="unfalleinschluss like 'OHN-UNF'";
break;
case 2:
$sql .="unfalleinschluss like 'MIT-UNF'";
}
$sql .=" and tarif-typ like '" . $_POST["tb"] . "' ";
$sql .= " order by praemie";
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Keine Datensätze gefunden";
while ($dsatz = mysqli_fetch_assoc($res))
echo $dsatz["versicherungsnamen"] . ", "
.$dsatz["kanton"] . ", "
.$dsatz["tarif-typ"] . ", "
.$dsatz["unfalleinschluss"] . ","
. $dsatz["praemie"] . "<br />";
mysqli_close($con);
?>
</body>
`
and here is my html form
`
<html>
<body>
<form action ="db_eingabe.php" method="post">
<p><input name="kanton" /> Kanton</p>
<p><input name="franchise" /> Franchise</p>
<p><input type="radio" name="unfall" value="1" checked="checked" />Unfall nein<br>
<input type="radio" name="unfall" value="2" />Unfall ja</p>
<br><p>
<b>Tarif</b>
</p>
<p><input type="checkbox" name="tb1" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="checkbox" name="tb2" value="TAR-HMO" />HMO</p>
<p><input type="checkbox" name="tb3" value="TAR-HAM" />HAM</p>
<p><input type="checkbox" name="tb4" value="TAR-DIV" />andere</p>
<p><input type="submit" />
<input type ="reset" /></p>
</form>
</body>
</html>
`
starting another post to keep things clean...
your PHP code:
<?php
include "db_connect.inc.php";
$sql = "
SELECT
*
FROM
`praemien`
WHERE
`kanton` LIKE '" . $_POST["kanton"] . "'
AND `franchise` = '". $_POST["franchise"] ."'
AND `unfalleinschluss` LIKE '" . $_POST["unfall"] . "'";
$tbs = array();
foreach( array( 'tb1', 'tb2', 'tb3', 'tb4' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`tarif-typ` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$sql .= " ORDER BY praemie";
echo $sql;
$res = mysqli_query($con, $sql) or die( mysql_error() );
$num = mysqli_num_rows($res);
if ($num==0) echo "Keine Datensätze gefunden";
while ($dsatz = mysqli_fetch_assoc($res)) {
echo $dsatz["versicherungsnamen"] . ", "
.$dsatz["kanton"] . ", "
.$dsatz["tarif-typ"] . ", "
.$dsatz["unfalleinschluss"] . ","
. $dsatz["praemie"] . "<br />";
}
mysqli_close($con);
?>
and your HTML code:
<html>
<body>
<form action ="db_eingabe.php" method="post">
<p><input name="kanton" /> Kanton</p>
<p><input name="franchise" /> Franchise</p>
<p><input type="radio" name="unfall" value="OHN-UNF" checked="checked" />Unfall nein<br>
<input type="radio" name="unfall" value="MIT-UNF" />Unfall ja</p>
<p>
<b>Tarif</b>
</p>
<p><input type="checkbox" name="tb1" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="checkbox" name="tb2" value="TAR-HMO" />HMO</p>
<p><input type="checkbox" name="tb3" value="TAR-HAM" />HAM</p>
<p><input type="checkbox" name="tb4" value="TAR-DIV" />andere</p>
<p><input type="submit" />
<input type ="reset" /></p>
</form>
</body>
</html>
NOTES: fixed franchise = '". $_POST["franchise"] ."' -- it did not have single quotes
changed the unfall radio group to have specific values to avoid the switch you had
lastly, if the tarif-typ and unfalleinschluss columns only contain specific strings you have shown, you do not need LIKE you can use '=', however, if you want to find the strings IN the values, i suggest LIKE '%search_string%' with the % wildcards.
I think your checkboxes should be radios with all the same name (tb) because there is no field named "tb" submitted in your form and that is the reason why your query fails.
<p><input type="radio" name="tb" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="radio" name="tb" value="TAR-HMO" />HMO</p>
<p><input type="radio" name="tb" value="TAR-HAM" />HAM</p>
<p><input type="radio" name="tb" value="TAR-DIV" />andere</p>
So me and my friend came to a conclusion that it's the $_email variable that screws everything up. As long as it's hard coded in, it works. But as soon as it's left as a $_email everywhere, it doesn't. The message goes through as "updated" but it doesn't update.
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="text" name="firstName" id="firstName" value="' . $_row['firstName'] . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_row['lastName'] . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_row['email'] . '" /><br />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
//$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
/*echo '<form class="update" method="post" action="MT_vjones_updateRecord.php">';
echo '<input type="text" name="firstName" id="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_email . '" /><br />';
echo '</form>';*/
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="hidden" name="firstName" value="' . $_firstName . '" />';
echo '<input type="hidden" name="lastName" value="' . $_lastName . '" />';
echo '<input type="hidden" name="email" value="*testBACK2FUN#test.com*" />';
}
echo '<p><< Back to the Admin Page</p>';
As you can see, we put in the email address in there for testing purposes...
check the id matches what you are intending to update.
To be sure print the $_id and $_email prior to the update and after.
#user710502: You don't need to segregate quotes with double-quotes in PHP. It reads it anyway, the only time you might bother is if you are reading from an array
eg:
"UPATE midterm SET email='".$POST['email']."'"
$_query = "UPDATE midterm " .
"SET email = '$_email' WHERE id = '$_id'" ;
should be
$_query = "UPDATE midterm " .
"SET email = $_email".
"WHERE id = $_id " ;
Reason is you are using $_ before variable which is not a valid variable declaration.
Because $_ is reserved for SUPER GLOBAL in php (i.e $_SESSION,$_SERVER,$_POST,$_GET,$_COOKIE etc).
if its not a issue for you then you need to concat your variable as below.
$_query = "UPDATE midterm SET email = '".$_email."' WHERE id = '".$_id."'" ;
SOLVED! Form issues.
SHOULD BE:
<?php
require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="hidden" name="firstName" id="firstName" value="' . $_row['firstName'] . '" />';
echo '<input type="hidden" name="lastName" id="lastName" value="' . $_row['lastName'] . '" />';
echo '<input type="hidden" name="email" id="email" value="' . $_row['email'] . '" />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="text" name="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" value="' . $_email . '" />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
}
echo '<p><< Back to the Admin Page</p>';
?>
Here's my code, not sure what I'm doing wrong here... First I check for the passed variable and add the bit about how to handle the form...
// Check for a user id:
if (empty($_POST['user_id'])) {
$errors[] = 'Oops! You forgot to enter your users.';
} else {
$user_id = mysqli_real_escape_string($dbc, trim($_POST['user_id']));
}
if (empty($errors)) { // If everything's good.
// Add the user to the database:
$q = "INSERT INTO users (user_id, page_id, account_id ) VALUES ('$user_id', '$page_id', '$id' )";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
//CLOSE UP THE SUBMIT CONDITIONAL
Then I select the users and echo the form
echo '<form action="addusers.php" method="post">';
while ($row = mysqli_fetch_array($show, MYSQLI_ASSOC)) {
echo '<p><label for="learner">Add</label>
<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id" /></p>';
}
echo '<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="account_id" value="$id" />
<input type="hidden" name="course_id" value="$course_id" />
</form>';
This creates a list of my users, and allows me to select as many as I like, but when I click to submit, it only enters the last user into the database, not all of them?
// UPDATE
Okay- I've edited the code so that the value of the checkbox is an array and know I need to use the foreach loop to pass the variables into the database, but could someone check this code for that insert?
// Add the user to the database:
$q = "INSERT INTO users (user_id) VALUES ('$user_id')";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
foreach($_POST as $key => $value){
if ($r->execute(Array($key, $value))){
echo "user added <br />";
}
} // end for each loop
Use:
<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id[]" />
(note the [] after user_id in the "name" attribute).
That will cause $_POST['user_id'] to be an array of values, one for each checked checkbox.
try this replace
<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id" /></p>';
by
<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id[]" /></p>';
// try to make your user_id checkbox as array like user_id[]