Data disappear from from in php after page refresh - php

Data disappear when i reload/refresh the form.
My query for getting data..
public function view_detail()
{
$qry=$this->conn->prepare("SELECT * FROM student_detail WHERE Student_Cnic=:search OR Student_Name=:search");
$qry->bindParam(':search',$this->stsearch);
$qry->execute();
return $qry;
}
The form from where i am sending data
<form action="view.php" method="post" id="view_form">
<br>
<label for="">View By Name or CNIC</label>
<input type="text" class="form-control" name="stu-view" id="stu-view" placeholder="Student Name or CNIC"><br>
<input type="submit" name="view-detail" id="view-detail" class="btn btn-success" value="Enroll"><br>
</form>
The form where i am populating data
<?php
include 'header.php';
include 'config.php';
include 'classes.php';
$database=new Database();
$db=$database->getConnection();
$gtstu=new stu_sys($db);
if(isset($_POST['stu-view']))
{
$_SESSION['stusearch'] = $_POST['stu-view'];
if(isset($_SESSION['stusearch'])){
$gtstu->stsearch = $_SESSION['stusearch'];
}
}
$fth = $gtstu->view_detail();
?>
<div class="row">
<div class="col-lg-12">
<div class="col-lg-2"></div>
<div class="col-lg-8" style="margin-top: 10%;">
<table class="table table-responsive table-bordered">
<th>Image</th>
<th>Name</th>
<th>Cnic</th>
<th>Department</th>
<th>View Detail</th>
<?php while($row = $fth->fetch(PDO::FETCH_OBJ)):?>
<tr>
<td><img src="images/<?php echo $row->Student_Image; ?>" alt=""/></td>
<td><?php echo $row->Student_Name ?></td>
<td><?php echo $row->Student_Cnic ?></td>
<td><?php echo $row->Deprt ?></td>
<td>View More Detail</td>
</tr>
<?php endwhile;?>
</table>
Back TO Dashboard
</div>
<div class="col-lg-2"></div>
</div>
</div>
<?php
include 'footer.php'
?>
i don't know why the form goes empty, mean when i reload the page the data wiped..
Any help will be appreciated.

The form where the data is being populated will never work for one simple reason: You're not including session_start(); at the top of your code. So on page refresh, the sessions are destroyed.
My mistake: I forgot to add session_start() on top of page.

if(isset($_POST['stu-view']))
{
$_SESSION['stusearch'] = $_POST['stu-view'];
}
// Move this out
if(isset($_SESSION['stusearch'])){
$gtstu->stsearch = $_SESSION['stusearch'];
}
I would assume that you are not resending the data in the form. So you are not getting the data from session because you only look at session if the data is sent,

Assuming you are talking about the form you are "sending data from", you should add a value attribute to the textbox (stu-view) and populate it with the submitted search value i.e. add:
value="<?php echo $_SESSION['stusearch']; ?>"

Related

After a Client has been edited/updated how to redirect to a new page to echo the client ID?

