What the error of update function? - php

I development admin control panel in my system when i update some thing in category the data not updated. what the error of my code? for more help you can download my files from here http://www.mediafire.com/download/m9psmr55pib5555/update_category.rar
<form action="categories.php" method="post">
<label>Edit category</label>
<?php
/*start of select data */
if(isset($_GET['edit_cat'])){
$cat_id = $_GET['edit_cat'];
$update_cat ="SELECT * FROM categories WHERE cat_id= $cat_id";
$run_cat_id = mysqli_query($connect , $update_cat);
while($row =mysqli_fetch_assoc($run_cat_id)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_name'];
?>
<div class="form-group">
<!--to pick the value -->
<input value="<?php if(isset($cat_title)){echo $cat_title;} ?>" type="text" class="form-control" name="cat_name">
</div>
<?php }}?>
<?php
if(isset($_POST['update'])){
$cat_update = $_POST['cat_name'];
$query ="UPDATE categories SET cat_name ='{$cat_update}' WHERE cat_id ={$cat_id}";
$update_query= mysqli_query($connect , $query);
<?php
ob_start();
include("includes/header.php");
include("includes/db.php");
?>
<div id="page-wrapper">
<div class="container-fluid">
<?php
if(isset($_GET['edit_cat'])){
$cat_id = $_GET['edit_cat'];
include ("includes/update_category.php");
}
?>
</div>
<!-- end of category form -->
</div>
<!--
===========================================================================================================
-->
<!-- start of table to show categories-->
<div class="col-lg-6">
<div class="btn btn-success"><h3>categories</h3></div>
<div class="table-responsive" >
<table class="table table-bordered table-hover">
<thead>
<tr>
<th width="10px">Id</th>
<th width="400px">category title</th>
</tr>
</thead>
<tbody>
<?php
/*Select from DB to show categories in table*/
$i=1;
$query = "SELECT *FROM categories";
$run_cat = mysqli_query($connect , $query);
while($row = mysqli_fetch_assoc($run_cat)){
$cat_id = $row['cat_id'];
$cat_name =$row ['cat_name'];
?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $cat_name; ?></td>
<td><div class='btn btn-warning' name="edit_cat"><i class='fa fa-pencil-square-o fa-2'></i> Edit</div></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<!-- end of table to show categories-->
<!-- =============================================================================
-->
</div>
</div>
</div>
</body>
</html>
if(!$update_query){
die("QUERY FAILED".mysqli_error($connect));
}
}
?>
<div class="form-group">
<input type="submit" name="update" value="submit" class="btn btn-success"/>
</div>
</form>

Related

How to Hide or Show an included with PHP

I'm trying to hide an included file with a condition.
<?php include('header_dashboard.php'); ?>
<?php include('session.php'); ?>
<?php $get_id = $_GET['id']; ?>
<?php
$post_id = $_GET['post_id'];
if($post_id == ''){
?>
<script>
window.location = "assignment_student.php<?php echo '?id='.$get_id; ?>";
</script>
<?php
}
?>
<body id="studentTableDiv">
<?php include('navbar_student.php'); ?>
<div class="container-fluid">
<div class="row-fluid">
<?php include('assignment_link_student.php'); ?>
<div class="span6" id="content">
<div class="row-fluid">
<!-- breadcrumb -->
<?php $class_query = mysqli_query($conn, "select * from teacher_class
LEFT JOIN class ON class.class_id = teacher_class.class_id
LEFT JOIN subject ON subject.subject_id = teacher_class.subject_id
where teacher_class_id = '$get_id'")or die(mysqli_error());
$class_row = mysqli_fetch_array($class_query);
?>
<ul class="breadcrumb">
<li><?php echo $class_row['class_name']; ?> <span class="divider">/</span></li>
<li>Asignaciones <span class="divider">/</span></li>
<li><b>Subir Asignación</b></li>
</ul>
<!-- end breadcrumb -->
<!-- block -->
<div id="block_bg" class="block">
<div class="navbar navbar-inner block-header">
<div id="" class="muted pull-left"></div>
</div>
<div class="block-content collapse in">
<div class="span12">
<?php
$query1 = mysqli_query($conn, "select * FROM assignment where assignment_id = '$post_id'")or die(mysqli_error());
$row1 = mysqli_fetch_array($query1);
?>
<div class="alert alert-info">Subir respuesta de asignacion en : <?php echo $row1['fname']; ?></div>
<div id="">
<table cellpadding="0" cellspacing="0" border="0" class="table" id="">
<thead>
<tr>
<th>Fecha Subida</th>
<th>Nombre de Archivo</th>
<th>Descripción</th>
<th>Enviado por:</th>
<th>Calificación</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($conn, "select * FROM student_assignment LEFT JOIN student on student.student_id = student_assignment.student_id where assignment_id = '$post_id' order by assignment_fdatein DESC")or die(mysqli_error());
while($row = mysqli_fetch_array($query)){
$id = $row['student_assignment_id'];
$student_id = $row['student_id'];
?>
<tr>
<td><?php echo $row['assignment_fdatein']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['fdesc']; ?></td>
<td><?php echo $row['firstname']." ".$row['lastname']; ?></td>
<?php if ($session_id == $student_id){ ?>
<td>
<span class="badge badge-success"><?php echo $row['grade']; ?></span>
</td>
<?php }else{ ?>
<td></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- /block -->
</div>
</div>
<?php include('submit_assignment_sidebar.php') ?>
</div>
<?php include('footer.php'); ?>
</div>
<?php include('script.php'); ?>
</body>
</html>
<?php include('submit_assignment_sidebar.php') ?>
OK what I'm trying to do is move this include that is at the end
<?php include('submit_assignment_sidebar.php') ?>
To:
<?php if ($session_id == $student_id){ ?>
<td>
<span class="badge badge-success"><?php echo $row['grade']; ?></span>
</td>
<?php }else{ ?>
<td></td>
<?php include('submit_assignment_sidebar.php') ?>
<?php } ?>
But it doesn't work. Doesn't include de sidebar file. Any sugestion?

How to initiate if else statement inside a td tag

In my database, I have a table column that has a filepath for images. The filepath's name is photo Some of my rows don't have a filepath. Inside my while loop forshowing the table, I would like to add a condition that if there is no filepath, it would prompt the text "User did not upload a photo yet." and when it has filepath, I can show the filepath and link it with a target blank.
This is my php file for it.
<?php require_once 'process.php';
session_start();
$role = $_SESSION['sess_userrole'];
$name = $_SESSION['sess_name'];
if(!isset($_SESSION['sess_username']) && $role!="admin"){
header('Location: index.php?err=2');
}
?>
<html>
<head>
<title>User Accounts</title>
<link rel="icon" href="isalonlogo.png">
<link rel="stylesheet" href="css2/bootstrap.min.css">
<script src="css/jquery-3.3.1.slim.min.js"></script>
<script src="css/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="css/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_SESSION['message'])):?>
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset ($_SESSION['message']);?>
</div>
<?php endif ?>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<span class="navbar-brand" href=""><?php echo " " . "$name"?>, here are the Stylist user lists.</span>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>User Lists</li>
<li><?php echo $_SESSION['sess_username'];?></li>
<li>Logout</li>
</ul>
</div>
</div>
</div>
<br> <br> <br>
<div class="container">
<?php
$mysqli = new mysqli("localhost","id7508046_root","123123123as","id7508046_isalon") or die(mysqli_error($mysqli));
$result = $mysqli->query("SELECT * FROM stylist ") or die($mysqli->error);
?>
<div class="row justify-content-center" width="80%">
<table class="table">
<thead>
<tr>
<th>UserName</th>
<th>Name</th>
<th>Image</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['name'] ?></td>
// THIS PART HERE IS THE PROBLEM
<td><?php if(strcmp($row['photo'],"") == 0): {echo 'Ola';
}else:
{echo '<img src="'.$row['photo'].'">';
}
endif;
?></td>
<td>
<a href="userlist.php?edit=<?php echo $row['stylist_id']; ?>"
class="btn btn-info">Edit</a>
<a href="process.php?delete=<?php echo $row['stylist_id']; ?>"
class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php
function pre_r($array){
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
<br>
<br>
<div class="container justify-content-center">
<h5 class=" justify-content-center">Admin <?php echo $name;?>, create or edit an account here.</h5>
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label>UserName</label>
<input type="text" name="username" class="form-control" value="<?php echo $username;?>" placeholder="Enter new user Username" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="text" name="password" class="form-control" value="<?php echo $password;?>" placeholder="Enter new user Password" required>
</div>
<div class="form-group">
<?php
if($update == TRUE): ?>
<button type="submit" class="btn btn-primary" name="update">Update</button>
<input type="button" class="btn btn-primary" name="reset" value="Reset" onclick="window.location.href='stylistUserlist.php'">
<?php
else: ?>
<button type="submit" class="btn btn-primary" name="save">Save</button>
<input type="button" class="btn btn-primary" name="reset" value="Reset" onclick="window.location.href='stylistUserlist.php'">
<?php
endif; ?>
</div>
</form>
</div>
</div>
</body>
Try
<td>
<?php if(!empty(trim($row['photo']))): ?>
<img src="<?php echo $row['photo'];?>"/>
<?php else: ?>
User did not upload a photo yet
<?php endif; ?>
</td>
Use below code for TD as you currently added same code in if and else both instead of using if else you can use the ternary operator.
<td>
<?php $photo = (isset($row['photo'])) ? $row['photo'] : "";
echo ($photo) ? "'. <img src=".$photo."> .'" : ""; ?>
</td>

when i click edit button nothing show in the edit form this the error

I am getting the following error:
http://localhost/sites/admin_panel/student_center_system/controller/admin/editmaster.php?item_id=Notice: Undefined variable: row in C:\xampp\htdocs\sites\admin_panel\student_center_system\controller\admin\viewitems.php on line 49
when i click edit button on viewitem.php nothing shows up in the form it is empty this viewitems.php The database connection is working perfect but the problem is the edit form .Ihave been stack for two days with this problem about edit form not working.could someone help me please i will appreciate thank you.
<?php
include "navigation.php";
include "header.php";
include "footer.php";
?>
<?php
include("../dbConfig.php");
$query = "SELECT item_id,item_name,description,item_category,pic FROM lost_items_table";
$returnD = mysql_query($query);
$returnD1 = mysql_query($query);
$result = mysql_fetch_assoc($returnD);
?>
<!-- Page Content -->
<div id="page-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">View Items</h1>
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">View Items</div>
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<tr>
<th>Item Id</th>
<th>Item Name</th>
<th>Description</th>
<th>Item category</th>
<th>Picture</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
while($result1 = mysql_fetch_assoc($returnD1)){
?>
<tr>
<td>
<?php echo $result1['item_id']; ?>
</td>
<td><?php echo ucfirst($result1['item_name']); ?></td>
<td><?php echo ucfirst($result1['description']); ?></td>
<td><?php echo ucfirst($result1['item_category']); ?></td>
<td><?php echo ucfirst($result1['pic']); ?></td>
<td>
Edit
</td>
<td>
Delete
</td>
</tr>
<?php
}
?>
</table>
<!-- /.table-responsive -->
</div>
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
</div>
</div>
this is editmaster.php file.this is the form for editing data and nothing is showing up in the form its empty.what could be wrong with my code .
<?php session_start();
include("../dbConfig.php");
include "navigation.php";
include "header.php";
include "footer.php";
{
}
$item_id = '';
$item_name = '';
$description = '';
$item_category = '';
$pic = '';
if (isset($_POST['updateitem']))
{
if (is_numeric($_POST['item_id']))
{
$item_id = $_POST['item_id'];
$item_name = mysql_real_escape_string(htmlspecialchars($_POST['item_name']));
$description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
$item_category = mysql_real_escape_string(htmlspecialchars($_POST['item_category']));
$pic = mysql_real_escape_string(htmlspecialchars($_POST['pic']));
if ($item_name == '' || $description == '' || $item_category == '' || $pic == '')
{
$error = 'ERROR: Please fill in all required fields!';
valid($item_id, $item_name, $description, $item_category, $pic, $error);
}
else
{
mysql_query("UPDATE lost_items_table SET item_name = '$item_name', description = '$description', item_category = '$item_category', pic = '$pic' WHERE item_id = '$item_id'")
or die(mysql_error());
header("Location: viewitems.php");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['item_id']) && is_numeric($_GET['item_id']) && $_GET['item_id'] > 0)
{
$item_id = $_GET['item_id'];
$result = mysql_query("SELECT * FROM lost_items_table WHERE item_id=$item_id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$item_name = $row['item_name'];
$description = $row['description'];
$item_category = $row['item_category'];
$pic = $row['pic'];
valid($item_id, $item_name, $description, $item_category, $pic,'');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
<!-- Page Content -->
<div id="page-wrapper">
<div class="container-fluid">
<!-- /.row -->
<div class="row">
<!--div class="col-md-8 col-md-offset-4"-->
<div class="col-lg-6">
<div class="Register-panel panel panel-default">
<!--div class="panel panel-default"-->
<div class="panel-heading">
Edit Item
</div>
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="panel-body">
<form role="form" action="adminPage.php">
<input type="text" name="item_id" value=<?php echo $item_id; ?> readonly><br>
<input type="text" name="item_name" required autofocus placeholder="Item-Name" value=<?php echo $item_name; ?>><br>
<input type="text" name="description" required autofocus placeholder="Description" value=<?php echo $description; ?>><br>
<input type="text" name="item_category" required autofocus placeholder="Item_category" valu
e=<?php echo $item_category; ?>><br>
<input type="text" name="pic" required autofocus placeholder="pic" value=<?php echo $pic; ?>><br>
<input type="submit" name="updateitemBtn" value="Update">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

undefined variable sub_total. even after defining the variable

<?php
include('includes/db.php');
?>
<?php include('functions/functions.php'); ?>
<?php
include('header.php');
?>
<?php
include('topheader.php');
?>
<?php
include('nav.php');
?>
<div id="content">
<div class="container">
<div class="col-md-12">
<ul class="breadcrumb">
<li>Home</li>
<li>Cart</li>
</ul> <!--breadcrumb ends--->
</div> <!---col-md-12 ends-->
<div class="col-md-9" id="cart">
<div class="box">
<form action="cart.php" method="post" enctype="multipart-form-data">
<h1>Shopping Cart</h1>
<?php
$ip_add = getUserIP();
$select_cart = "select * from cart where ip_add='$ip_add'";
$run_cart = mysqli_query($conn, $select_cart);
$count = mysqli_num_rows($run_cart);
?>
<p class="text-muted">You currently have <?php echo $count; ?> items in your cart</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th colspan="2">Product</th>
<th>Quantity</th>
<th colspan="1">Unit Price</th>
<th>Container</th>
<th colspan="1">Delete</th>
<th colspan="2">Sub Total</th>
</tr>
</thead> <!--thead ends-->
<tbody>
<?php
$total = 0;
while ($row_cart = mysqli_fetch_array($run_cart)) {
$pro_id = $row_cart['p_id'];
$pro_size = $row_cart['size'];
$pro_qty = $row_cart['qty'];
$get_products = "select * from products where product_id='$pro_id'";
$run_products = mysqli_query($conn, $get_products);
while ($row_products = mysqli_fetch_array($run_products)) {
$product_title = $row_products['product_title'];
$product_img1 = $row_products['product_img1'];
$only_price = $row_products['product_price'];
$sub_total = $row_products['product_price'] * $pro_qty;
$total += $sub_total;
}
?>
<tr>
<td><img src="admin_area/product_images/<?php echo $product_img1; ?>"></td>
<td><?php echo $product_title; ?></td>
<td><?php echo $pro_qty; ?></td>
<td>₹<?php echo $only_price; ?>.00</td>
<td><?php echo $pro_size ?></td>
<td><input type="checkbox" name="remove[]" value="<?php echo $pro_id; ?>"></td>
<td>₹<?php echo $sub_total; ?>.00</td>
</tr> <!---tr ends-->
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th colspan="2">₹ <?php echo $total; ?>.00</th>
</tr>
</tfoot> <!--tfoot ends-->
</table> <!--table ends-->
</div> <!---table-responsive end-->
<div class="box-footer">
<div class="pull-left">
<a href="index.php" class="btn btn-default">
<i class="fa fa-chevron-left"></i>Continue Shopping
</a>
</div> <!--pullleft ends-->
<div class="pull-right">
<button class="btn btn-default" type="submit" name="update" value="Update Cart"><i class="fa fa-refresh"></i>Update Cart </button>
<a href="checkout.php" class="btn btn-primary">
Proceed to checkout<i class="fa fa-chevron-right"></i>
</a>
</div> <!--pullright ends-->
</div> <!----box footer ends-->
</form> <!---form ends-->
</div> <!---box ends-->
<?php
function update_cart() {
global $conn;
if (isset($_POST['update'])) {
foreach ($_POST['remove'] as $remove_id) {
$delete_product = "delete from cart where p_id='$remove_id'";
$run_delete = mysqli_query($conn, $delete_product);
if ($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
}
echo #$up_cart = update_cart();
?>
<div class="row same-height-row">
<div class="col-md-3 col-md-6">
<div class="box same-height headline">
<h3 class="text-center">Recently Viewed Product</h3>
</div> <!----box same-height headline end-->
</div> <!---col-m-3 col-md-6 ends---->
<?php
$get_products = "select * from products order by rand() LIMIT 0,3";
$run_products = mysqli_query($conn, $get_products);
while ($row_products = mysqli_fetch_assoc($run_products)) {
$pro_id = $row_products['product_id'];
$pro_title = $row_products['product_title'];
$pro_price = $row_products['product_price'];
$pro_img1 = $row_products['product_img1'];
echo "<div class='center-responsive col-md-3 col-sm-6'>
<div class='product same-height'>
<a href='details.php?pro_id=$pro_id'>
<img src='admin_area/product_images/$pro_img1' class='img-responsive'>
</a>
<div class='text'>
<h3><a href='details.php?pro_id=$pro_id'>$pro_title</a></h3>
<p class='price'>₹ $pro_price</p>
</div>
</div>
</div>";
}
?>
</div>
</div>
<div class="col-md-3">
<div class="box" id="order-summary">
<div class="box-header">
<h3>Order Summary</h3>
</div> <!--box-header ends-->
<p class="text-muted">
Shipping and additional costs are calulated based on the value you have entered.
</p>
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td>Order Subtotal</td>
<th>₹<?php echo $sub_total; ?></th>
</tr>
<tr>
<td>Shipping and Handling</td>
<td>₹0.00</td>
</tr>
<tr>
<td>Tax</td>
<td>₹0.00</td>
</tr>
<tr class="total">
<td>Total</td>
<th>₹ <?php echo $total; ?></th>
</tr>
</tbody>
</table> <!--table ends-->
</div> <!---table responsive ends-->
</div> <!---box ends-->
</div> <!----col-md-3 ends--->
</div> <!--container ends-->
</div> <!---content ends-->
<?php include_once 'footer.php'; ?>
i get undefined variable sub_total even after defining variable subtotal above. Can someone figure out what the problem is?? I have check all curly brackets and semicolen. Due on a project that needs to be submited tomorrow. I am thinking improving the coding later but the basic functionality is not working.

Getting database fetched with select box

I'm testing my skills in php using bootstrap libraries and i'm in a dead end.
I have two tables, students and countries.
I got a select box where I get the country (and the ID associated) and I want to see the students from that country in a form.
Actually, I got the form, got the select but i can't find how to interact together.
Please, help me. Thanks
the code of my php
<?php
//Access to BDD
include 'database.php';
?>
<html>
<Head>
<title>List of students</title>
<script src="bootstrap/js/bootstrap.js"></script>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
</Head>
<body>
<!-- formulaire de recherche -->
<form class="panel-group form-horizontal" method="get" action="home.php" role="form">
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-header">
<h4>Search</h4>
</div>
<div class="col-sm-3">
<!-- dropdown countries -->
<select class="form-control" id="select" name="country">
<option value="">country</option>
<?php
$sel_country = "SELECT * FROM countries";
$run_country = mysqli_query($conn,$sel_country);
while ($rows= mysqli_fetch_assoc($run_country)){
echo '<option value='.$rows['id'].'>'.$rows['brand_name'].'</option>';
}
?>
</select>
<br/>
<br/>
<button type="submit" class="btn btn-default" id="searchbtn" name="submit">Go</button>
</div>
</div>
</div>
</form>
<?php
//SQL listing all the students.
$sql = "SELECT * FROM student ORDER by student_id"; //CHOIX NON PLUS!!!
$run_sql = mysqli_query($conn, $sql);
while ($rows = mysqli_fetch_assoc($run_sql)){
echo '<div class="container">
<table class="table table-hover">
<tr>
<td><h2><a class="btn btn-info" href="detail.php?vo_id='.$rows['student_id'].'">'.$rows ['name'].'</a></h2></td>
</tr>
<tr>
<td>'.$rows ['surname'].'</td>
</tr>
<tr>
<td>'.$rows ['age'].'</td>
</tr>
<tr>
<td>'.$rows ['country'].'</td>
</tr>
</table>
</div>
<br>';
}?>
Update below code in existing:
Assuming that all code is on same page (home.php)
if(isset($_GET[country]))
{
$sql = "SELECT * FROM student WHERE country_id =". $_GET["country"]." ORDER by student_id"; //CHOIX NON PLUS!!!
$run_sql = mysqli_query($conn, $sql);
while ($rows = mysqli_fetch_assoc($run_sql)){
echo '<div class="container">
<table class="table table-hover">
<tr>
<td><h2><a class="btn btn-info" href="detail.php?vo_id='.$rows['student_id'].'">'.$rows ['name'].'</a></h2></td>
</tr>
<tr>
<td>'.$rows ['surname'].'</td>
</tr>
<tr>
<td>'.$rows ['age'].'</td>
</tr>
<tr>
<td>'.$rows ['country'].'</td>
</tr>
</table>
</div>
<br>';
}

Categories