how to refresh tabs data when update table? - php

index.php
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Engineering')">Engineering</button>
<button class="tablinks" onclick="openCity(event, 'LAW')">LAW</button>
</div>
<div id="Engineering" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'engineering'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
<div id="Law" class="tabcontent">
<table class="items">
<tr>
<th>State</th>
<th>College Name</th>
</tr>
<?php
$query = "select * from college where field = 'law'";
$show = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($show))
{
?>
<tr>
<td><?php echo $fetch['state']?></td>
<td><?php echo $fetch['college_name']?></td>
<td>
edit
</td>
</tr>
<?php
}
?>
</table>
</div>
edit.php
<?php
if(isset($_POST['update']))
{
$college_name = $_POST['colleges'];
$state = $_POST['state'];
$sqli = "update college set college_name = '$college_name', state = '$state' where id = '$id'";
$results = mysqli_query($link,$sqli);
if($result == true)
{
$msg .= "<p style='color:green;'>Your data update successfully</p>";
}
else
{
$msg .= "<p style='color:red;'>Errror!</p>";
}
}
?>
<form method="POST" enctype="multipart/form-data" >
<select name="state" id="state">
<option value="<?php echo $stateid; ?>"><?php echo $statename; ?></option>
<option value="">Select State</option>
<?php
$sql = "select * from statemaster";
$result = mysqli_query($link,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['stateid'].">".$row['statename']."</option>";
}
?>
</select>
<select name="colleges" id="colleges">
<option value="<?php echo $college_name; ?>"><?php echo $college_name; ?></option>
<option value="">Select College</option>
</select>
<button type="submit" name='update' id='update'>update</button>
</form>
In this code when I click on edit button then it will go to edit.php page where I get id from url and run update query after updating table college the data will update but when I move from edit page to index.php page the data will remain same but in database update data will be there. So, How can I fix this issue ?
Thank You

Check for caching. It could be that the browser is not going to the server in order to get the contents of index.php, as it thinks it has it.
Try calling index.php with a variable, like:
Home

Related

While loop finding the last iteration only

*How to solve this..??
My while loop is finding the last iteration only, If I select last attendance A then all the student's values will be 0, if I select P then all the values will be 1. So, how can I solve this?*
Index.php file
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
?>
<table border="1">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Attendance</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</td>
</tr>
<?php
}
?>
</table>
<br>
<button type="submit">Submit</button>
</form>
attendance.php
<?php
include'connection.php';
$sql = "SELECT * FROM `student_info`";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if(isset($_POST['atten'])){
$atten = $_POST['atten'];
echo $atten.'<br>';
}
}
Here is output
Image for Index.php file
Image for attendance.php file
You're not submitting the student ID in the form. You can use a hidden input for this.
<?php
while($row=mysqli_fetch_assoc($result))
{
$id = $row['stu_id'];
?>
<tr>
<td><?php echo $row['stu_id'];?></td>
<td><?php echo $row['first_name'];?></td>
<td><?php echo $row['last_name'];?></td>
<td>
<form action="attendance.php" method="post">
<input name="stu_id" type="hidden" value="<?php echo $row['stu_id'];?>">
<select name="atten" id="">
<option value="1">P</option>
<option value="0">A</option>
</select>
</form>
</td>
</tr>
<?php
}
?>
Then in attendance.php you can update that student.
<?php
include'connection.php';
$stmt = $conn->prepare("UPDATE `student_info` set atten = ? WHERE stu_id = ?");
$stmt->bind_param("ii", $_POST['atten'], $_POST['stu_id']);
$stmt->execute();
echo "Attendance for student {$_POST['stu_id']} updated";

PHP update multiple rows on submit