I'm wondering if some more experienced PHP coders can help me resolve this issue:
I have a edit.php page which displays current client data to update in form fields.
the update button that when clicked at the moment is just going to a page clients.php (Showing all clients data including the updated client data)
My goal is when the update button is clicked instead of going to clients.php as a redirect and showing a table full of clients, i wish it to redirect to just the individual client info on the report.php page and have it echo the current updated client data in a table.
All i get is a syntax error below.
syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Below is the redirect code on the edit.php page, then below that the reports.php page i want to redirect to but only echo current Client ID's data.
if($run_update){
header("Location: reports.php?cus_id=<?php echo $result['id'] ?>");
keepmsg('<div class="alert alert-success text-center">
×
<strong>Success!</strong> Client updated successfully.
</div>');
} else {
keepmsg('<div class="alert alert-danger text-center">
×
<strong>Sorry!</strong> Client could not be updated.
</div>');
}
Below is the simple reports.php page. This is were I'm trying to redirect to and display only the current clients data by ID
<div class="container">
<div class="jumbotron">
<div class="container">
<div class="row">
<div class="col-md-12">
<small class="pull-right"><a href="edit.php?cus_id=<?php echo $row['id'] ?>" class='btn btn-danger'>Edit Client</a> </small>
<h2 class="text-center invert"> Client Info: <span class="font-weight-light"><?php echo $row['deceased'] ?></span></h2>
<hr>
</div>
</div>
</div>
<br>
<div class="table-responsive table-wrapper">
<table class="table table-bordered table-hover text-center">
<thead>
<tr>
<th class="text-center">Job Type</th>
<th class="text-center">Name of Deceased</th>
<th class="text-center">Plot Number</th>
<th class="text-center">Cemetery</th>
</tr>
</thead>
<tbody class="invert_td">
<tr>
<td><?php echo $row['jobtype'] ?></td>
<td><?php echo $row['deceased'] ?></td>
<td><?php echo $row['plot'] ?></td>
<td><?php echo $row['cemetery'] ?></td>
</tr>
</tbody>
</table>
</div>
</div>
Below if the 1st line of code from my form on the edit.php page if that helps
<form class="form-horizontal" role="form" method="post" action="edit.php?cus_id=<?php echo $client_id ?>">
try header("Location: reports.php?cus_id=".$result['id']);
Hi Mohammed i have a function in my functions.php
Though I'm not sure if this is what you meant by passing in in the header?
function redirect($page){
header("Location: {$page}{$result} ['id']");
}
I created this function yet it still does not take me to the correct client info page
e.g http://localhost:9090/wmdb/admin/reports.php?cus_id=132
only to
http://localhost:9090/wmdb/admin/reports.php?cus_id=

How to display data from selected table in the same database in the form of table, in php?

I am working on a website where I am getting input of semester number from the user and I want to display the subjects in the semester they entered accordingly.
For this, I planned to create tables for each semester which would have the subjects in it.
For now, I am successful to retrieve data from one table. But I am required to get subjects as per the entered semester by the student.
Following is the code,which works perfectly for semester 5. Please suggest alterations as per the requirements asked above.
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- very important -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="subject.css">
</head>
<body>
<h1 class="text-center text-capitalize font-weight-bold">Subject Information</h1>
<div class="container">
<div>
<br>
<div>
<form class="form-inline">
<!-- <div class="mx-auto" style="width: 200px;"> -->
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<br>
</div>
</form>
</div>
</div>
</div>
<div>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= "select * from sem5";
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>
Please suggest alterations. Will be highly obliged.
You need to do a HTTP GET request and use PHP to get the value. Then use PHP to make the SQL query. Here it is in its simplest form.
I've added a submit button which submits the form and executes the HTTP GET with the parameters attached to the URL. You will see ?sem_no=x after it is submitted. That's the Query String.
...
...
<div class="form-group">
<label for="Sem"> Semester: </label>
<div>
<input type="text" name="sem_no" placeholder="Enter current semester" id=sem class="form-control">
</div>
<div>
<input type="submit"/>
</div>
<br>
...
...
...
...
//check the value was submitted and it is not blank
<?php if(isset($_GET['sem_no']) && !empty($_GET['sem_no'])) { ?>
<!-- TABLE DISPLAYED WITH DATA FROM RESPECTIVE DATABASE -->
<div class= "container">
<div class="col-lg-12 ">
<br><br>
<h2 class="text-center">Subjects</h2>
<table class="table table-striped table-hover table-bordered">
<tr>
<th>Subject code</th>
<th>Subject Name</th>
<th>Faculty name</th>
</tr>
<?php
$con = mysqli_connect('localhost','root');
mysqli_select_db($con,'subjects');
$q= 'select * from sem'.$_GET['sem_no'];
$query = mysqli_query($con,$q);
while($res = mysqli_fetch_array($query))
{
?>
<tr>
<td><?php echo $res['subject_no']; ?></td>
<td><?php echo $res['subject_name']; ?></td>
<td><?php echo $res['faculty_name']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<?php
//closing if
} ?>
...
...
As mentioned in the comments earlier, this database design is quite poor. I know you are learning though. Have you considered having a subjects table that has a Semester column? Then you could do a query like:
SELECT * FROM subjects where semester_number = x
There are even better ways to do it beyond that but this would be a good starting point for you.

trying to update the records but failing in php

im trying to a update a record by getting the id of the post via $_GET then update the records through $_POST.
i already have performed the delete action through $_GET it works fine also the mysqli_fetch_assoc works fine for displaying the record for editing but the actual editing does not happens it gives a Empty error from the validation in the code in the empty check function.
i have gone through lots research but cant seem to get my head around the error , i would thank full if any one could suggest any changes in the code.
Thank you in advance!
This is the error
Notice: Undefined index: id in /then the long url etc/
Below is the code
<?php
//DB Connection
include'include/db-conn.php';
if (isset($_POST['edit'])) {
//Raw GET Inputs
$raw_c_id = $_GET['id'];
//Cleaned Inputs
$c_c_id = filter_var($raw_c_id, FILTER_SANITIZE_STRING);
//Error Mwssages
$empty = '<div class="alert alert-danger alert-dismissible">
×
<strong>Error!</strong>Field is empty please provide content!
</div>
';
$success = '<div class="alert alert-success alert-dismissible fixed-top">
×
<strong>Success!</strong> Content Added Successfully
</div>
';
$not_success = '<div class="alert alert-danger alert-dismissible">
×
<strong>Not Success!</strong> Content Not Added Successfully
</div>
';
if (empty($c_c_id)) {
echo $empty;
exit();
header("Location:index.php");
}
$update = "UPDATE `continents`
SET `continent_name`='$c_c_name', `last_edited`='date(d/m/Y)'
WHERE `id`='$c_c_id'";
$run_update = mysqli_query($conn, $update);
if (!$run_update) {
header("Location: index.php");
echo $not_success;
}
else{
header("Location: index.php");
echo $success;
}
}
?>
This is the html part
<div class="panel-body">
<form action="edit.php" method="POST">
<div class="form-group">
<label for="continent_name">Continent Name</label>
<input required type="text" placeholder="10" class="form-control" value="<?php echo $c_name ; ?>" name="continent_name">
</div>
<small>Date Added: <?php echo $c_dated_added ; ?></small> / <small>Last Edited: <?php echo $c_last_edited ; ?></small>
<div class="form-group">
<input class="form-control btn-success" type="submit" name="edit" value="Submit">
</div>
</form>
</div>
Thid the while loop
<div class="table-responsive">
<table id="example" class="table table-hover ">
<thead>
<tr class="">
<th>ID</th>
<th>Continent Name</th>
<th>Date Added</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$all_continents = "SELECT * FROM `continents` ORDER BY `status`";
$run = mysqli_query($conn,$all_continents);
while ($row_result = mysqli_fetch_assoc($run)) {
$id = $row_result['id'];
$c_continent_name = $row_result['continent_name'];
$c_date_added = $row_result['date_added'];
$c_status = $row_result['status'];
echo "
<tr>
<td>$id</td>
<td>$c_continent_name</td>
<td>$c_date_added</td>
<td>$c_status</td>
<td>
<a class='btn btn-info' href='edit.php?id=$id'>Edit</a>
</td>
<td>
<a class='btn btn-danger' href='delete.php?id=$id'>Delete</a>
</td>
</tr>
";
}
?>
</tbody>
</table>
It looks like you're trying to use GET and POST parameters at the same time. The reason it's not working is because the GET parameter is lost when you submit your form. You need to pass it on in the form's action attribute:
<form action="edit.php?id=<?php echo $_GET['id'] ?>" method="POST">
Please also take a look at the advice in this post:
Is there a way to use GET and POST together?

Add or Remove Out Of Stock Data From MySql Output in Php

I need to apply WHERE status!='disable' in my php code at $result after I click on the checkbox and then on uncheck of the checkbox it should be removed again and both enable and disable datas from MySql should be shown in the php, is that possible without using AJAX codes, if it is then how can I do that. Can anyone find where am I going wrong
Checkbox, code given below:
<h4 id="CATEGORIESfilters">Availability</h4>
<table border="0" cellspacing="0" id="Availabilitytable">
<tr>
<td>
<input type="checkbox" name="Availability" id="Availabilitycheckboxexcludeoutofstock" >
</td>
<td>
<label for="Availabilitycheckboxexcludeoutofstock">Exclude Out Of Stock</label>
<td>
</tr>
</table>
Php Code is given below:
<?php
include "db_connection.php";
if(isset($_REQUEST['Availability']))
{
$availability="WHERE status!='disable'";
}
else
{
$availability="";
}
$result = mysql_query("SELECT * FROM burger $availability ORDER BY product_no_id DESC");
while($data=mysql_fetch_array($result)):
if($data['status']=="enable")
{
?>
<div class="productwrap">
<div id="Instockwrap">
In Stock
</div>
</div>
<?php
}
else
{
?>
<div class="productwrap">
<div id="Outofstockwrap">
Out Of Stock
</div>
</div>
<?php
}
endwhile;
?>
you can add a class to the out of stock items and hide them via jquery if checkbox is checked, so you dont have to reload the page on checkbox changes

while() - not looping - PHP

I have a little problem with my PHP while loop. I am looping out all the available membership packages (and it's details) from my database.
My code is like this:
<?php
while($mp = mysql_fetch_assoc($s)):
?>
<tr class="hover">
<td class="name" width="30%"><?php echo $mp['membershipname']; ?></td>
<td class="price" width="30%">$<span><?php echo $mp['ap_price']; ?></span>/<span><?php echo $mp['duration']; ?></span> days.</td>
<?php if($userdata['membership']>$mp['membershipid']): ?>
<td width="40%" class="purchase"></td>
<?php else: ?>
<td width="40%" class="purchase" >
Pricing
</tr>
<?php endif; ?>
<div style='display:none'>
<div id='inline_content' style='padding:10px; background:#fff;'>
<div style="text-align:center;font-weight:bold">Please select your payment method:</div>
<div style="text-align:center">
<br />
<?php
if($sdata['allow_paypal'] == 1 && $mp['pp_price']>0 && $userdata['paypal']!=""): ?>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="float:left;" >
<input type="submit" name="submit" class="click cblue" value="PayPal - $<?php echo $mp['pp_price']; ?>">
</form>
<?php endif;
if($sdata['allow_alertpay'] == 1 && $mp['ap_price']>0 && $userdata['alertpay']!=""):
//Do not change any input fields, OTHER than return URL (if needed)
?>
<form method="post" name="ap" action="https://www.alertpay.com/PayProcess.aspx" style="float:right;" >
<input type="submit" name="submit" class="click cgreen" value="AlertPay - $<?php echo $mp['ap_price']; ?>" />
</form>
<?php
endif; ?>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$("#upg<?php echo $mp['id']; ?>").colorbox({inline:true, width:"30%",href:"#inline_content"});
});
</script>
<?php endwhile; ?>
As you can see in the above code, I am doing a while loop.
My problem is that the $mp['pp/ap_price'] inside the #inline_content is not looping. It only take the price from the first row. Although, it is looping in the table.
What's the issue here? I tried to do another loop inside the #inline_content, but it didn't work.
HTML IDs should be unique to one DOM element.
You have a DOM element #inline_content for every record in your database query result. Then, when you try to use Javascript to display them all, only one is displayed because they all have the same ID.
HTML and Javascript are not aware of your PHP loop.
Consider using a class attribute instead of an id attribute.

Categories