MySQL problem using php when trying to update table - php

I want to update my tbl_category. But when I click on the Update button, it does not show any error. But it does not update the value of tbl_category. I am sure it's very simple since I am a beginner i am not getting well what is wrong exactly so please help me.
Here is the script:
catlist.php
<?php include 'inc/header.php';?>
<?php include 'inc/sidebar.php';?>
<?php include("../classes/Category.php");?>
<?php
$cat = new Category();
if(isset($_GET['delcat'])){
$id = $_GET['delcat'];
//$id = preg_replace('/[^-a-zA-Z0-9_]/', '', $_GET['delcat']);
$delCat = $cat->delCatById($id);
}
?>
<div class="grid_10">
<div class="box round first grid">
<h2>Category List</h2>
<div class="block">
<?php
if(isset($delCat)){
echo $delCat;
}
?>
<table class="data display datatable" id="example">
<thead>
<tr>
<th>Serial No.</th>
<th>Category Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$getCat = $cat->getAllCat();
if($getCat){
$i = 0;
while($result = $getCat->fetch_assoc()){
$i++;
?>
<tr class="odd gradeX">
<td><?php echo $i;?></td>
<td><?php echo $result['catName'];?></td>
<td>Edit || <a onclick="return confirm('Are you sure to delete!')" href="?delcat=<?php echo $result['catId'];?>">Delete</a></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
setupLeftMenu();
$('.datatable').dataTable();
setSidebarHeight();
});
</script>
<?php include 'inc/footer.php';?>
catedit.php
<?php include 'inc/header.php';?>
<?php include 'inc/sidebar.php';?>
<?php include("../classes/Category.php");?>
<?php
/*if(!isset($_GET['catid']) || $_GET['catid'] == NULL){
//echo "<script>window.location = 'catlist.php'; </script>";
} else{
$id = $_GET['catid'];
}*/
$id = isset($_GET['catid']) ? $_GET['catid'] : '';
//$id = $_GET['catid'];
$cat = new Category();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$catName = $_POST['catName'];
$updateCat = $cat->catUpdate($catName,$id);
}
?>
<div class="grid_10">
<div class="box round first grid">
<h2>Update Category</h2>
<div class="block copyblock">
<?php
echo $id;
?>
<?php
if(isset($updateCat)){
echo $updateCat;
}
?>
<?php
$getCat = $cat->getCatById($id);
if($getCat){
while($result = $getCat->fetch_assoc()){
?>
<form action="catedit.php" method="post">
<table class="form">
<tr>
<td>
<input type="text" name="catName" value="<?php echo $result['catName'];?>" placeholder="Enter Category Name..." class="medium" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" Value="Update" />
</td>
</tr>
</table>
</form>
<?php
}
}
?>
</div>
</div>
</div>
<?php include 'inc/footer.php';?>
Category.php
<?php
include_once("../lib/Database.php");
include_once("../helpers/Format.php");
?>
<?php
class Category {
private $db;
private $fm;
public function __construct() {
$this->db = new Database();
$this->fm = new Format();
}
public function catInsert($catName){
$catName = $this->fm->validation($catName);
$catName = mysqli_real_escape_string($this->db->link , $catName);
if(empty($catName)){
$msg = "<span class='error'>Category field must not be empty !</span>";
return $msg;
} else{
$query = "INSERT INTO tbl_category(catName) VALUES('$catName')";
$catinsert = $this->db->insert($query);
if($catinsert){
$msg = "<span class='success'>Category Inserted Successfully</span>";
return $msg;
} else {
$msg = "<span class='error'>Category Not Inserted.</span>";
return $msg;
}
}
}
public function getAllCat(){
$query = "SELECT * FROM tbl_category ORDER BY catId DESC";
$result = $this->db->select($query);
return $result;
}
public function getCatById($id){
$query = "SELECT * FROM tbl_category WHERE catId = '$id'";
$result = $this->db->select($query);
return $result;
}
public function catUpdate($catName,$id){
$catName = $this->fm->validation($catName);
$catName = mysqli_real_escape_string($this->db->link , $catName);
$id = mysqli_real_escape_string($this->db->link , $id);
if(empty($catName)){
$msg = "<span class='error'>Category field must not be empty !</span>";
return $msg;
} else{
//$query = "UPDATE tbl_category
//SET
//catName = '$catName'
//WHERE catId = '$id'";
$query = "UPDATE tbl_category SET catName = '$catName' WHERE catId = '$id'";
$updated_row = $this->db->update($query);
if($updated_row){
$msg = "<span class='success'>Category Updated Successfully</span>";
return $msg;
} else {
$msg = "<span class='error'>Category Not Updated.</span>";
return $msg;
}
}
}
public function delCatById($id){
$query = "DELETE FROM tbl_category WHERE catId = '$id'";
$delData = $this->db->delete($query);
if($delData){
$msg = "<span class='success'>Category Deleted Successfully</span>";
return $msg;
}
else {
$msg = "<span class='error'>Category Not Deleted.</span>";
return $msg;
}
}
}
?>