Hi I have echoed out multiple rows from a database. Each row has a select box which echoes out what is currently in that field related to that row.
With the select box I can select a different option. On submit nothing is happening, no errors. What it should do is update those fields. I have never managed to update multiple rows before so this is new to me.
The first query below is the update I'm trying to achieve to update all the fields with order_ref
The second query just echoes out all the data required to view.
<?php
ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>
<form>
<table class="tbl-qa">
<thead>
<tr>
<th class="table-header"><p>Action</p></th>
<th class="table-header"><p>Campaign</p></th>
<th class="table-header"><p>Title</p></th>
<th class="table-header" width="100px"><p>Order Reference</p></th>
<th class="table-header"><p>Last updated</p></th>
</tr>
</thead>
<tbody>
<?php
require_once("../db/db_connection.php");
if (isset($_POST['submit'])) {
$sql = $db->prepare("UPDATE articles SET order_ref=? WHERE id=?
");
$order_ref = $_POST['order_ref'];
$sql->bind_param("ii", $order_ref, $_GET["id"]);
if($sql->execute()) {
$success_message = "Edited Successfully";
} else {
$error_message = "Problem in Editing Record";
}
}
$sql = $db->prepare("SELECT * FROM articles WHERE campname=? ORDER BY order_ref ASC
");
$sql->bind_param("s",$_GET["campname"]);
$sql->execute();
$result = $sql->get_result();
?>
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td class="table-row" width="100px"><div class="edit">Edit |</div><div class="bin"><a href="delete.php?id=<?php echo $row["id"]; ?>" class="link"><img content="delete" id="delete" title="Delete" onclick="return confirm('Are you sure you want to delete?')" src="/_admin/images/delete.png"/></div>
</a>
</td>
<td>
<?php echo $row["campname"]; ?>
</td>
<td class="table-row"><?php echo $row["art_title"]; ?></td>
<td class="contentedit">
<select name="order_ref">
<option value="<?php echo $row["order_ref"]?>"selected><?php echo $row["order_ref"]?></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td class="table-row"><?php echo strftime("%b %d, %Y", strtotime($row["last_updated"])); ?></a></td>
<!-- action -->
</tr>
<?php
}
}
else {
echo "No results";
}
?>
<tr class="table-row">
<td colspan="5"><input name="submit" type="submit" value="Update" class="submit"></td>
</tr>
</tbody>
</table>
</form>
What do I need to do in order to update order_ref for every row that's echoed out?

onchange dropdown show checkbox is checked in php

code:
<script>
$(document).ready(function(){
$(".menu").click(function(){
ids = $('.menu:checked').map(function() {
return this.id;
}).get().join(',');
console.log(ids);
$("#ids").val(ids);
});
});
</script>
<?php
if(isset($_POST['submit']))
{
$adminid = $_POST['admin'];
$menuids = explode(",", $_POST['ids']);
foreach ($menuids as $idd)
{
$sql = "update menu set admin_id = concat(admin_id,'$adminid',',') where id = '$idd'";
$result = mysqli_query($link,$sql);
}
if($result == true)
{
$msg .= "<p style='color:green'>successfull</p>";
}
else
{
$msg .= "<p style='color:red'>error!</p>";
}
}
?>
<form method="post">
<select name="admin" id="admin">
<option value="">---Select Admin---</option>
<?php
$sql = "select * from admin";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['firstname']?></option>
<?php
}
?>
</select>
<table>
<tr>
<th>Share</th>
<th>Menu Name</th>
</tr>
<?php
$query = "select * from menu";
$results = mysqli_query($link,$query);
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" id="<?php echo $fetch['id']; ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>
</table>
<input type="text" name="ids" id="ids" value=""/>
<input type="submit" name="submit" id="submit" />
</form>
In this code I am update a table having name menu in database. Now, I want to check only those checkbox where admin_id like ,1, or ,2, which is update by query. How can I fix this issue ?please please help.
Thank You
while ($fetch = mysqli_fetch_array($results))
{
?>
<tr>
<td>
<input type="checkbox" class="menu" value="<?php if($fetch['id']==1 or
$fetch['id']==2 ) { echo "checked";} else{} ?>" name="menuid" />
</td>
<td>
<?php echo $fetch['menu_name']; ?>
</td>
</tr>
<?php
}
?>

linked data selection option with ajax & php

