Saving select option value into database for all rows at once - php

I have this problem of saving the option value into the database. Everything seems to look correctly but when submitted, the value in the database does not change. The form is in a table which retrieve user's submission and consent_id was use to update the option the admin. Hope anyone can spot what I am missing or what it should be written. Thanks.
This is my code:
manageLeave.php
<script type="text/javascript">
$(document).ready(function () {
$("#confirmForm").click(function () {
$("#statusForm").submit();
});
});
</script>
<body>
...
<tr>
<td><?php echo $staffName; ?></td>
<td><?php echo $convertDateFrom; ?></td>
<td><?php echo $convertDateTo; ?></td>
<td><?php echo $reason; ?></td>
<td>
<form method="post" action="doManageLeave.php" id="statusForm" name="statusForm">
<input type="hidden" name="consentId" value="<?php echo $consentId; ?>">
<select name="leaveStatus" data-native-menu="false">
<option value="" disabled selected style="display: none">Please select one...</option>
<option value="1">Approved</option>
<option value="2">Rejected</option>
</select>
</form>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
}
?>
<input type="button" id="confirmForm" value="Submit" />
</body>
doManageLeave.php
$getConsentId = $_POST['consentId'];
$leaveStatus = $_POST['leaveStatus'];
$editQuery = "UPDATE consent SET approval_id = '$leaveStatus' WHERE consent_id = $getConsentId";
$successInsert = mysqli_query($link, $editQuery) or die ("Error Query ". mysqli_error($link));
mysqli_close($link);

Related

PHP value wont post

Hello for some reason job_id will not parse from myjobs.php to payment.php and I really cannot see how.Does anyone know why this may be?Similar files seem to work but for some reason this wont I am thinking maybe because I also have html code on payment.php which i haven't done before?
<?php while($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row['job_id']; ?></td>
<td><?php echo $row['title'];?></td>
<td><?php echo $row['description'];?></td>
<td><?php if($row['accepted']==1 AND $row['start_escrow']==0):?><form action = "payment.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
<td><?php if($row['start_escrow']==1):?><form action = "review.php">
<input type="hidden" value="<?php echo $row['job_id']?>" name="job_id" />
<input type="submit" class="btn btn-xlarge btn-block btn-primary" value ="Start Escrow"></input></input><?php endif; ?></td>
</tr>
<?php endwhile;?>
</table>
payment.php
<?php
require 'config.php';
$jobid = $_POST['job_id'];
$query = "UPDATE job SET start_escrow = '1' WHERE job_id = '$jobid''";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
echo "Thank You For Contacting Us <br>";
$conn->close();
?>
Set your method as POST in form.
<form action = "payment.php" method="POST">
<form action = "review.php" method="POST">

Inserting values into database using PDO in Jquery AJAX PHP

I'm still learning PHP and I'm trying to insert new values to the database using a dropdown. So far this is what I've made:
<table class="table">
<tr>
<th>Employee Name</th>
<th>Time</th>
<th>Priority</th>
<th>Assignee</th>
<th>Subject</th>
<th>Problem</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM tickets order by ticketno DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['full_name']; ?></td>
<td><?php echo $row_message['time']; ?></td>
<td><?php echo $row_message['priority']; ?></td>
<?php if ($row_message['assignee']) : ?>
<td><?php echo $row_message['assignee']; ?></td>
<?php else : ?>
<td>
<form method="post" action="update1.php">
<input type="hidden" name="ticketno" value="<?php echo $row_message['ticketno']; ?>" />
<input type="submit" name="accept" value="Accept"></input>
</form>
</td>
<?php endif ; ?>
<td><?php echo $row_message['subject']; ?></td>
<td><?php echo $row_message['problem']; ?></td>
<td>
<label for=""></label> <select style="font-family: Questrial;" name="status" required>
<option disabled selected hidden>Select Status</option>
<option name="status" value="In Progress">In Progress</option>
<option name="status" value="Closed: Cancelled">Closed: Cancelled</option>
<option name="status" value="Closed: Solved">Closed: Solved</option>
</select>
</td>
</tr>
<?php } ?>
</table>
I also got a sample script:
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var gender = $(this).val();
$.ajax({
url:"insert.php",
method:"POST",
data:{gender:gender},
success: function(data){
$('#result').html(data);
}
});
});
});
</script>
My concern is, I'm not sure what should be my ('input[type="radio"]') if I'm using a dropdown.
Here's my table schema:
Table Schema
<!-- page1.php--->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="">Change Status</label>
<select style="font-family: Questrial;" name="status" id="status" onchange="getdropdownvalue();" required>
<option disabled selected hidden>Select Status</option>
<option name="status" value="in_progress">In Progress</option>
<option name="status" value="cancelled">Closed: Cancelled</option>
<option name="status" value="solved">Closed: Solved</option>
</select>
<div id="result">
</div>
<script>
function getdropdownvalue(){
var status =$("#status").val();
alert("status value is-"+status);
if(status!=''){
$.ajax({
type:'POST',
url:"insert.php",
data:"status="+status,
success:function(data){
if(data.trim() == 'success')
{
$("#result").html("<div style='color:green;'>record inserted successfully </div>");
}else{
// error
}
}
});
}
}
</script>
<?php
/************** insert.php page ***********/
// you can get status value by $_POST
if(isset($_POST['status'])){
$status = $_POST['status'];
// insert query here
// if insert successfully you should -
//echo "success";
}
?>

