trying to update the records but failing in php - 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?

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 store the old data of a table before update query

How can I store the old value of the selected ID in a table before update query? I have this code for my insert query and update query . I want to save the old value of the selected ID before my UPDATE query.
<form method="post" id="reg-form" action="user_save">
<div class="box-body">
<div class="row">
<div class="col-md-12">
<table id="example1" class="table table-bordered table-striped" style="margin-right:-10px">
<thead>
<tr>
<th>RECRUITER</th>
</tr>
</thead>
<?php
include('../dist/includes/dbcon.php');
$bdo=$_SESSION['name'];
$query=mysqli_query($con,"SELECT * FROM accounts_at ")or die(mysqli_error());
while($row=mysqli_fetch_array($query)){
$id=$row['id'];
$rec=$row['rec'];
<tr>
<td><?php echo $rec;?></td>
<td>
<a id="click" href="user_edit.php?id=<?php echo $id;?>">
<i class="glyphicon glyphicon-edit text-blue"></i></a>
</td>
And this is my user_update query .
<?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index');
endif;
include('../dist/includes/dbcon.php');
$id = mysqli_real_escape_string($con, $_POST['id']);
$rec= mysqli_real_escape_string($con,$_POST['rec']);
mysqli_query($con,"UPDATE accounts_at set rec='$rec' where id='$id'")or
die(mysqli_error());
echo "<script type='text/javascript'>alert('Successfully updated account
details!');</script>";
echo "<script>document.location='update_acc'</script>";
?>
it is dependent on how you want to save this data ?
you can save it in file as you can use fopen() and write it first
or send it to your self via mail()
$save_method = //method you want to save data
if($save_method){//code to update data}

correct syntax in a php/mysql code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am struggling with the syntax if someone can help me.
Condition1 = if the value of $ncha['p_no_contacts']-$ncha['r_cnt'] is 1 then add an extra echo in the h1 section saying that, "You are viewing last contact details and have exhausted your membership. Please renew your membership" If the above value is more than 1 then let that one echo statement be as it is.
next thing is to update the payments table or delete the row from payments table if $inc1 is equal to $ncha['p_no_contacts']
I need a correct syntax if someone can help.
EDIT:
This is what I tried but I get error Parse error: syntax error, unexpected '>' on line 51 which is the h1 line of else statement.
Thanks
<div class="modal-dialog yoyo-large">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×
</button>
if $ncha['p_no_contacts']-$ncha['r_cnt'] = 1
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']);?>)
</h1>
}
else
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']) <br> You are viewing last contact details and have exhausted your membership. Please renew your membership;?>)
</h1>
</div>
<div class="modal-body">
<div class="col-sm-12 form-group">
<div class="col-sm-6" style="font-size:13px;">
<table class="table table-hover table-striped">
<tr height="30">
<td width="80">
<strong>Matri ID :
</strong>
</td>
<td>
<?php echo $fet['matri_id']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Name :
</strong>
</td>
<td>
<?php echo $fet['username']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Address :
</strong>
</td>
<td>
<?php echo $fet['address']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Phone :
</strong>
</td>
<td>
<?php echo $fet['phone']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Mobile :
</strong>
</td>
<td>
<?php echo $fet['mobile']; ?>
</td>
</tr>
<tr height="30">
<td>
<strong>Email :
</strong>
</td>
<td>
<?php echo $fet['email']; ?>
</td>
</tr>
</table>
</div>
</div>
<?php
$chk1=$ncha['r_cnt'];
$inc1=$chk1+1;
If $inc1 = $ncha['p_no_contacts']
(
$dele="delete from payments where (pemail='$mid' or pmatri_id='$mid')";
$de=mysql_query($dele) or die(mysql_error());
}
else
{
$upda="update payments SET r_cnt='$inc1' where (pemail='$mid' or pmatri_id='$mid')";
$up=mysql_query($upda) or die(mysql_error());
}
$ex=mysql_query("select id from today_contact where who='$mid' and whose='$from_id'");
if(mysql_num_rows($ex)==0)
{
mysql_query("insert into today_contact (who,whose,on_date) values ('$mid','$from_id',now())");
}
else
{
mysql_query("update today_contact set on_date=now() where who='$mid' and whose='$from_id'");
}
?>
</div>
</div>
</div>
You are mixing HTML and PHP code together.
You also forgot parentheses around the condition on the first line of this code fragment.
Change:
if $ncha['p_no_contacts']-$ncha['r_cnt'] = 1
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']);?>)
</h1>
}
else
{
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
to:
<?php
if ($ncha['p_no_contacts']-$ncha['r_cnt'] = 1)
{
?>
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
<?php echo ($ncha['p_no_contacts']-$ncha['r_cnt']);?>)
</h1>
<?php
}
else
{
?>
<h1 class="modal-title" id="myModalLabel" style="color:red;">Remaining Contacts (
Other important things:
- mysql_* functions in PHP are all deprecated, and removed from the last version of PHP (PHP 7). You should use mysqli_* or PDO functions instead
- it seems you are using unescaped variables in your queries. This is very insecure and may cause your code to fail, or be the target of SQL injections. Read How can I prevent SQL-injection in PHP? for more information.
I got the answer. By mistake I added a parenthesis instead of brace bracket in the second line of the below code. Phew! It took me almost two days to figure this out. Thanks #Jocelyn for your kind assistance.
If $inc1 = $ncha['p_no_contacts']
(
$dele="delete from payments where (pemail='$mid' or pmatri_id='$mid')";
$de=mysql_query($dele) or die(mysql_error());
}
else
{
$upda="update payments SET r_cnt='$inc1' where (pemail='$mid' or pmatri_id='$mid')";
$up=mysql_query($upda) or die(mysql_error());
}

Data disappear from from in php after page refresh

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']; ?>"

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

Categories