Once your catedit.php form is submitted, PHP is not receiving 'id', which it needs to be able to execute catUpdate method.
You should have additional (hidden) field in your form which would be for $id. Then, you will take that one from POST as well. $_POST['id'];
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$catName = $_POST['catName'];
$id = $_POST['id'];
$updateCat = $cat->catUpdate($catName,$id);
}
....
<form action="catedit.php" method="post">
<input type="hidden" id="i" name="id" value="<?php echo $id; ?>">

Related

applying search or filter to table with pagination

hello kind sirs can you help me with this code. What i try to do is when i type something in the search box, ex. pending it will show the 5 pending reservation per page(5 rows of pending reservation). but when i try it, it shows all the pending reservation which is more than 10.
here is the image
i try something like this.. but it shows nothing
$query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$valueToSearch."%' LIMIT " . $this_page_first_result . ',' . $results_per_page";
Here is the whole code
<?php
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ERROR | E_PARSE);
session_start();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "srdatabase";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$results_per_page = 5;
$select= "SELECT * FROM reservations";
$result = mysqli_query($conn, $select);
$number_of_results = mysqli_num_rows($result);
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
$this_page_first_result = ($page-1)*$results_per_page;
$sql = "SELECT * FROM reservations LIMIT " . $this_page_first_result . ',' . $results_per_page;
$result = mysqli_query($conn, $sql);
$number_of_pages = ceil($number_of_results/$results_per_page);
?>
<div id="paging-div">
<?php
for($page=1;$page<=$number_of_pages;$page++)
{
echo '<a id="pagingLink" href="adminControl.php?page=' . $page . '">' . $page . '</a>';
}
?>
<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else
{
$query = "SELECT * FROM reservations";
$search_result = filterTable($query);
}
function filterTable($query)
{
$conn = mysqli_connect("localhost", "root", "", "srdatabase");
$filter_Result = mysqli_query($conn, $query);
return $filter_Result;
}
?>
</div>
<!DOCTYPE html>
<html>
<head>
<title>Admin Control</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="topnav" id="myTopnav">
Home
Speakers
About
Contact
Reservation
Sign Out
<?php echo $_SESSION['firstname']; ?>
Sign Up
Sign In
Admin control
☰
</div>
<br>
<br>
<br>
<br>
<h4 style="padding-left:10px; text-align:center;">Reservation List</h4>
<hr>
<form action="adminControl.php" method="POST">
<input type="text" name="valueToSearch" placeholder="type a value">
<input type="submit" name="search" value="Filter">
</form>
<br>
<br>
<div style="overflow-x:auto;">
<table class="reservations-table">
<tr>
<th class="thFirstName">First Name</th>
<th class="thLastName">Last Name</th>
<th class="thEmailAddress">Email Address</th>
<th class="thContactNumber">Contact Number</th>
<th class="thSpeaker">Speaker</th>
<th class="thTopic">Topic</th>
<th class="thLocation">Location</th>
<th class="thAudience">Audience</th>
<th class="thCount">Count</th>
<th class="thTime">Time</th>
<th class="thDate">Date</th>
<th class="thAction">Reservation Date</th>
<th class="thAction">Status</th>
<th class="thAction">Action</th>
<th class="thAction">Action</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['lastname'];?></td>
<td><?php echo $row['emailaddress'];?></td>
<td><?php echo $row['contactnumber'];?></td>
<td><?php echo $row['speaker'];?></td>
<td><?php echo $row['topic'];?></td>
<td><?php echo $row['location'];?></td>
<td><?php echo $row['audience'];?></td>
<td><?php echo $row['count'];?></td>
<td><?php echo $row['time'];?></td>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['reservationdate'];?></td>
<td><?php echo $row['reservationstatus'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</div>
<?php
$epr='';
$msg='';
if(isset($_GET['epr']))
$epr=$_GET['epr'];
if($epr=='delete')
{
$id=$_GET['id'];
$delete=mysqli_query($conn, "DELETE FROM reservations WHERE id=$id");
if($delete)
header('location:adminControl.php');
else
$msg='Error :'.mysqli_error();
}
?>
<?php
$epr='';
$msg='';
if(isset($_GET['epr']))
$epr=$_GET['epr'];
if($epr=='approve')
{
$id=$_GET['id'];
$approve=mysqli_query($conn, "UPDATE reservations SET reservationstatus='approved' WHERE id=$id");
header('location:adminControl.php');
}
?>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
<script>
function ifAdmin()
{
document.getElementById("signIn").style.display = "none";
document.getElementById("signUp").style.display = "none";
document.getElementById("signOut").style.display = "block";
document.getElementById("adminControl").style.display = "block";
}
</script>
<script>
function ifNotAdmin()
{
document.getElementById("signIn").style.display = "none";
document.getElementById("signUp").style.display = "none";
document.getElementById("signOut").style.display = "block";
document.getElementById("adminControl").style.display = "none";
}
</script>
<script>
function ifNotLogin()
{
document.getElementById("user").style.display = "none";
document.getElementById("signOut").style.display = "none";
document.getElementById("adminControl").style.display = "none";
}
</script>
<?php
if (isset($_SESSION['signedIn']) && $_SESSION['signedIn'] == true)
//if login
{
if($_SESSION['type'] == 1)
{
echo "<script type='text/javascript'>ifAdmin();</script>";
}
elseif($_SESSION['type'] == 0)
{
echo "<script type='text/javascript'>ifNotAdmin();</script>";
}
}
//if not login
else
{
echo "<script type='text/javascript'>ifNotLogin();</script>";
}
?>
<div id="footer" class="push">Copyright 2017</div>
</body>
</html>
... when i try it, it shows all the pending reservation which is more than 10.
That's because when you hit 2nd, 3rd, ... pages(after navigating from the 1st page), the $_POST array would be empty i.e. $_POST['search'] won't be set, and that's why else{...} part of the code will get executed every time you navigate to 2nd, 3rd, ... pages. Since you're not sending any sensitive data with the form, use GET instead of POST in the method attribute of the form, like this:
<form action="..." method="get">
and get the user inputted data like this:
if (isset($_GET['search'])) {
$valueToSearch = $_GET['valueToSearch'];
...
Subsequently, you need to attach that search query in each of your pagination links, so that the search query would be available when you hop from page to page.
// your code
<?php
for($page=1;$page<=$number_of_pages;$page++)
{
echo "<a id='pagingLink' href='adminControl.php?page=" . $page . "&valueToSearch=". urlencode($_GET['valueToSearch']) ."&search'>" . $page . "</a>";
}
?>
// your code

php dynamic pagination logical error

I am developing a page where a user can enter the limit of the table in pagination. While I am doing this the data I enter is taken and query is performed according to that. But as I click on another page of that table the value is reset to default which I set to 5 here.
<?php session_start(); ?>
Submit New record<br><br>
<form method="post">
<input type="text" name="dlimit">
<input type="submit" name="submit">
</form>
<?php
$database = 'test';
require 'connection.php';
if(!isset($_POST['submit']))
{
$limit = 5;
}
else
{
$dlimit = $_POST['dlimit'];
$limit = $dlimit;
}
#$id = $_GET['id'];
if($id==""||$id==null)
{
$page=0;
}
else
{
$page = ($id*$limit)-$limit;
}
$qq ="select * from record limit $page,$limit";
$result = $link -> query($qq);
?>
<table border="1"><th>ID</th>
<th>Name</th>
<th>qualification</th>
<th>address</th>
</tr>
<?php
while ($row = mysqli_fetch_object($result))
{
?>
<tr>
<td><?php echo $row->id ?></td>
<td><?php echo $row->user_name ?></td>
<td><?php echo $row->qualification ?></td>
<td><?php echo $row->address ?></td>
</tr>
<?php
}
?>
</table>
<?php
$query = "SELECT * FROM record";
$result = $link -> query($query);
$rows = mysqli_num_rows($result);
$rr = $rows/$limit;
$rr = ceil($rr);
for ($i=1; $i<=$rr ; $i++) {
?>
<?php echo #$i;?>
<?php
}
mysqli_close($link)
?>
Run the above code and check. If my words are not clear to you.
I think that the $_POST data is missing. You click on the link, so the new page will open without POST infos.
You can change this, if you switch to GET instead of POST. You can add the GET parameter to your <a href=""> Tag.
For example
<a href="pagination.php?page=5&dlimit=100
Also try to avoid the #error suppression and don't pass the $_POST/$_GET Vars directly to your sql string. Bad people could use it for SQL Injections
So I got the answer for my question. I am posting it here if anyone need it on later basis.
Submit New record<br><br>
<form method="get">
<input type="text" name="dlimit">
<input type="submit" name="submit">
</form>
<?php
$database = 'test';
require 'connection.php';
if(empty($_GET['dlimit']) && !isset($_GET['submit']) && empty($_GET['n']))
{
$limit = 5;
global $limit;
}
else
{
if (isset($_GET['dlimit']))
{
$limit = $_GET['dlimit'];
}
else
{
#$limit = $_GET['n'];
}
global $limit;
}
if(!isset($_GET['submit'])&& empty($_GET['n']))
{
$n=5;
global $n;
}
else
{
if(empty($_GET['dlimit']))
{
$n=$_GET['n'];
}
else
{
$n=$_GET['dlimit'];
}
global $n;
}
#$id = $_GET['id'];
if($id==""||$id==null)
{
$page=0;
}
else
{
$page = ($id*$limit)-$limit;
}
$qq ="select * from record limit $page,$limit";
$result = $link -> query($qq);
?>
<table border="1"><th>ID</th>
<th>Name</th>
<th>qualification</th>
<th>address</th>
</tr>
<?php
while ($row = mysqli_fetch_object($result))
{
?>
<tr>
<td><?php echo $row->id ?></td>
<td><?php echo $row->user_name ?></td>
<td><?php echo $row->qualification ?></td>
<td><?php echo $row->address ?></td>
</tr>
<?php
}
?>
</table>
<?php
if (!isset($_GET['submit']) && empty($_GET['n'])) {
$n = 5;
global $n;
}
else
{
if (empty($_GET['dlimit'])) {
$n = $_GET['n'];
}
else
{
$n = $_GET['dlimit'];
}
global $n;
}
$query = "SELECT * FROM record";
$result = $link -> query($query);
$rows = mysqli_num_rows($result);
$rr = $rows/$limit;
$rr = ceil($rr);
for ($i=1; $i<=$rr ; $i++) {
?>
<?php echo #$i;?>
<?php
}
mysqli_close($link)
?>
Again I am mentioning here that files which I included here are just connection where I filled my connection details and other is my record entry file where data is entered by the user.

Search not functiong using pdo

I have a project that I'm doing now and my project consist of CRUD functions and it works fine. My project also has its pagination and search function.
The pagination is working but the search function does not seem to work. What I want is, a search where I can search anything within my table like I want to search all the fields or maybe a live search. Can somebody please help me.
Dbconfig.php
<?php
$db_host = 'localhost'
$db_username = 'root';
$db_password = '';
$db_name = 'survey';
try{
$db_con = new PDO("mysql:host={$db_host};dbname={$dbname}",db_username,$db_password);
}
catch(PDOException $exception{
echo $exception->getMessage();
}
include_once 'class.paginate.php'
$paginate = new paginate($DB_con);
?>
Class.paging.php
<?php
class paginate{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['password']; ?></td>
<td><?php echo $row['province']; ?></td>
<!-- <td>visit</td> -->
</tr>
<?php
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
public function search($query)
{
$stmt=$this->db->prepare($query);
$query = '%' . $query . '%';
$stmt->bindparam('query', $query, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
if(empty($result) or $result == false)
return array();
else
return $result;
}
public function paging($query,$records_per_page)
{
$starting_position=0;
if(isset($_GET["page_no"]))
{
$starting_position= ($_GET["page_no"]-1)*$records_per_page;
}
$query2=$query." limit. $starting_position,$records_per_page";
return $query2;
}
public function paginglink($query,$records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if($total_no_of_records > 0)
{
?><tr><td colspan="4" align="center"><?php
$total_no_of_pages=ceil($total_no_of_records/$records_per_page);
$current_page=1;
if(isset($_GET["page_no"]))
{
$current_page=$_GET["page_no"];
}
if($current_page!=1)
{
$previous =$current_page-1;
echo "<a href='".$self."?page_no=1'>First</a> ";
echo "<a href='".$self."?page_no=".$previous."'>Previous</a> ";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<strong><a href='".$self."?page_no=".$i."' style='color:red;text-decoration:none'>".$i."</a> </strong> ";
}
else
{
echo "<a href='".$self."?page_no=".$i."'>".$i."</a> ";
}
} if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<a href='".$self."?page_no=".$next."'>Next</a> ";
echo "<a href='".$self."?page_no=".$total_no_of_pages."'>Last</a> ";
}
?></td></tr><?php
}
}
}
Index.php
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="Bootstrap/bootstrap.css">
<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
<body>
<div id="search-wrapper">
<form name="search" method="GET" action="">
<table id="search" class="table-responsive">
<tr>
<input name="var1" type="text" id="search-box">
<button id="submit" name="submit" type="submit">Search</button>
</tr>
</table>
<?php
$query = "SELECT * FROM `login` WHERE (`username` like :query or `password` like :query) ";
$paginate->search($query)
?>
</form>
</div>
<div id="table-wrapper" class="table-responsive">
<table id="data" class="table table-striped table-hover">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
<th>Actions</th>
</tr>
</thead>
<?php
$query = "SELECT * FROM login";
$records_per_page=20;
$newquery = $paginate->paging($query,$records_per_page);
$paginate->dataview($newquery);
$paginate->paginglink($query,$records_per_page);
?>
</table>
</div>
<div id="footer">
cleartuts.blogspot.com
</div>
</body>
</html>

my delete program doesn't work in php

My delete program doesn't work in php.
The following code displays the information, but the delete code doesn't work, where did I make mistakes?
The connection is established, but delete code is not working, why?
connection.php
<?php
// Database Connection
$con = mysql_connect("localhost","sathishcst","geni7joy");
mysql_select_db("practice",$con);
?>
display.php
<?php
require_once 'connection.php';
$query = "select * from pays";
$data = mysql_query($query);
?>
<html>
<body>
<table border=”1″ cellpadding=”5″>
<tr>
<th>Id</th> <th>NewsList</th> <th>Anchor link</th> <th colspan=”2″>Action</th>
</tr>
<?php while($rec = mysql_fetch_array($data)) { ?>
<tr>
<td> <?php echo $rec['id']; ?> </td>
<td> <?php echo $rec['name']; ?> </td>
<td> <?php echo $rec['email']; ?> </td>
<td> <a href=”edit.php?id=<?php echo $rec['id']; ?>”>edit</a> </td>
<td> <a onClick="return confirm('Sure to delete!')" href="delete.php?id=<?php echo $rec['id']; ?>">delete</a> </td>
</tr>
<?php } ?>
</table>
</body>
</html>
delete.php
<?php
require_once “connect.php”;
$msg = “”;
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : “0″;
$query = “delete from tbemp where id=”.$id;
if(mysql_query($query)) {
header(“location:display.php”);
} else {
echo “unable to delete!”;
}
?>
You made an error in delete.php file. After where clause, you need to use $_REQUEST['id'] instead of $id
Try this code:
<?php
require_once "connect.php";
$msg = "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "0";
$query = "delete from tbemp where id=".$_REQUEST['id'];
if(mysql_query($query)) {
header("location:display.php");
} else {
echo "unable to delete!";
}
?>
change this:
<?php
require_once “connect.php”;
$msg = “”;
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : “0″;
$query = “delete from tbemp where id=”.$id;
if(mysql_query($query)) {
header(“location:display.php”);
} else {
echo “unable to delete!”;
}
?>
with this:
<?php
require_once "connection.php";
$msg = "";
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : "0";
$query = "delete from tbemp where id=".$id;
if(mysql_query($query)) {
header("location:display.php");
} else {
echo "unable to delete!";
}
?>

Table won't populate with data

I've been new to programming and I been working with phpmyadmin on localhost. I am making a simple table on a webpage to display data. The problem is that everytime I load the page it only displays the table and not table will load up. Here is my code:
<?php
require('../model/database.php');
require('../model/product_db.php');
$products = get_products();
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'under_construction';
}
// Display the product list
include('view-productList.php');
?>
This is the view-productList.php:
<?php include '../view/header.php'; ?>
<div id="main">
<h1>Product List</h1>
<div id="content">
<!-- display a table of products -->
<h2><?php echo $name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Version</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['name']; ?></td>
<td class="right"><?php echo $product['version']; ?></td>
<td><form action="." method="post">
<input type="hidden" name="action"
value="delete_product" />
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>" />
<input type="submit" value="Delete" />
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</div>
</div>
<?php include '../view/footer.php'; ?>
Query Page:
<?php
function get_products() {
global $db;
$query = 'SELECT * FROM products
ORDER BY productID';
$products = $db->query($query);
return $products;
}
function get_products_by_category($category_id) {
global $db;
$query = "SELECT * FROM products
WHERE products.categoryID = '$category_id'
ORDER BY productID";
$products = $db->query($query);
return $products;
}
function get_product($product_id) {
global $db;
$query = "SELECT * FROM products
WHERE productID = '$product_id'";
$product = $db->query($query);
$product = $product->fetch();
return $product;
}
function delete_product($product_id) {
global $db;
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$db->exec($query);
}
product_db.php should not be commented out for one - assuming that is the file that holds the "Query Page:" contents.
$products = get_products();
should come immediately after the include.
and your for loop needs the fetch result and not just the product resource:
<?php foreach ($products->fetch() as $product) : ?>
assuming fetch() is relevant to this type of resource since we can't see your db class.

Categories