some combobox reset each other after selected

I have some combobox with onchange event, and they're reset each other when selected the orther one of them, does any suggest how to retain the value on the page? this my script :
<form method="POST" name="form1" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>
<select name="select_petugas1" style="width:18px;" onchange="this.form.submit('select_petugas1');"> //first combobox
<option></option>
<?php include 'dbconn.php';
$sql_peg1="SELECT * FROM users"; $result_peg1=$conn->query($sql_peg1);
while( $row_peg1=$result_peg1->fetch_assoc() ){
echo "<option>".$row_peg1['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if(isset($_POST['select_petugas1'])){
$select_petugas1=$_POST['select_petugas1'];
echo "<input type='text' name='select_petugas1' value='".$select_petugas1."'>"; // Throw 1st result into the text box
$sql_NIP1="SELECT NIP FROM users WHERE nama='$select_petugas1'";
$result_NIP1=$conn->query($sql_NIP1);
$row_NIP1=$result_NIP1->fetch_assoc();
$NIP1=$row_NIP1['NIP'];
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type="text" name='NIP1' value="<?php echo $NIP1; ?>"></td>
</tr> <!-- child of first result -->
<tr>
<td colspan="5" bgcolor="blue"></td>
</tr>
<tr>
<td>
<select name="peg_2" style="width:18px;" onchange="submit(this)"><!--2nd combobox-->
<option></option>
<?php
$sql_peg2="SELECT nama FROM users";
$result_peg2=$conn->query( $sql_peg2 );
while ($row_peg2=$result_peg2->fetch_assoc()){
echo "<option value='".$row_peg2['nama']."'>".$row_peg2['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if( isset($_POST['peg_2']) ){
$peg_2=$_POST['peg_2'];
echo "<input type='text' name='peg2' value='".$peg_2."'>"; // 2nd result throw into 2nd texbox
$sql_NIP2="SELECT NIP FROM users WHERE nama='$peg_2'";
$result_NIP2=$conn->query($sql_NIP2);
$row_NIP2=$result_NIP2->fetch_assoc();
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type='text' name='NIP2' value="<?php echo $row_NIP2['NIP'];?>"> <!--2nd child of result-->
<?php
}
}
if(isset($_POST['NIP2'])){
$NIP2=$_POST['NIP2'];
echo "<br /> NIP2 :".$NIP2."<br />";
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
<form method="POST" name="wilayah" id="wilayah" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="1">
<tr>
<td>
<select name="select_provinsi" onchange="submit(this)" style="width:18;">
<option selected>PROVINSI</option>
<?php
include 'dbconn.php';
$sql_prov="SELECT * FROM wilayah GROUP BY provinsi";
$result_prov=$conn->query($sql_prov);
echo "";
while($row_prov=$result_prov->fetch_assoc()){
$provinsi=$row_prov['provinsi'];
echo "<option value='".$provinsi."'>".$provinsi."</option>";
}
?>
</select>
<?php
if(isset($_POST['select_provinsi'])){
$select_provinsi=$_POST['select_provinsi'];
echo "
<input type='text' name='select_provinsi' value='".$select_provinsi."' placeholder='PROVINSI'>
</td>
</tr>";
$sql_kabkota="SELECT * FROM wilayah WHERE provinsi='$select_provinsi' GROUP BY kab_kota";
$result_kabkota=$conn->query($sql_kabkota);
?>
<tr>
<td>
<select name="select_kabkota" style="width:18px;" onchange="submit(this)"><option>KAB/KOTA</option>
<?php
while($row_kabkota=$result_kabkota->fetch_assoc()){
echo "<option>".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
<?php
}
if(isset($_POST['select_kabkota'])){
$select_kabkota=$_POST['select_kabkota'];
?>
<input type="text" name="kab_kota" value="<?php echo $select_kabkota;?>">
<?php
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
hope any suggestion for resolved of my problem with them,,
onchange="submit(this)" means that you want to submit the form when the value of the combobox changes. So, when the form is sent, the page reloads and you get the default value of your form.
To restore the chosen value, I would do something like :
<select name="select_kabkota" style="width:18px;" onchange="submit(this)">
<option>KAB/KOTA</option>
<?php
if(isset($_POST['select_kabkota']))
$select_kabkota=$_POST['select_kabkota'];
while($row_kabkota=$result_kabkota->fetch_assoc())
{
$selected = $select_kabkota == $row_kabkota['kab_kota'] ? 'selected="selected"' : '';
echo "<option ".$selected." >".$row_kabkota['kab_kota']."</option>";
}
?>
</select>

What is the error in my php code that I cannot use the edit(function)?

I have sql consist of: idChecklist,descriptiveTitle, courseNo, lec, lab, units, year, semester. Now there is no error in my code when I run, but I cannot edit the words I want to edit. It just views my data in database. when I click the edit and press save nothing happens. What is the missed for edit in my code? Thanks
editSubject.php
<?php
include 'connection.php';
$id=$_GET['id'];
?>
<html>
<title>Edit Subjects</title>
</head>
<body>
<form method="post">
<table>
<?php $checklist_query=mysql_query("select * from checklist where idChecklist='$id'");
$checklist_rows=mysql_fetch_array($checklist_query);
?>
<tr><td>Course:</td><td><input type="text" name="course" value="<?php echo $checklist_rows['course']; ?>"></td></tr>
<tr><td>Descriptive Title:</td><td><input type="text" name="descriptiveTitle" value="<?php echo $checklist_rows['descriptiveTitle']; ?>"></td></tr>
<tr><td>Course No:</td><td><input type="text" name="courseNo" value="<?php echo $checklist_rows['courseNo']; ?>"></td></tr>
<tr><td>Lec:</td><td><input type="text" name="lec" value="<?php echo $checklist_rows['lec']; ?>" ></td></tr>
<tr><td>Lab:</td><td><input type="text" name="lab" value="<?php echo $checklist_rows['lab']; ?>" ></td></tr>
<tr><td>Units:</td><td><input type="text" name="units" value="<?php echo $checklist_rows['units']; ?>" ></td></tr>
<tr><td>Year:</td><td><input type="text" name="year" value="<?php echo $checklist_rows['year']; ?>" ></td></tr>
<tr><td>Semester:</td><td><input type="text" name="semester" value="<?php echo $checklist_rows['semester']; ?>" ></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="save"></td></tr>
</table>
</form>
</body>
</html>
<?php
if (isset($_POST['submit'])){
$course=$_POST['course'];
$descriptiveTitle=$_POST['descriptiveTitle'];
$courseNo=$_POST['courseNo'];
$lec=$_POST['lec'];
$lab=$_POST['lab'];
$units=$_POST['units'];
$year=$_POST['year'];
$semester=$_POST['semester'];
mysql_query("update subjectschedule set course='$course',descriptiveTitle='$descriptiveTitle',courseNo='$courseNo',lec='$lec' ,lab='$lab' ,units='$units' ,year='$year' ,semester='$semester'where idChecklist='$id'");
header('location:checklist.php');
}
?>
Checklist.php
<!doctype html>
<html>
<head>
<body>
Back
AddSubject
<p>Sort by:</p>
Course<select id = "course">
<option value="">----</option>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require "connection.php";
$sql="Select course from checklist group by course;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row["course"].'">'.$row["course"].'</option>';
}
?>
</select>
Year<select id = "year">
<option value="">----</option>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require "connection.php";
$sql="select year from checklist group by year;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row["year"].'">'.$row["year"].'</option>';
}
?>
</select>
Semester<select id = "semester">
<option value="">----</option>
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require "connection.php";
$sql="Select semester from checklist group by semester;";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row["semester"].'">'.$row["semester"].'</option>';
}
?>
</select>
<table border="1">
<?php
$checklist_query=mysql_query("select * from checklist");
while($checklist_rows=mysql_fetch_array($checklist_query)){
?>
<tr>
<td><?php echo $checklist_rows['course'] ; ?></td>
<td><?php echo $checklist_rows['descriptiveTitle'] ; ?></td>
<td><?php echo $checklist_rows['courseNo'] ; ?></td>
<td><?php echo $checklist_rows['lec'] ; ?></td>
<td><?php echo $checklist_rows['lab'] ; ?></td>
<td><?php echo $checklist_rows['units'] ; ?></td>
<td><?php echo $checklist_rows['year'] ; ?></td>
<td><?php echo $checklist_rows['semester'] ; ?></td>
<td>Schedule/s</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</body>
</html>
you're calling header() after you are outputting some text/HTML to the browser, which is a no-no. so you must put this block at top of your codes.
if (isset($_POST['submit'])){
...
}
and there are two duplicates for 'require "connection.php";' in checklist.php
in update query there is no space before where! you need to seperate words with space.
semester='$semester'where
you should use mysql_real_scape_string() to prevent sql injection. ex:
$semester = mysql_real_scape_string($_POST['semester']);
or when you know it's integer:
$id = (int) $_GET['id'];
It's a good practise to use mysqli or PDO instead of mysql, because it's deprecated.

How to print or download the filtered query?

hello I'm super noob in web developing I have this project where in I would like to print or download the filtered query.. here is my code
<form action="adminarchive.php" method="post">
From: <input name="from" type="text" class="tcal"/>
To: <input name="to" type="text" class="tcal"/>
<label for="select"></label>
<select name="status" size="1" id="status">
<option value="Delivered">Delivered</option>
<option value="Cancelled">Cancel</option>
</select>
<input name="search" type="submit" value="Search" />
<input name="print" type="submit" value="Print Report" onclick="window.print()" />
</form><br />
<table width="589px" border="1px"><tr><th>Client</th><th>Item</th><th>Unit Price</th><th>Quantity</th><th>TotalPrice</th><th>Date Ordered</th><th>Status</th><th>TrackNumber</th></tr>
<?php
if (isset($_POST['search']))
{
$a = isset($_POST['from'])?$_POST['from']:"";
$b = isset($_POST['to'])?$_POST['to']:"";
$c = isset($_POST['status'])?$_POST['status']:"";
$rest = mysql_query("SELECT * FROM delivered WHERE dateord BETWEEN '$a' and '$b' AND stats = '$c' order by dateord DESC");
while($row1 = mysql_fetch_assoc($rest)){
?>
<tr align="center" bgcolor="#00FFCC" style="font-size:10px">
<td><?php echo $row1['customer']; ?></td><td><?php echo $row1['itemname'];?></td>
<td><?php echo $row1['unitP']; ?></td><td><?php echo $row1['quant']; ?></td>
<td><?php echo $row1['totalP']; ?></td><td><?php echo $row1['dateord']; ?></td>
<td><?php echo $row1['stats']; ?></td><td><?php echo $row1['tracknumb']; ?></td></tr>
<?php
}
}
?>
</table>
I would like to print the table by pressing the print button just the table. can I possibly print a certain part of a page?
You should read about creating an outfile with your query results. And then you can simply call the link that the query was saved as and generate a download that way.
You can read about generating outfiles with your query here: http://dev.mysql.com/doc/refman/5.7/en/select-into.html
This can create a CSV file which could then be opened using excel or another program displaying a nicely organized report for you.

Categories