I am trying to populate Material Description from the Material Number. Bot hte values are stored in same SQL tasble. So what I want when I select Material Maiterial Description shpul auto populate.
Fileds in table are Material & MaterialDescripotion
Below is code in in main file where data is fetched
<?php
include_once "dbConnect.php";
$sql = "SELECT * FROM DRLINK";
$result2 = mysqli_query($conn, $sql);
if (!$result2) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
$options = "";
while($row2 = mysqli_fetch_array($result2))
{
$options = $options."<option>$row2[1]</option>";
}
?>
<html>
<!DOCTYPE html>
<head>
<title>Dropdown Ajax</title>
</head>
<body>
<form action ="DSSTRsubmit.php" method="POST">
<table border="1">
<tr>
<td>Select Retailer</td>
</tr>
<tr>
<td>
<?php
echo "<select>";
echo $options;
echo "</select>";
?>
</td>
</tr>
</table>
<br/>
<br/>
<br/>
<table border="1">
<tr>
<td>Material</td>
<td>Material Description</td>
<td>Quantity</td>
<td>Unit of Measure</td>
</tr>
<tr>
<td>
<div class="Material">
<select name="Material" onchange="getId(this.value);">
<option value="">Select Country</option>
<?php
$query ="SELECT * FROM MATERIALLIST";
$results = mysqli_query($conn, $query);
foreach($results as $MATERIALLIST) {
?>
<option value="<?php echo $MATERIALLIST["Material"];?>"><?php echo $MATERIALLIST["Material"];?></option>
<?php
}
?>
</select>
</div>
</td>
<td>
<div class="MaterialDescription">
<select name="MaterialDescription" id="DesList">
<option value=""></option>
</select>
</div>
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
function getId(val){
$.ajax({
type:"POST",
url:"getdata1.php",
data:"Material="+val,
success: function(data){
$("#DesList").html(data);
}
});
}
</script>
</td>
<td><input type="text" name="dquantity_name" /> </td>
<td><input type="text" name="duom_name" /> </td>
</tr>
</table>
<legend> </legend>
<p> <button type="submit" class="pure-button pure-button-primary">Send Stock</button>
<br>
<br>
<?php
echo "Distributor Page";
?>
</body>
</html>
below is the getdata1.php
<?php
include_once "dbConnect.php";
if(!empty($_POST["Material"])){
$Material= $_POST["Material"];
$query = "SELECT * FROM MATERIALLIST WHERE Material = $Material";
$results = mysqli_query($conn,$query);
foreach($results as $MaterialDescription){
?>
<option value="<?php echo $Des["Material"];?>"><?php echo $materialDescription ["MaterialDescription"];?></option>
<?php
}
}
?>
Iam able to select the material but on selection of material no material description auto populates.
Thanks for the help
PHP is case sensitive. Be careful about naming variables.
foreach contains upper case variable, while echo - lowercase. Also in echo you have space after variable and before opening bracket.
$MaterialDescription
$materialDescription_["MaterialDescription"]

Can't display data from database

I have 3 tables in my database (standards, courses, students). I want to display Fname and gender from students table enrolled in selected 'standard' and 'course' from dropdown - but the "submit" button doesn't seem to work. The code is as shown below. I am sure it is connected to the database as the dropdowns for 'courses' and 'standards' work:
<html>
<head>
<title>Courses</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="container">
<div id="wrapper">
<h1> Students</h1>
<div id="data">
<form action="index.php" method="POST">
<select name="standards">
<option>Standard</option>
<?php
include 'includes/dbconnect.php';
$query1 = "SELECT * FROM standards";
$result1 = mysql_query($query1);
while($rows1 = mysql_fetch_array($result1)){
$standardID = $rows1['id'];
$rowsData1 = $rows1['standardName'];
?>
<option value="<?php echo $standardID; ?>">
<?php
echo $rowsData1; ?></option>
<?php
}
?>
</select>
</div>
<div id="data2">
<select name="courses">
<option>Courses</option>
<?php
$query2 = "SELECT * FROM courses";
$result2 = mysql_query($query2);
while($rows2 = mysql_fetch_array($result2)){
$coursesID = $rows2['id'];
$rowsData2 = $rows2['courseName'];
?>
<option value="<?php echo $courseID;?>">
<?php echo $rowsData2; ?></option> <?php }?>
</select>
</div>
<div id="submit">
<input type="submit" name="submit" id="submit" value="submit"/>
<table border="1" id="table1">
<tr>
<th>Student Name</th>
<th>Gender</th>
</tr>
<?php
if(isset($_POST['submit'])){
$standardName = $_POST['standards'];
$courseName = $_POST['courses'];
$query3 = "SELECT students.Fname, students.gender
FROM students
WHERE students.standardID = '$standardName'
AND students.courseID = '$courseName'";
$result3 = mysql_query($query3);
while($rows3 = mysql_fetch_array($result3)){
//$dataID = $rows3['id'];
$studentName = $rows3['FName'];
$gender = $rows3['gender'];
?>
<tr>
<td><?php echo $studentName; ?></td>
<td><?php echo $gender; ?></td>
<tr>
<?php
}
}
?>
</table>
</div>
</div>
</body>
</html>
Inscribe the input elements within a form tag with post as method. Input elements with type submit are used for submission of forms.
You only do anything with the submitted form data if that data includes a submit field.
if(isset($_POST['submit'])){
However you have:
<input type="submit" name="submit" id="submit"/>
Your submit button don't have a value, so nothing will be included in the form data for it.

Categories