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!";
}
?>
Related
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; ?>">
First off: There are no YouTube videos or tutorials for this as far as I know. I found some but they were JavaScript, but I have no knowledge of it so I can't do it in a very short time.
I recently posted the same question but altered my codes after some people's suggestions. I got a lot of errors instead of a solution...
Anyway, I have a picture of my site and what I want to do.
Site:
https://imgur.com/a/hcpuA
and here's a picture of what I want to do:
https://imgur.com/a/Vuuk9
And finally my codes are below, I cut down the HTML parts which isn't really necessary.
adduser.php:
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('location: login.php');
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Manage users</title>
<link rel="icon" type="image/png" sizes="16x16" href="image/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="image/favicon-32x32.png">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/table.css">
</head>
<body>
<?php include ("header.php"); ?>
<?php
require ("config.php");
?>
<form name="frmSearch" method="post" action="adduser.php">
<div class="table-container">
<div class="table-something">
<div class="table-header">
<span id="message"></span>
<h2>Admin List<span class="blink">_</span> </h2>
<input name="var1" type="text" id="var1" />
<input class="dede" type="submit" name="search" value="search" />
</div>
<div class="table-body">
<table class="table-hen">
<?php
if (isset($_POST['var1'])) {
$var1 = $_POST['var1'];
}
else {
$var1 = 1;
}
$sql= "SELECT user_id, fname, mname, lname, username, type, a_e_num, user_status FROM users WHERE a_e_num LIKE :search";
$stmt = $db->prepare($sql);
$stmt->bindValue(':search', '%' . $var1 . '%', PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() > 0) {
?>
<tr>
<th>User Id</th>
<th>Name </th>
<th>Username</th>
<th>Employee # </th>
<th>User Type</th>
<th>Status</th>
<th>Action</th>
</tr>
<?php
$result = $stmt->fetchAll();
foreach ($result as $row):
?>
<tr>
<td><?php echo $row['user_id']; ?></td>
<td><?php echo $row['fname']; ?> <?php echo $row['mname']; ?> <?php echo $row['lname']; ?> </td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['a_e_num']; ?> </td>
<td><?php echo $row['type']; ?></td>
<td>
<?php if ($row['user_status']=='Enable') echo "Active";
if ($row['user_status']=='Disable') echo "Disabled" ?>
</td>
<td>
<?php
$user_id = $row['user_id'];
$status = '';
if ($row['user_status'] == 'Enable') {
$status = 'Enable';
}
else if ($row['user_status'] == 'Disable') {
$status = 'Disable';
}
?>
<?php
$user_id = $row['user_id'];
$status = $row['user_status'];
?>
<a class="archive" action="archive.php" onclick="
return confirm('Are you sure you want to <?php if ($row['user_status']=='Enable') echo "disable";
if ($row['user_status']=='Disable') echo "enable"?> this user account?')"
href="archive.php?user_id=<?= $user_id?>&status=<?=$status?>">
<?php if ($row['user_status']=='Disable')
echo "Unarchive";
if ($row['user_status']=='Enable')
echo "Archive" ?>
</a></td>
</tr>
<?php endforeach;
} else {
echo 'there is nothing to show';
}
?>
</table>
</form>
</div>
</div>
</div>
<br><h1></h1>
<br><h1></h1>
<br><h1></h1>
<br><h1></h1>
<br><h1></h1>
<?php include ("footer.php"); ?>
</body>
</html>
And here's my Archive.php:
<?php
require ("config.php");
$user_id= $_GET['user_id'];
$user_status = $_GET['user_id'];
$query = $db->prepare ("SELECT * FROM users WHERE user_id = :user_id, user_status = :user_status");
$query->bindParam(':user_id', $user_id);
$query->bindParam(':user_status',$user_status);
$query->execute();
if ($user_status=='Enable')
{
$sql = "UPDATE users SET user_status = 'Disable' WHERE user_id = :user_id";
}
if ($user_status=='Disable')
{
$sql = "UPDATE users SET user_status='Enable' WHERE user_id = :user_id";
}
if ($query->execute([':user_status'=>$user_status, ':user_id'=>$user_id])){
header("Location:adduser.php");
}
?>
First you need to clear your statement and separate your parameters like below.
Step 1:
Parameters are (Query String) which start with ? and more paramters will be followed by &
<?php
$user_id = $row['user_id'];
$status = '';
if ($row['user_status'] == 'Enable') {
$status = 'Enable';
} else if ($row['user_status'] == 'Disable') {
$status = 'Disable';
}
?>
<a href="archive.php?user_id=<?=$user_id?>&user_status=<?=$status?>">
IF your $row['user_status'] value is always same as conditional statement then you don't need condition simply follow assign variable.
<?php
$user_id = $row['user_id'];
$status = $row['user_status'];
?>
<a href="archive.php?user_id=<?=$user_id?>&status=<?=$status?>">
Step 2:
Then this
$id= $_GET['user_id'];
To
$user_id= $_GET['user_id'];
$user_status = $_GET['user_status'];
because your bind parameters take variable which is not defined in your Archive.php
$query->bindParam(':user_id', $user_id);
$query->bindParam(':user_status',$user_status);
EDIT 2:
change your ` where it start with
<td>
<?php
$user_id = $row['user_id'];
$status = $row['user_status'];
Just replace with below.
<td>
<?php
$user_id = $row['user_id'];
$status = $row['user_status'];
$btn_confirm = '';
if ($status == 'Enable') {
$btn_confirm = "disable";
} else if ($status == 'Disable') {
$btn_confirm = "enable";
}
?>
<a class="archive" onclick="return confirm('Are you sure you want to <?= $btn_confirm; ?> this user account?')"
href="Archive.php?user_id=<?= $user_id ?>&status=<?= $status ?>">
<?php if ($status == 'Disable') {
echo "Unarchive";
} else if ($status == 'Enable') {
echo "Archive"; }
?>
</a>
</td>
Note: your file name is Archive.php not archive.php this causing issue for blank page
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
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.
I am trying to call function ProdTotal to get the total quantity order and total amount ordered for a product and load it in the table in the Display Function. I am getting the error: Call to undefined function ProdTotal();
<?php
Class CarsClass {
private $user = 'php06';
private $pwd = 'php06';
private $dbConn;
function __construct($db='classicmodels') {
//Create connection to MySQL database requested, if blank just connect up.
$this->dbConn = new mysqli('localhost', $this->user, $this->pwd, $db);
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
$query = "select count(*) as 'custcount' from customers";
$result = $this->dbConn->query($query);
$row = $result->fetch_assoc();
$custCount = $row['custcount'];
print "Connected to DB $db as user $this->user<br><br> Number of Rows $custCount<br><br>";
}
function __destruct(){
mysqli_close();
Print "DB closed by user <br><br>";
}
function header(){
echo "Midterm Exam Script 2 Header<br><br>";
}
function display(){
$totqty = 0;
$totamt = 0;
//get row from WB_Resident Table
$query = "select productCode,productName,productDescription,quantityInStock,buyprice,MSRP from products";
$result = $this->dbConn->query($query);
?>
<table id="midterm2">
<tr>
<th colspan="13">Product Database Table</th>
</tr>
<tr>
<th width="2%">productCode</th>
<th width="10%">productName</th>
<th width="10%">productDescription</th>
<th width="10%">quantity in stock</th>
<th width="10%">buyPrice</th>
<th width="2%">MSRP</th>
<th width="10%">Total Qty Ordered</th>
<th width="10%">Total $ Ordered</th>
</tr>
<?php
while($row = $result->fetch_assoc()):
$producta = $row["productCode"];
ProdTotal($producta);
?>
<tr>
<td>
<?php echo $row["productCode"]; ?>
</td>
<td>
<?php echo $row["productName"];?>
</td>
<td>
<?php echo $row["productDescription"]; ?>
</td>
<td>
<?php echo $row["Qty In Stock"]; ?>
</td>
<td>
<?php echo $row["Buy Price"]; ?>
</td>
<td>
<?php echo $row["MSRP"]; ?>
</td>
<td>
<?php echo $totqty; ?>
</td>
<td>
<?php echo $totamt; ?>
</td>
</tr>
<?php
endwhile;
?>
</table>
<?php
}
function footer(){
echo "Midterm Exam Script 3 Footer<br><br>";
}
function ProdTotal($product){
echo "product code entered = $product <br><br>";
$query = "select RTRIM(productCode) as productt, quantityOrdered, priceEach from orderdetails order by productt";
$result = $this->dbConn->query($query);
while($row = $result->fetch_assoc()){
if ($row["productt"] == $product){
echo $row;
$total = $row["quantityOrdered"] * $row["priceEach"];
$totqty = $totqty + $row["quantityOrdered"];
$totamt = $totamt + $total;
echo totqty;
echo totamt;
return array($totqty, $totamt);
}
}
}
}
?>
This script is executed to call class.
<html>
<head>
<title>Midterm2 Script 3</title>
<link rel="stylesheet" type="text/css" href="midterm2.css" />
</head>
<body>
<?php
require 'CarsClass3a.php';
$obj1= new CarsClass('classicmodels');
$obj1->header();
$obj1->display();
$obj1->footer();
?>
</body>
</html>
In display(), change the call to ProdTotal() to
list($totqty, $totamt) = $this->ProdTotal($producta);