Trying to insert data into another table on button click - php

I have a form that takes data from the db and displays it onto a table using a foreach loop. After reviewing the data, I want to insert that data into another table that is already in the DB via a button click. I have tried multiple ways, but can't seem to get it to work. Any help would be great. Thanks!
my index.php page
<div class='container-fluid'>
<div class='row'>
<div class='col-12'>
<div class='table table-responsive'>
<table class='table table-striped table-bordered datatable active' id='grantTable'>
<thead>
<tr>
<th>FP ID</th>
<th>Short Title</th>
<th>PI</th>
<th>Department</th>
<th>Division</th>
<th>Sponsor Name</th>
<th>Project Start</th>
<th>Project End</th>
<th>Funding Type</th>
<th>Yes/No</th>
<th>Proper Type If No</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<?php
//Foreach loop iterates through each column of $getallrows function
foreach($allRows as $rowID => $rowInfo){ ?>
<tr>
<td><?php echo $rowInfo['fpID'];?></td>
<td><?php echo $rowInfo['shortTitle'];?></td>
<td><?php echo $rowInfo['PI'];?></td>
<td><?php echo $rowInfo['Department'];?></td>
<td><?php echo $rowInfo['Division'];?></td>
<td><?php echo $rowInfo['sponsorName'];?></td>
<td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>
<td><?php echo $rowInfo['fundingType'];?></td>
<!-- //Create dynamic id -->
<?php $rdDrop = "ddgrantType_".$rowInfo['fpID'];?>
<form action="process.php" method="POST">
<td>Yes<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="Yes" id="rdYes" onclick="disable('<?php echo $rdDrop;?>')" checked/><br />
No<input type="radio" name="rdGrant[<?php echo $rowInfo['fpID'];?>]" value="No" id="rdNo" onclick="enable('<?php echo $rdDrop;?>')"/></td>
<input type="hidden" name="id" value="<?php echo $fpID; ?>"/>
<td>
<div class="dropdown">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="<?php echo $rdDrop;?>" data-toggle="dropdown" disabled>Select Proper Funding Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" id="grantComment" placeholder="Comments"></textarea>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="flex-container" id="saveBtn">
<div class="dSave"><button type="submit" class="btn btn-success" name="submit" id="Save">Save</button></div>
<div><button type="submit" class="btn btn-success" name="Complete" id="Complete">Complete and Save</button></div>
</div>
</form>
</div>
</div>
</div>
</div>
My process.php page
if(isset($_POST['submit'])) {
$count = count($_POST['fpID']);
for($i=0; $i < count; $i++) {
$mssql = "INSERT into some_table (FP_id, Short_Title, PI, Department, Division, Sponsor, Date_Project_Start, Date_Project_End, Funding_Type, Yes_No, Proper_Type_If_No, Comment, Complete_Flag)
VALUES ('"
$mssql .= $_POST['Funding Proposal ID'][$i] . "','";
$mssql .= $_POST['Short Title'][$i] . "','";
$mssql .= $_POST['PI'][$i] . "','";
$mssql .= $_POST['Department'][$i] . "','";
$mssql .= $_POST['Division'][$i] . "','";
$mssql .= $_POST['Sponsor Name'][$i] . "','";
$mssql .= $_POST['Date_Project_Start'][$i] . "','";
$mssql .= $_POST['Date_Project_End'][$i] . "','";
$mssql .= $_POST['Funding Type'][$i] . "','";
$mssql .= $_POST['Yes_No'][$i] . "','";
$mssql .= $_POST['Proper_Type_If_No'][$i] . "','";
$mssql .= $_POST['Comment][$i] . "','";
$mssql .= 'No' "')";
}
header("Location: index.php");
}
?>

This:
$mssql .= $_POST['Funding Proposal ID'][$i] . "','";
should be
$mssql .= $_POST[$i]['Funding Proposal ID'] . "','";
Hope it helps

Looks like you only build the SQL query into the $mssql variable, but you don't do anything with it. You should send the variable content to the database object, like
$result = db::$conn->query($mssql);
This way the database should get filled. Put the header statement outside the for loop (at the bottom of the script).

Related

How to submit values of a modal and values outside of modal at the same time?

I have a modal with select value and when I post it I want to submit that values together with other values outside of the modal.
Here is the entire code:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="myForm">
<table id="example" class="table table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th><input name="select_all" value="1" id="example-select-all" type="checkbox" /></th>
<th>Client</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo '<input type=hidden name=fetched_devices_array[] value=' . $row['device_id'] . '>' ?>
<?php echo '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' ?>
</td>
<td>
<?php echo $row["name"]; ?>
</td>
</tr>
<div class="modal" id="acceptModal">
<div class="modal-header">
<h4 class="modal-title" id="exampleModalLabel">Select the return to manufacturer</h4>
</div>
<div class="modal-body">
<p>
<?php
$sql_return_to = $db->prepare("SELECT
users.id as user_id,
users.full_name,
users.role,
companies.company_name
FROM users
INNER JOIN companies ON users.company_id=companies.id
WHERE (users.role=? OR users.role=? OR users.role=? AND users.active=?)");
$sql_return_to->bind_param("iiii", $usr_type1, $usr_type2, $usr_type3, $active);
$sql_return_to->execute();
$result_ = $sql_return_to->get_result();
$result = [];
if ($result_->num_rows > 0) {
$result = $result_->fetch_all(MYSQLI_ASSOC);
}
$sql_return_to->close();
echo "<select class='device_manufacturer' name='device_manufacturer'>";
$nothing = "Nothing selected";
echo "<option value='" . $nothing . "'>" . $nothing . "</option>";
foreach ($result as $row) {
echo "<option value='" . $row['user_id'] . "'>" . $row['company_name'] . ' (' . explode(' ', trim($row['full_name']))[0] . ')' . "</option>";
}
echo "</select>"; ?>
</p>
<div id="wrapper">
<button type="button" id="buttonConfirm" onclick="SubmitFormData()" name="goback" class="btn">Confirm</button>
</div>
</div>
</div>
</tbody>
</table>
</form>
When I call :
function SubmitFormData() {
$("#myForm").submit(); // Submit the form
}
I am able to receive in PHP all the values outside the modal but not the one from the dropdown device_manufacturer . Why does that happen?
P.S: There are values in the dropdown in case you are wondering if it might be empty.

How to select all option for search to show in the below table?

I am creating the form to select all option to show in the below table, I want to search when I am selected the option show in the table. My part to do is let me to onchange "Transaction Type" then show the below table. How to function can let me to select all option to show in the below table? Hope anyone can give me example or edit in my coding to guide me how to do it. I want the output can follow my select date range and transaction type to show the table.
transaction_history file to show the frontend create the form and pass the data to the backend.
<?php
$system_user_type = $user_type;
$lang = $_COOKIE["Language"];
$new_cur_date = date("d-m-Y", strtotime($cur_date));
require_once("language/lang_transaction_" . $lang . ".php");
?>
<html>
<head>
</head>
<body>
<div class="row">
<div class="col-lg-12">
<div class="box form-group">
<header>
<h5><?php echo $language["LIST_TITLE1"]; ?></h5>
<!-- .toolbar -->
<div class="toolbar">
<nav style="padding: 8px;">
<a href="javascript:;" class="btn btn-default btn-xs collapse-box">
<i class="fa fa-minus"></i>
</a>
</nav>
</div><!-- /.toolbar -->
</header>
<section class="content">
<div class="col-lg-12 form-group" >
<label for="text1" class="form-group control-label col-lg-2"><?php echo $language['type']; ?>:</label>
<div class="col-lg-2">
<select id="select_type" class="form-group form-control required" onchange="show_table();">
<option value="transfer" selected><?php echo $language["transfer"]; ?></option>
<option value="withdraw"><?php echo $language["withdraw"]; ?></option>
<option value="upgrade"><?php echo $language["upgrade"]; ?></option>
<option value="register">Register</option>
<option value="receive"><?php echo $language["receive"]; ?></option>
</select>
</div>
<div class="col-lg-8"></div>
</div>
<div class="col-lg-12 form-group">
<label for="text1" class="form-group control-label col-lg-2">Date Range:</label>
<div class="col-lg-2">
<?php echo custom_period_opt(); ?>
</div>
<label for="text1" class="form-group control-label col-lg-2">Date Created</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="start_date" name="start_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
<label for="text1" class="form-group control-label col-lg-2">To</label>
<div class="col-lg-2">
<input type="text" class="form-group form-control datepicker" id="end_date" name="end_date" data-date-format="dd-mm-yyyy" title="" value="<?php echo $new_cur_date; ?>" readonly>
</div>
</div>
<div class="col-lg-12" style="text-align:center; padding-bottom:10px; padding-top:10px;">
<button id="search_button" type="button" class="btn btn-sm btn-primary" onclick="search2_('search', 'bill_table', 'billing');">Search</button>
<button id="clear" type="button" class="btn btn-sm btn-default" onclick="clearData()">Clear</button>
</div>
<div class="" id="table_result">
</div>
</section>
</div>
</div>
</div>
</body>
</html>
<script>
$(function() {
show_table();
$("#select_type").on("change", function() {
show_table();
});
});
function show_table() {
//alert(123);
var select_type = $("#select_type").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type
},
before_send: function() {
show_overLay();
//$('#patient_result').html('');
},
success: function(data) {
hide_overLay('');
//alert(data);
if (data) {
$("#table_result").html("");
$("#table_result").append(data);
//
$('.dataTable').dataTable();
} else {
alert("Please fill in the field.");
}
}
});
}
</script>
<style>
.myClass
{
display: none;
}
</style>
transaction_table file to show the backend function send to frontend page.Below is my coding:
<?php
$select_type = $_POST['select_type'];
if ($select_type == "withdraw") {
echo '<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>';
$i = 1;
$select_transfer = 'SELECT * FROM withdrawal_record bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['withdraw_amount'] . '</td>';
echo '</tr>';
}
echo " </tbody>
</table>";
}elseif ($select_type == "transfer") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>To Type</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_bp WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
if($rs_transfer['point_type']=="2"){
$to_type="Register Point";
}elseif($rs_transfer['point_type']=="3"){
$to_type="Entertainment Point";
}elseif($rs_transfer['point_type']=="4"){
$to_type="Business Point";
}
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $to_type . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "upgrade") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM upgrade_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "register") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM transfer_history_rp WHERE user_id = ' . $user_id . ' and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" and use_type=1 ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}elseif ($select_type == "receive") {
?>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>No</th>
<th>Date</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$select_transfer = 'SELECT * FROM extra_point WHERE user_id = ' . $user_id . ' ORDER BY created';
$arr_transfer = db_conn_select($select_transfer);
foreach ($arr_transfer as $rs_transfer) {
echo '<tr>';
echo '<td>' . $i++ . '</td>';
echo '<td>' . date('d-m-Y', strtotime($rs_transfer['created'])) . '</td>';
echo '<td>' . $rs_transfer['total_amount'] . '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php
}
?>
Below is my output, this output just can follow what I select the transaction type to onchange to show in the table:
Below is my database information:
If I understand your question correctly, then you need to include these additional parameters in your request as you already to with select_type.
First, get the additional values in theshowTable function, also add them to the request data:
function show_table() {
var select_type = $("#select_type").val();
var start_date = $("#start_date").val();
var end_date = $("#end_date").val();
$.ajax({
url: "?f=transaction_table",
type: "POST",
data: {
select_type: select_type,
start_date: start_date,
end_date: end_date
},
// ...
}
then, receive the values in your PHP file like you already do with select_type:
<?php
$select_type = $_POST['select_type'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
// ...
Finally, you have to adjust your queries accordingly. As you already have and created between "2019-11-01 12:25:05" and "2099-11-01 12:25:05" in your queries, it's just a matter of replacing the hardcoded dates with the ones you received. Do not forget to sanitize your input dates! If possible use prepared statements.
Note 1: Your script and style tags are outside of the html part of your site. This is not valid. These tags belong into either the head or the body tag.
Note 2: There are whitespaces before your opening html tag. You may wan't to avoid this too, since it can trigger IE into quirks mode. Simply change this
?>
<html>
to this
?><html>

How can I put a form for each row of a table

Here's the thing, I need to update a selected row from a table, so I'm putting a form for each row of it (Every single row has an update button) and when I click update, it doesn't submit, actually, doesn't do anything.
Here's my code, I'll be grateful with the solution.
<div class="table-responsive">
<table class="table table-condensed">
<thead class="">
<tr>
<th>ID</th>
<th>Project</th>
<th>Type</th>
<th>Kick-Off Date</th>
<th>Deadline Date</th>
<th>Current Ninja</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$q = $_GET['q'];
$sql="SELECT pr.project_name, pr.project_type, pr.project_start_date, pr.project_end_date, us.nombre, st.id_status, pr.id_project FROM NT_Projects as pr LEFT JOIN Project_Status as st on st.id_status = pr.id_status LEFT JOIN NT_Clients as cl on cl.id_client = pr.id_client LEFT JOIN usuarios as us on us.username = pr.username WHERE cl.id_client = $q";
$result = mysql_query($sql) or die(mysql_error());
$upt = 1;
while($row = mysql_fetch_array($result)) {
echo '
<div id="update-project">
<form method="post" action="sistema/_actions/updateProject.php" id="res-update-proj-'.$upt.'">';
$kickoff = date('m/d/Y', strtotime($row[2]));
$deadline = date('m/d/Y', strtotime($row[3]));
echo '<tr>';
echo '<td width="95px">
<input class="form-control" name="id_Proj" type="text" value="'.$row[6].'" readonly>
</td>';
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $kickoff . "</td>";
echo "<td>" . $deadline . "</td>";
echo "<td>" . $row[4] . "</td>";
echo '<td width="225px">';
echo '<select class="form-control" name="proj_Status">';
$qStatus = "SELECT * FROM Project_Status;";
$exStatus = mysql_query($qStatus);
while($rStatus = mysql_fetch_array($exStatus))
{
if($row[5] == $rStatus[0])
echo '<option value="'.$rStatus[0].'" selected>'.$rStatus[1].'</option>';
else
echo '<option value="'.$rStatus[0].'">'.$rStatus[1].'</option>';
}
echo '</select>
</td>
<td class="text-center">
<button type="submit" class="btn btn-sm btn-primary btn-UProj" value="res-update-proj-'.$upt.'">Update</button>
<div id="res-update-proj-'.$upt.'" style="width: 100%; margin:0px; padding:0px;"></div>
</td>
</tr>
</form>
</div>';
$upt = $upt + 1;
}
?>
</tbody>
</table>
</div>
That code is being called from another HTML with ajax
You cannot mix table tags with divs and forms according to the web standards. If you do, browser's HTML-parser will mix your tags in unpredictable way.
The only solution I know is to utilize CSS:
<div style="display: table">
<form style="display: table-row">
<div> style="display: table-cell"></div>
</form>
</div>

How to create search function in PHP/MySQL

I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
I want to search in my table, this works fine so far but when I search on A for example and I click my pagination page 1 it rebuilds the page with all te records from the database.
I tried to add a parameter to the url upon button click but upon creating this button with link the searchValue is still null.
This question is not about my sql query. It's about searching with pagination
Could anyone help me fix this?

Collapsing bootstrap table with select

I have a bootstrap table where I get the values from my database using select:
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>Studentnamn</th>
<th>Student ID</th>
<th>Registrerade kurser</th>
<th>Betyg</th>
</tr>
</thead>
<tbody>
<?php include 'studentDB.php'; ?>
</tbody>
</table>
I want to make my table collapse and then display an inputform where the values on onclicked row can be updated. I have used following code:
$sql = "SELECT student.studName, grades.studId, grades.courseId, grades.grade FROM student
INNER JOIN grades ON student.studId=grades.studId";
try {
foreach ($pdo->query($sql) as $row) {
echo '<tr data-toggle="collapse" data-target="#collapseUpdate">';
echo '<td>' . $row['studName'] . '</td>';
echo '<td>' . $row['studId'] . '</td>';
echo '<td>' . $row['courseId'] . '</td>';
echo '<td>' . $row['grade'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="6" class="hiddenRow" id="collapseRow"><div class="accordian-body collapse" id="collapseUpdate">
<div class="card card-block">
<form class="form-horizontal" action="update.php" method="post" id="newGrade">
<div class="control-group">
<label class="control-label">Nytt betyg:</label>
<div class="controls">
<p><input id="cbetyg" name="grade" type="text"></p>
<input type="hidden" name="studId" value="'. $row['studId'] .'">
<input type="hidden" name="courseId" value="'. $row['courseId'] .'">
</div>
</div>
<div class="form-actions">
<input type="submit" name="submit" value="Ă„ndra">
</div>
</form>
My problem here is now that, only the first row collapses and can use update. When I click on the other rows, the first row collapses... Can somebody tell me what I can do? And also, will my update still work for each individual row if I make the collapse work for all the rows?
My table looks like this (click here)
Thanks!
You are using the same collapseUpdate target in any field. Try modify these lines:
instead
echo '<tr data-toggle="collapse" data-target="#collapseUpdate">';
use
echo '<tr data-toggle="collapse" data-target="#collapseUpdate'.$row['studId'].'">';
and instead
echo '<td colspan="6" class="hiddenRow" id="collapseRow"><div class="accordian-body collapse" id="collapseUpdate">
use
echo '<td colspan="6" class="hiddenRow" id="collapseRow"><div class="accordian-body collapse" id="collapseUpdate'.$row['studId'].'">
I suspect that studId is unique in this table.

Categories