I need to Insert data to the DB using the form given below
<form action="OtherEventPayment.php" id="frmSignIn" method="post">
<input type="hidden" name="online_id" value="<?php echo $lid; ?>" >
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Item</th>
<th>No. of Participants</th>
<th>Tick the Items</th>
</tr>
</thead>
<tbody>
<tbody>
<?php
$sn ="1";
$id = $oth_event_id;
$stmt1 = $DB_con->prepare('SELECT * FROM oth_events_details
LEFT JOIN oth_event_category ON (oth_events_details.oth_evcat_id=oth_event_category.oth_evcat_id)
WHERE oth_event_id =:uid ORDER BY oth_event_det_id DESC');
$stmt1->execute(array(':uid'=>$id));
$stmt1->execute();
if($stmt1->rowCount() > 0)
{
while($row1=$stmt1->fetch(PDO::FETCH_ASSOC))
{
extract($row1);
?>
<tr>
<td><?php echo $sn; ?></td>
<td>
<?php echo $row1['oth_category'];?> -
<?php
$group =$row1['oth_catgroup_type'];
if ($group=="S")
{
echo "Single";
}
elseif ($group=="D")
{
echo "Doubles";
}
else{
echo "Group";
}
?>
</td>
<td><?php echo $row1['participntno']; ?></td>
<td>
<b>
</b>
<input type="checkbox" name="chk[<?php echo $row1['oth_event_det_id'];?>]" value="<?php echo $row1['oth_event_det_id'];?>" id="chk[<?php echo $row1['oth_event_det_id'];?>]" />
Fees:- <?php echo $row1['oth_ev_fee'];?>
</td>
</tr>
<?php $sn++; ?>
<?php
}
}
else
{
?>
<div class="col-xs-12">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
?>
</tbody>
</table>
<div class="col-md-6">
<input type="submit" name="selectItems" value="Submit & Proceed" class="btn btn-primary pull-right mb-xl" data-loading-text="Loading...">
</div>
</div>
<?php echo $sn1=$sn-1; ?>
</form>
in the OtherEventPayment.php i have written the code. But not working . How to Insert data correctly to DB
<?php
require_once 'dbconfig.php';
if(isset($_POST['selectItems']))
{
echo array[] = $_POST['chk[]'];
echo $oth_online_id= $_POST['online_id'];
if($oth_event_detid != ""){
for($i=0;$i<sizeof($oth_event_detid);$i++)
{
// oth_event_det_id,oth_online_id
$stmt = $DB_con->prepare('INSERT INTO othevents_itemsonline(oth_event_det_id,oth_online_id) VALUES( :oth_event_det_id, :oth_online_id)');
$stmt->bindParam(':oth_event_det_id',$oth_event_det_id);
$stmt->bindParam(':oth_online_id',$oth_online_id);
if($stmt->execute())
{
$lastonlineid= $DB_con->lastInsertId();
$successMSG = "Thank you For Registering with us . Please select the items to be participating...";
// header("refresh:0;OtherEventsOnlineRegistrationThankyou.php"); /
}
else
{
$errMSG = "error while registering....";
} } }
}
?>
Name should be same for input field. Use following code:
<input type="checkbox" name="chk[]" value="<?php echo $row1['oth_event_det_id'];?>" id="chk[<?php echo $row1['oth_event_det_id'];?>]" />
Fees:- <?php echo $row1['oth_ev_fee'];?>
You can see name. Hopefully it will be clear enough
Just change the value of you checkboxes and the value it represents but keep the name same with the others, yet it should have a name with []
<input type="checkbox" id="chk<?php echo $row1['oth_event_det_id'];?>" name="chk[]" value="<?php echo $row1['oth_event_det_id'];?>">
<label for="chk<?php echo $row1['oth_event_det_id'];?>"><?php echo $row1['oth_event_det_id'];?></label>
having a name chk[] like this will send and serve as an array in your get or post-function so loop it on controller or function that will add it in the DB
upon inserting it,
$data = $_GET['chk']; //this is in array form
foreach($data as $chk){
//insert code here
}
Related
In a quiz app, I am taking user answer using a form. I am retrieving correct answer from database table. I want to compare the correct answer with the user's answer and count how many answer was right and how many answer was wrong.
Here is my form:
<form id="question" class="" action="quiz_ans.php" method="post">
<table id="quiz-question" align="center" class="row-border compact order-column stripe">
<input class="form-control" type="hidden" name="NumberofQuestions" id="NumberofQuestions" value="<?php echo $NumberofQuestions; ?>">
<thead>
<?php
if($QuizQuestions) {
$i=1;
foreach($QuizQuestions as $row):
?>
<tr>
<th><?php echo $i; ?>. <?php echo $row->Question; ?>
<br>
<?php if(isset($row->Screenshot)) { ?>
<img src="<?php echo htmlspecialchars($row->Screenshot); ?>" alt="test" height="300" width="980">
<?php } ?>
</th>
</tr>
</thead>
<tbody>
<?php if(isset($row->Option1)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="0"><?php echo $row->Option1; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option2)) { ?>
<tr class="info">
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="1"> <?php echo $row->Option2; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option3)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="2"> <?php echo $row->Option3; ?></td>
</tr>
<?php } ?>
<?php if(isset($row->Option4)) { ?>
<tr>
<td><input type="radio" name="AnswerId[<?php echo $row->Id; ?>]" value="3"><?php echo $row->Option4; ?></td>
</tr>
<?php } ?>
<tr>
<td><label for="AnswerReason">Why?</label><input class="form-control" type="text" name="AnswerReason[]" id="AnswerReason" value=""></td>
</tr>
<?php if(isset($row->Id)) { ?>
<tr>
<td><input class="form-control" type="hidden" name="QuestionId[]" id="QuestionId" value="<?php echo $row->Id; ?>"></td>
</tr>
<?php } ?>
</tbody>
<?php
$i++;
endforeach;
}
?>
</table>
<br>
<input type="submit" name="submit" value="Submit" class="btn btn-success">
</form>
I am getting the user answer from the form submit:
$NumberofQuestions = $_POST['NumberofQuestions'];
$ans = implode("", $_POST['AnswerId']);
I am retreiving the correct answer from the database table:
try {
$sql = "CALL spQuizAnswers(:quiz_num)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':quiz_num', $quiz_num, PDO::PARAM_STR, 50);
$stmt->execute();
$QuizAns=$stmt->fetchAll();
$stmt->closeCursor();
} catch (PDOException $e) {
die("Error occurred:" . $e->getMessage());
}
I am comparing the user's answer and the correct answer:
for ($i=0; $i<$NumberofQuestions; $i++) {
if($QuizAns) {
foreach($QuizAns as $row):
if($row->CorrectAns == $ans[$i]){
$right++;
} elseif($ans[$i] == 4){
$not_answered++;
} else {
$wrong++;
}
endforeach;
}
}
$CorrectAnswer = $right;
$WrongAnswer = $wrong;
$NotAnswered = $not_answered;
$TotalQuestion = $right+$wrong+$not_answered;
It does not give correct calculation. For 5 questions it gives $TotalQuestion=25.
How can I achieve the correct calculation? Any help would be much appreciated.
I have three Table their structure is added down there. i want to show all role on one screen. and when click on one any role it will take you to next screen where (all the permission shown in check boxes) permission that is given to that role checked and other unchecked. and i want to that dynamically, as it can be given and taken by using check boxes. there check boxes should contain role_id and perm_id so it can b use for crud operations.table has many to many relation.
i have the role_id bu using the isset($_GET['role_id']) metthod and perm_id by calling a function that will give me the perm_id
<?php if (isset($_GET['role_id'])) {
$role_id = $_GET['role_id'];
} ?>
user_permission.php page
<?php $row = $role->allPermission($role_id); if(!empty($row)){
?>
<table class="table table-bordered table-striped" id="datatable-editable">
<thead>
<tr>
<th><center>Add</center></th>
<th><center>Update</center></th>
<th><center>Delete</center></th>
<th><center>View</center></th>
</tr>
</thead>
<tbody>
<tr>
<?php $all_permissions = $permission->allPermissions();
while ($row=mysqli_fetch_assoc($all_permissions)) {
$perm_id = $row['perm_id'];
$check_permission = $permission->checkPermission($role_id,$perm_id);
while ($row=mysqli_fetch_assoc($check_permission)){
?>
<td><center><input type="checkbox" perm_id="<?php echo $row['perm_id']?>" value="<?php echo $row['permission']?>"> <?php echo $row['permission']?>
</center>
</td>
<?php
}} ?>
</tr>
<tr>
<?php foreach ($row as $val) {?>
<td>
<?php if ($val['permission'] == 'add' || $val['permission'] == 'update' || $val['permission'] == 'delete' || $val['permission'] == 'view'){ ?>
<input type="checkbox" checked="checked" value="<?php echo $val['id']?>">
<?php } else { ?> <input type="checkbox" value="<?php echo $val['id']?>" > <?php }?>
</td>
<?php } ?>
<!-- foreach ($value as $key => $val) {
// echo $key . ' => ' . $val." / ";
echo $key['prem'];
} -->
<!-- <td>
<?php if (in_array('add', $row)){ ?>
<input type="checkbox" checked="checked" value="<?php echo $id?>">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('update', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('delete', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td>
<td>
<?php if (in_array('view', $row)){ ?>
<input type="checkbox" checked="checked">
<?php } else { ?> <input type="checkbox" > <?php }?>
</td> -->
</tr>
</tbody>
</table>
allPermissions()
public function allPermissions()
{
$query="SELECT * from permission where status = 1";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
chechkPermission()
public function checkPermission($role_id,$perm_id)
{
$query = "select * from role_perm rp where role_id = '$role_id' and perm_id = '$perm_id' ";
$conn=$this->Connection();
$result = mysqli_query($conn,$query);
$num_rows = mysqli_num_rows($result);
if ($num_rows>0) {
return $result;
}
}
its been three(now) days i'm stuck here, please help. and sorry for my bad english
Table Structure
This is how i want it
you mean something like this? <input type="checkbox" <?= ($permission >= 3 ? "checked" : "") ?>
I have problem with my buttons. I will try to explain.
1)I have users db from postgresql and new db in ms sql.
2)Created site with 2 columns in table ("SELECT * from users"-postgresql ): They are id/user
3)Then added new column "Operator" which contain submit buttons and functionality of submit buttons is updating "access" column from ms sql db.
PROBLEM:
It prints all buttons for all data that I have in ms sql(I have 7 rows data in ms sql, it prints 7 buttons for each row), I need to "echo" 1 button for each row which will be changable. If access==1 it should be named Active , else it should be named Diactive.
Here is my code and picture of what I got:
<?php
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
<th>View</th>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
?>
<tr>
<td>
<?php
$id = $row["id"];
echo $id;
?>
</td>
<td>
<?php
$username = $row["username"];
echo $username;
?>
</td>
<td>
<form method="POST" action="oper.php">
<?php
include ("db.php");
$result2 = pg_query($db_connection, "SELECT * from users ORDER by id asc");
while ($row1 = pg_fetch_array($result2))
{
$iddrain= $row1['id'];
//echo $iddrain;
//echo $iddrain;
$q7= "Select access from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid= sqlsrv_query($link, $q7, $params, $options);
while($row7= sqlsrv_fetch_array($resultid))
{
//$rs7=$row7['ID_M'];
$rs8=$row7['access'];
//echo $rs8;
//break;
if($rs8==1)
{
echo "<p><input type=\"submit\" name=\"uid\" value=Operator-ON onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
else
{
echo "<p><input type=\"submit\" name=\"uid\" value=DIavtive onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
}
}
?>
</form>
</td>
<?php
}
?>
</tr>
</table>
?>
You are doing typo mistake na dnot giving quote for value attributes near :
try like this :
if($rs8==1)
{
echo '<p><input type="submit" name="uid" value="Operator-ON" onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value="'.$id.'" onchange=
"this.form.submit()"></p>';
}
else
{
echo '<p><input type="submit" name="uid" value="DIavtive" onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value="'.$id.' onchange="this.form.submit()"></p>';
}
}
I have reditted your code to remove some errors that were making it fail to work;
<?php include ("db.php"); ?>
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
<th>View</th>
</tr>
</thead>
<?php
//while ($row = pg_fetch_array($result)) {
<?php foreach( pg_fetch_array($result) as $row ) { ?>
?>
<tbody>
<tr>
<td<?php echo $row['id'] ?></td>
<td><?php echo $row['username'] ?></td>
<td>
<form method="POST" action="oper.php">
<?php
$result2 = pg_query($db_connection, 'SELECT * from users ORDER by id asc');
while ($row1 = pg_fetch_array($result2)) {
$iddrain = $row1['id'];
$q7 = "Select access from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid = sqlsrv_query($link, $q7, $params, $options);
while ($row7 = sqlsrv_fetch_array($resultid)) {
//$rs7=$row7['ID_M'];
$rs8 = $row7['access'];
if ($rs8 == 1) {
echo '<p><input type="submit" name="uid" value=Operator-ON onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value=$id onchange="this.form.submit()"></p>';
} else {
echo '<p><input type="submit" name="uid" value=DIavtive onchange="this.form.submit()"></p>
<p><input type="hidden" name="uid" value=$id onchange="this.form.submit()"></p>';
}
}
}
?>
</form>
</td>
</tr>
</tbody>
</table>
I`ve changed code and now it is working, problem was additional useless fetch. Here is the code:
<table class="table table-condensed">
<thead>
<tr>
<th>ID</th>
<th>User</th>
<th>Operator</th>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
?>
<tr>
<td>
<?php
$id = $row["id"];
echo $id;
?>
</td>
<td>
<?php
$username = $row["username"];
echo $username;
?>
</td>
<td>
<form method="POST" action="oper.php">
<?php
include ("db.php");
$iddrain= $row['id'];
$q7= "Select * from nezeret where id_m=$iddrain";
//var_dump($q7);
$resultid= sqlsrv_query($link, $q7, $params, $options);
while($row7= sqlsrv_fetch_array($resultid))
{
$rs8=$row7['access'];
//echo $rs8;
if($rs8==1)
{
echo "<p><input type=\"submit\" name=\"uid\" value=Operator onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
else
{
echo "<p><input type=\"submit\" name=\"uid\" value=Nazeret onchange=\"this.form.submit()\"></p>
<p><input type=\"hidden\" name=\"uid\" value=$id onchange=\"this.form.submit()\"></p>";
}
}
?>
</form>
</td>
</tr>
<?php
}
?>
</tr>
</table>
I have a php flie that connects the database and selects the table login_info and retries the login details like email ,name,row id and level and with the result I have shown a table having a check box and I want to pass check box value by href tag.
Below given code:
<?php
include("../script/config.php");
$sql = "select * from login_info";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$username=$row['username'];
$useremail=$row['useremail'];
$level=$row['level'];
$id=$row['id'];
<tr>
<td class="style3"><div align="left" class="style9 style5">
<div align="center"><strong><?php echo $username; ?></strong></div>
</div>
</td>
<td class="style3"><div align="left" class="style9 style5"><div align="center"><strong><?php echo $useremail; ?></strong></div></div>
</td>
<td class="style3"><div align="left" class="style9 style5">
<div align="center">
<input name="checkbox" type="checkbox" value="1"
<?php
if ($level==1)
{
echo 'checked' ;
}
else
{
echo 'unchecked';
}
?>
/>
</div>
</div>
</td>
<td class="style3"><div align="left" class="style9 style5">
<div align="center"><strong>
<a href="../script/Edit_user.php?Id=<?php echo $id;?>&checkbox=<?php echo $checkbox;?>">
<img src="../images/update.jpg" width="42" height="40" u="image" /></a></strong></div>
</div>
</td>
?>
Create a variable and then use it as you like.
$isChecked = ($level == 1 ? 'checked' : 'unchecked');
<input name="checkbox" type="checkbox" value="1" <?php echo $isChecked; ?>/>
<a href="../script/Edit_user.php?Id=<?php echo $id;?>&checkbox=<?php echo $isChecked;?>">
here I have another question, how can we update the database through html/php form with some of the data was populated using dropdown menu?
It is something like this:
User choose to update the asset, and the asset will display the form with the stored data, and they can just directly update the asset using that form again (this is including the dropdown menu).
Hope some of you can get back to me soon. Really appreciate it.
Thank you.
Edited:
<form method="POST" name="update" action="update.php">
<h2>EDIT</h2>
<table class="reference" cellspacing="0" cellpadding="0" border="1" width="60%" id="tablecss">
<tr>
<td> Category </td>
<td> <?php echo '<select name="categoryid">';
foreach ($category as $data)
{
echo '<option'.($row['name']==$data? ' selected' : '').'>'.$data.'</option>';
}
echo '</select>'; ?> </td>
</tr>
<tr>
<td> Brand </td>
<td> <?php echo '<select name="brandid">';
foreach ($brand as $data1)
{
echo '<option'.($row['name']==$data1? ' selected' : '').'>'.$data1.'</option>';
}
echo '</select>'; ?> </td>
</tr>
<tr>
<td> Location </td>
<td> <?php echo '<select name="locationid">';
foreach ($location as $data2)
{
echo '<option'.($row['name']==$data2? ' selected' : '').'>'.$data2.'</option>';
}
echo '</select>'; ?> </td>
</tr>
<tr>
<td> Staff </td>
<td> <?php echo '<select name="staffno">';
foreach ($staff as $data3)
{
echo '<option'.($row['name']==$data3? ' selected' : '').'>'.$data3.'</option>';
}
echo '</select>'; ?> </td>
</tr>
<tr>
<td> Supplier </td>
<td> <?php echo '<select name="supplierid">';
foreach ($supplier as $data4)
{
echo '<option'.($row['name']==$data4? ' selected' : '').'>'.$data4.'</option>';
}
echo '</select>'; ?> </td>
</tr>
<tr>
<td> Project </td>
<td> <?php echo '<select name="projectid">';
foreach ($project as $data5)
{
echo '<option'.($row['name']==$data5? ' selected' : '').'>'.$data5.'</option>';
}
echo '</select>'; ?> </td>
</tr>
<tr>
<td> <input type="hidden" name="assetid" value="<?php echo "$assetid"; ?>" > </td>
</tr>
</table>
<input type="submit" name="submit" value="Update" onclick="return confirm('Update this?');">
<input type="button" value="Back" onclick="history.back();">
</form>
So, basically this can't function well. They didn't capture the stored data, but it can update into the database. Any help?
This is pretty straight forward form processing and you could have found out how to do it with 1 minute of googling...
//formpage.php:
<?php
$data_to_populate_dropdown = array();
//run some sql query or whatever to populate that array
//..or just populate the dropdown statically, then there's no need for this
//if the form has been submitted
$errors = array();
if(isset($_GET['action']) && $_GET['action']=='process') {
if(isset($_POST['my_dropdown'])) {
$my_dropdown_value = mysql_real_escape_string($_POST['my_dropdown']);
//write this to db...
header('Location: formpage.php'); //redirect to avoid double posting;
} else {
$errors[] = 'Some data is missing';
}
}
if(!empty($errors)) {
foreach($errors as $error) {
echo $error;
}
}
?>
<form action="formpage.php?action=process" method="post">
<select name="my_dropdown">
<?php foreach($data_to_populate_dropdown as $key => $value) {
echo '<option value="'.$key.'">'.$value.'</option>';
} ?>
</select>
<input type="submit" value="OK" />
</form>
This code is not tested and quite abstract, but I think you get the idea.