I want to get the attendance from the student list into my database.
I have my newattinsert.php file here:
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="date" name="attdate" id="attdate" data-date="" data-date-format="DD MMMM YYYY" placeholder="MM/DD/YYYY" name="attdate" id="attdate" class="form-control" min="1990-01-01" max="2030-12-31" required/>
</div>
<?php
if (! empty($_POST['classn'])) {
$class = $_POST['classn'];
//run the store proc
$sql = "CALL addattlist('".$class."')";
$result = mysqli_query($conn,$sql);
if (! empty($result)) {?>
<div class="row" style="width: 140%;">
<table id="myTable" class="table table-bordered table-striped">
<thead>
<th>Student Name</th>
<th>Class Name</th>
<th>Status</th>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($result)){
?>
<tr>
<td><input type="hidden" name="name" value="<?php echo $row[1] ?>"><?php echo $row['studentname']; ?></td>
<td><input type="hidden" name="classn" value="<?php echo $row[2] ?>"><?php echo $row['classname']; ?></td>
<td>
<input type="radio" id="absent" name="status" value="Absent">
<label for="status">Absent</label>
<input type="radio" id="present" name="status" value="Present">
<label for="status">Present</label>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
}
?>
<div class="group" href="attendanceinsert.php" style="padding-left: 10px; padding-right: 5px;">
<button type="submit" name="submit" class="btn btn-success"><span class="glyphicon glyphicon-ok-sign"></span> Submit Data</button> OR <button type="reset" class="btn btn-warning"> <span class="glyphicon glyphicon-refresh"></span> Reset The Form</button>
</div>
and my attedanceinsert.php file here:
<?php
include('connection.php');
if(isset($_POST['submit'])){
$name= $_POST['. $row[1] .'];
$classn= $_POST['. $row[2] .'];
$status= $_POST['status'];
$attdate= $_POST['attdate'];
$sql = "CALL attendanceregister('".$_POST['name']."', '".$_POST['classn']."','".$_POST['status']."','".$_POST['attdate']."')";
$result = mysqli_query($conn,$sql);
if($result){
$_SESSION['success'] = 'Attedance Recorded!';
header('location: allatt.php');
}
else{
$_SESSION['error'] = 'Something went wrong!';
}
}
?>
the problem is, I only get the data of the last student in the list only and the radio button only allows me to choose one choice only. If I click on the radio button on the student no1, the data that get into my database is the data from the student no2. Any idea how to resolve this? I'm really desperate as this is the only problem that I'm stuck at right now.
ps: this is the stored procedure that I use
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `addattlist`(IN `class` VARCHAR(20))
NO SQL
BEGIN
SELECT studentid, student_name, classname
FROM student
WHERE classname = class;
END$$
DELIMITER ;
EDIT: I still can't figure out the solution. I'm open to any changes or anything that will make it work.
Related
I have an order table in my project. I am pulling the products from the database to this table using foreach. I show the quantity and unit price calculation information on the table instantly with jquery. I want to save the data in this table from the row selected with the checkbox to the database. I tried a few things with foreach but it looped so it created a new record for each row in the database. I want to print arrays with implode using commas between them. For example, the data in the rows entered in the quantity field in the table should be entered in the quantity field in the database as 1,2,3,4. If I have to explain briefly, I want to make an insert in one go.
Table:
Table & Form Code:
<form action="" method="POST">
<table class="table table-sm mb-3 text-center align-middle">
<thead>
<tr>
<th>Choose</th>
<th>Product Name</th>
<th width="137px">Quantity</th>
<th>Unit Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php foreach($ProductTbl as $product){ ?>
<tr>
<th scope="row">
<div class="form-check form-check-success">
<input class="form-check-input" name="check[]" type="checkbox" value="
<?= $product-> productid">
</div>
</th>
<td>
<?= $product->productname ?>
</td>
<td>
<input class=" w-25 text-center quantity" type="text" name="quantity[]" value="0">
</td>
<td>
<input class="w-25 text-center unitprice" type="text" name="unitprice[]" value="
<?= $product->unitprice ?>" disabled="disabled"> €
</td>
<td>
<input class="w-25 text-center totalprice" type="text" name="totalprice[]" value="" disabled="disabled"> €
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center">
<button class="btn btn-success col-md-2" type="submit" name="add">Offer Preview</button>
<button class="btn btn-warning col-md-1" type="reset">Reset</button>
<a href="#">
<button class="btn btn-danger col-md-1" type="button">Cancel</button>
</a>
</div>
</form>
//The code structure I tried
if(isset($_POST['add'])){
$check = $_POST['check'];
$quantity = implode(',', $_POST['quantity']);
$totalprice = implode(',', $_POST['totalprice']);
if (!empty($check)) {
foreach ($check as $chk => $value){
// I printed it for testing.
echo $value.' - product id in the checked checkbox<br>';
print_r($quantity);
print_r($totalprice);
}
}
}
How does this code work the way I want?
I am trying to send my dynamic table to mysql database but I am having difficulties. I've tried using a for to get it to send but I'm not getting very far. At the moment I am just displaying it but I really want to send it to mysql. I want all the data to be in one row with the rest of the data like test case name, test case number and so on. But the dynamic table must be put into the same row where the rest of the data goes.
Below you will see what I have done and maybe you can see what my intentions are...
<div class="col-md-6">
<label>Test Case Number:</label>
<?php
$sql = "SELECT test_case_number FROM qa_testing_application ORDER BY id desc LIMIT 1";
$result = mysqli_query($database, $sql) or trigger_error("SQL", E_USER_ERROR);
while ($row = mysqli_fetch_assoc($result)) {
?>
<input class="form-control" style="display: none" type="text" name="test_case_number" readonly="readonly" value="<?php echo $row['test_case_number']+3?>">
<?php } ?>
<label>Company Name:</label>
<input class="form-control" type="text" name="test_case_company_name"/>
<label>Tester:</label>
<input class="form-control" style="display: none" type="text" name="user_username" value="<?php echo $user_username ?>" readonly/>
<label>System:</label>
<input class="form-control" type="text" name="test_case_system"/>
<label>URL:</label>
<input class="form-control" type="text" name="test_case_url"/>
</div>
<div class="col-md-12" style="border: 1px solid #28415b; padding-bottom: 12px; padding-top: 12px;margin-top: 20px; margin-bottom: 15px">
<p>
<INPUT class="btn btn-primary ladda-button" type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT class="btn btn-primary ladda-button" type="button" value="Delete row" onclick="deleteRow('dataTable')" />
</p>
<div class="clear"></div>
<p>'All fields below are compatible to use markdowns for editing' </p>
<table class="table table-hover">
<thead>
<tr>
<th style="width: 15px">Chk</th>
<th style="width: 335px">Action:</th>
<th style="width: 326px;">Expected System Response:</th>
<th style="width: 151px;">Pass/ Fail</th>
<th>Comment</th>
</tr>
</thead>
</table>
<table id="dataTable" class="table table-hover">
<tbody>
<tr>
<td style="width:20px;"><INPUT type="checkbox" name="chk[]" id="chk"/></td>
<td><INPUT class="form-control" type="text" name="step[]" autocomplete="on" placeholder="Action" required/></td>
<td><INPUT class="form-control" type="text" name="url[]" autocomplete="on" placeholder="Expected Outcome" required/></td>
<td>
<select name="passfail[]" class="form-control" style="width:120px;">
<OPTION value="Pass">....</OPTION>
<OPTION value="Pass">Pass</OPTION>
<OPTION value="Fail">Fail</OPTION>
</select>
</td>
<td>
<TEXTAREA class="form-control" type="text" name="comment[]" rows="2" cols="15" placeholder="Comment" required></TEXTAREA>
</td>
</tr>
</tbody>
</table>
Thats my table that i am dynamically making...
In my PHP file i display the dynamic table as follows:
<table class="table table-bordered">
<thead>
<tr>
<td>Step</td>
<td>process</td>
<td>Expected System Response</td>
<td>
<center>Pass/ Fail</center>
</td>
<td>Comment</td>
</tr>
</thead>
<?php
if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$pass_fail = $_REQUEST['passfail'];
$comment = $_REQUEST['comment'];
$countPass = 0;
$countFail = 0;
foreach ($step as $key => $row) {
?>
<tbody>
<tr>
<td><?php echo $key + 1; ?></td>
<td><?php echo $step[$key]; ?></td>
<td><?php echo $url[$key]; ?></td>
<td style="color:<?php if ($pass_fail[$key] == 'Fail') {
echo 'color: red';
} else {
echo 'limegreen';
} ?>"><b>
<center><?php echo $pass_fail[$key]; ?></center>
</b></td>
<td><?php echo $comment[$key]; ?>
</td>
</tr>
</tbody>
</table>
Now how do i insert it into mysql???
So basically what you need to do to insert it into your database is doing a sql query in PHP.
Let me show you an code example:
//establish db connection
$con=mysqli_connect("dbhost","username","dbpassword","dbname");
$sql = "INSERT INTO tablename (name_of_row1,
name_of_row2,
name_of_row3)
VALUES ('".$value1."',
'".$value2."',
'".$value3."')";
mysqli_query($con, $sql);
mysqli_close($con);
So you have to open a sql connection and then insert your data to the table you want. I hope this it's clear enough. Now if the PHP file gets called the SQL query is beeing made.
I am doing a registration form where i am also saving image to database, well that image is being stored in LONGBLOB format. I am able to add and view all the data accept the image part.
Here is my product_register.php code:
<form role="form" method="post" action="product_register.php">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Product Name" name="product_name" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Product Code" name="product_code" type="text" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Product Image" name="product_image" type="file" value="">
</div>
<div class="form-group">
<input class="form-control" placeholder="Purchase Date" name="date" type="date" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Time Guarantee" name="time_guarantee" type="text" autofocus>
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="submit" name="submit" >
</fieldset>
</form>
<?php
include("db_conection.php");//make connection here
//mysql_close();
if(isset($_POST['submit']))
{
$usern = $_SESSION['sess_username'];
$product_name = $_POST['product_name'];
$product_code = $_POST['product_code'];
$product_image = $_POS['product_image'];
$date = $_POST['date'];
$time_guarantee = $_POST['time_guarantee'];
$insert_user = "insert into product (product_name,product_code,product_image,date,time_guarantee,usern) VALUE ('$product_name','$product_code','$product_image','$date','$time_guarantee','$usern')";
if(mysqli_query($dbcon,$insert_user))
// $result = mysql_query($insert_user);
{
echo "<script type='text/javascript'>alert('Product added successfully !!!')</script>";
// echo"<script>window.open('product_register.php','_self')</script>";
// mysql_close();
}
}
?>
Here is the code to view it view_product.php code:
<table class="table table-bordered table-hover table-striped" style="table-layout: fixed">
<thead>
<tr>
<!-- <th>User Id</th> -->
<th>Product Name</th>
<th>Product Code</th>
<th>Product Image</th>
<th>Date of Purchase</th>
<th>Time Guarantee</th>
<!-- <th>Delete User</th> -->
</tr>
</thead>
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("gmgmt") or die(mysql_error());
// $view_users_query="select * from users WHERE role='user'"; //select query for viewing users.
// $view_users_query="select * from users WHERE role='admin'"; //select query for viewing users.
$view_users_query="select * from 'product' WHERE 'usern' = 'demo'"; //select query for viewing users.
// $run=mysql_query($view_users_query);//here run the sql query. // '".$_SESSION['userid']."'
$check = mysql_query("SELECT * FROM product WHERE usern = '".$_SESSION['sess_username']."' ") or die(mysql_error());
while($row=mysql_fetch_array($check)) //while look to fetch the result and store in a array $row.
{
$id=$row[0];
$product_name=$row[1];
$product_code=$row[2];
$product_image=$row[3];
$date=$row[4];
$time_guarantee=$row[5];
// $usern=$row[6];
?>
<tr>
<!--here showing results in the table -->
<!-- <td><?php echo $id; ?></td> -->
<td><?php echo $product_name; ?></td>
<td><?php echo $product_code; ?></td>
<td><?php echo "<img src='php/imgView.php?imgId=".$product_image."' />"; ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $time_guarantee; ?></td>
<!-- <td><button class="btn btn-danger">Delete</button></td> -->
</tr>
<?php } ?>
</table>
I searched a lot but I didn't find a code or method that suits my needs.
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['imageContent'] ) . '" />';
Use the above code to display you image store in mysql database. Hope this will help you
I just want to ask on how can I create a dynamic ID in the fields that I have loaded.
Here is my view:
<div id="content">
<form action="" method="POST">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" onclick="location.reload();"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
</div>
<div id="div1">
<fieldset>
<legend><?php // echo $tablename; ?></legend>
<table id="table_main">
<tr>
<th>Column Name:</th>
<th>Type:</th>
<th>Length/Values:</th>
</tr>
<?php foreach($updatemaintenance1 as $field) { ?>
<tr>
<td><input type="text" value="<?php echo $field->name; ?>" style="width: 80%"/></td>
<td>
<select style="width: 80%">
<option><?php echo $field->type; ?></option>
<option>INT</option>
<option>VARCHAR</option>
<option>TEXT</option>
<option>DATE</option>
</select>
</td>
<td><input type="text" value="<?php echo $field->max_length; ?>" style="width: 100%"/></td>
</tr>
<?php } ?>
<tr>
<?php //foreach ($tablename as $row1) { ?>
<td><input type="text" id="ninja79" name="ninja79" value="<?php echo $table_name1; ?>"/></td>
<?php// } ?>
</tr>
</table>
<div class="modal-footer">
<button type="button" class="btn_editinventorytype btn-info btn-sm">Update</button>
<button type="button" class="btn_deleteinventorytype btn-danger btn-sm">Delete</button>
</div>
</fieldset>
</div>
</form>
</div>
As you can see I have a foreach loop where I placed all the values from my database. What I want is to generate an ID on the textboxes and the selectbox. And I have this code in the model where I can modify the loaded fields.
Here is my model:
public function modify_table($modify){
$fields = array($modify[''] = array('name' => $modify[''],
'type' => $modify[''],
'constraint' => $modify['']),
);
$this->dbforge->modify_column($modify['ninja79'], $fields);
}
This codes are not as simple as 'SELECT * FROM 'tablename' I mean I'm talking about modifying tables in the database, so please could you help me? All I want is to have a dynamic ID in my model and in my view. I have no idea on how to code that. This is confusing so PLEASE I need your help. Thank you in advance!
I've been trying to submit two buttons, one to delete and one to edit/add. But I can't seem to get it right.
HTML:
<!-- Edit/Delete Account Form -->
<form method="get" action="edit.php" onsubmit="setTimeout('location.reload()');">
<div id="edit-form" class="modal hide fade">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4><?php _e('Edit/Delete Account'); ?></h4>
</div>
<div class="modal-body">
<div id="message"></div>
<div class="control">
<div id="edit-account-form">
<table id="add-account" class="table">
<tbody>
<tr>
<td valign="middle">
Account Number
</td>
<td>
<input type="hidden" id="editid" name="editid" value=""/>
<input type="text" id="number" name="number" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Account Description
</td>
<td>
<input type="text" id="description" name="description" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 01
</td>
<td>
<input type="text" id="level01" name="level01" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 02
</td>
<td>
<input type="text" id="level02" name="level02" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 03
</td>
<td>
<input type="text" id="level03" name="level03" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Level 04
</td>
<td>
<input type="text" id="level04" name="level04" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Tax
</td>
<td>
<input type="text" name="account" value="Enter Some text here "/>
</td>
</tr>
<tr>
<td>
Allow General Journals
</td>
<td>
<select>
<option selected disabled>Select</option>
<option>Yes</option>
<option>No</option>
</select>
</td>
</tr>
<tr>
<td>
Allow Payments
</td>
<td>
<select>Select</select>
</td>
</tr>
<tr>
<td>
Allow Expense Claims
</td>
<td>
<select>Select</select>
</td>
</tr>
<tr>
<td>
Show On Dashboard
</td>
<td>
<select>Select</select>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" data-complete-text="<?php _e('Done'); ?>" name="add" class="btn btn-primary pull-right"><?php _e('Submit'); ?></button>
<p class="pull-left"><button type="submit" name="delete" class="btn btn-primary pull-right"><?php _e('Delete'); ?></button></p>
</div>
</div>
</form>
PHP:
<?php
//Account Details
$number = $_GET['number'];
$description = $_GET['description'];
$level01 = $_GET['level01'];
$level02 = $_GET['level02'];
$level03 = $_GET['level03'];
$level04 = $_GET['level04'];
$id = $_GET['editid'];
$dbc = mysqli_connect('localhost', 'root', 'root', 'accounting') or die('Connection error!');
if(isset($_POST['delete'])) {
$check = "DELETE FROM accountSlave WHERE `id` = '$id' ";
mysqli_query($dbc, $check) or die('Database error, delete account!');
}
if(isset($_POST['add'])) {
//$check = "DELETE FROM accountSlave WHERE `id` = '$id' ";
$check = "UPDATE accountSlave SET `accountNumber` = '$number', `accountDescription` = '$description', `accountLevel1` = '$level01', `accountLevel2` = '$level02', `accountLevel3` = '$level03', `accountLevel4` = '$level04' WHERE `id` = '$id'";
mysqli_query($dbc, $check) or die('Database error, add account!');
}
header('location:master-accounts.php');
?>
It works when I have one query. I've tried one query to add and to delete and it worked. But I need to submit a delete if delete button clicked and vice versa.
I followed this post to try get it right multiple buttons on a form
Any help or suggestions will be appreciated!
You need to use $_GET instead of $_POST. It should be like
if(isset($_GET['delete'])) {
and
if(isset($_GET['add'])) {
Because your form submit method is GET not the POST
You have to check which button is pressed and according to that do your operation.
Also you have to use $_GET not $_POST because your form method is GET.
if(isset($_GET['add'])) {
// code for add
} elseif(isset($_GET['delete'])) {
// code for delete
}