Cannot generate the report - php

I want to generate the report that click and choose from the category drop down list, then it will show that the order which product in the category that you select and how many of them sold. But it cannot come out, I have been find many times the errors, and I also tried the query in SQL as it showed correct. I don't know why. Please help me.
<?php
session_start();
include_once 'header_admin.php';
?>
<div class="report">
<h2>Sales Report</h2>
<div class="sales_report">
<form action="" method="POST">
<select name="category" class="category">
<option>Category</option>
<?php
include_once 'includes/dbh.php';
$query1 = "SELECT * from category";
$result1 = mysqli_query($conn, $query1);
while ($row1 = mysqli_fetch_assoc($result1)) {
$CategoryID = $row1['CategoryID'];
$rowData1 = $row1['CategoryName'];
?>
<option value="<?php echo $CategoryID; ?>"><?php echo $rowData1; ?></option>
<?php
}
?>
</select>
<button class="generate" type="submit" name="generate">Generate</button>
<table class="genreport">
<tr>
<th>Furniture ID</th>
<th>Furniture Name</th>
<th>Category Name</th>
<th>Order Date</th>
<th>Quantity</th>
<th>Total Amount</th>
</tr>
<?php
if (isset($_POST['generate'])) {
$catName = mysqli_real_escape_string($conn, $_POST['category']);
$query2 = "SELECT D.FurCode, F.FurName, C.CategoryName, O.OrderDate, D.quantity, COUNT(D.quantity * D.PriceEach)
FROM orderdetail D, furniture F, category C, ordertab O
WHERE C.CategoryName = '$catName' GROUP BY C.$catName ORDER BY C.$catName ";
$result2 = mysqli_query($conn, $query2);
if ($result2) {
while ($row2 = mysqli_fetch_array($result2)) {
$furCode = $row2['FurCode'];
$furName = $row2['FurName'];
$orderDate = $row2['OrderDate'];
$quantity = $row2['quantity'];
$priceEach = $row2['PriceEach'];
$total = $quantity * $priceEach;
?>
<tr>
<td><?php echo $furCode; ?></td>
<td><?php echo $furName; ?></td>
<td><?php echo $catName; ?></td>
<td><?php echo $orderDate; ?></td>
<td><?php echo $quantity; ?></td>
<td><?php echo $total; ?></td>
</tr>
<?php
}
}
} else {
echo '<tr><td>No Result!</td><td></td><td></td><td></td><td></td><td></td></tr>';
}
?>
</table>
</form>
</div>
</div>
</div>
</section>
</body>
</html>

Related

I want the php results to be displayed on the same page instead of a completely new page

I'm trying to search by name and display the results on the same page.
My php code and the html code are in 2 separate files. Given below is my search.php code.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = '';
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
if($count == 0) {
$output = 'There was no search result!';
}else{
while($row = mysqli_fetch_array($result)) {
$ID = $row['id'];
$Name= $row['customername'];
$Type = $row['type'];
$Phone = $row['phoneno'];
$Email = $row['email'];
$output .= '<div>' .$Name.' '.$Type.' '.$Phone.' '.$Email.'<div>';
}
}
}
?>
<?php print("$output");?>
Given below is the html code for my form.
<form action="search.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
When I search, the results are displayed on a completely new page. But I want the results to be displayed on the same page with all the existing page layout, headers and footers.
How can I achieve this?
Thank in Advance!
Assuming your first file name is index.php.
Then need to call this file on form action & little bit refactor a file. Now it have $output array which is empty. If it contains search parameter, it will fill data to $output array and then on html part it checks if $output array has data to display it.
<?php
include_once '../connection.php';
include_once '../session.php';
$output = array();
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
$output[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<?php if(count($output) > 0): ?>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php foreach($output as $row): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</form>
</body>
</html>

PHP View data from a single record by clicking on the view button

I have two pages students_list.php and students_profile.php
On students_list.php i have a table showing all records of all students and the VIEW button which when clicked it should open the students_profile.php page and get all the data from the single record selected based on the std_id
UPDATE: So now i get the id http://localhost/sms/student_profiles.php?std_id=3 when i click on the VIEW button but i am still not retrieving the data from this record selected
This is the code for students_list.php
<?php
require_once 'include/database/connect.php';
try {
$pdo = new PDO("mysql:host=$host_db;dbname=$name_db", $user_db, $pass_db);
$sql = 'SELECT * FROM students';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Could not connect to the database $name_db :" . $e->getMessage());
}
?>
<table id="data-table-default" class="table">
<thead>
<tr>
<th width="1%">#</th>
<th width="1%" data-orderable="false"></th>
<th>Name & Surname</th>
<th>Gender</th>
<th>Faculty</th>
<th>Semester</th>
<th>Mobile</th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr class="odd gradeX">
<td width="1%" class="f-s-600 text-inverse"><?php echo ($row['std_id']) ?></td>
<td width="1%" class="with-img"><?php echo ($row['std_status']) ?></td>
<td><?php echo ($row['std_name']) ?></td>
<td><?php echo ($row['std_gender']) ?></td>
<td><?php echo ($row['std_faculty']) ?></td>
<td><?php echo ($row['std_semester']) ?></td>
<td><?php echo ($row['std_mobile']) ?></td>
<td align="right">
<i class="fas fa-eye"></i> View
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and below this is the student_profile.php
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
}
?>
<h4><?php echo ($row['std_name']) ?></h4>
<div class="d-flex">
<div class="pr-4"><strong>NATIONAL#</strong> <?php echo ($row['std_number']) ?></div>
<div class="pr-4"><strong>DOB</strong> <?php echo ($row['std_dob']) ?></div>
<div class="pr-4"><strong>GENDER</strong> <?php echo ($row['std_gender']) ?></div>
<div class="pr-4"><strong>MOBILE</strong> <?php echo ($row['std_mobile']) ?></div>
</div>
<div class="pt-1 font-weight-bold">Master's Degree (MBA)</div>
<div>STD# <strong><?php echo ($row['std_id']) ?></strong></div>
<div>email#example.com</div>
You don't fecth the data from the query. So $row will never contain the array you expect:
$row = mysqli_fetch_assoc($res); //this will populate $row
in your code:
<?php
require_once('include/database/sharepoint.php');
if(isset($_GET) & !empty($_GET)){
$std_id = $_GET['std_id'];
$sql = "SELECT * FROM `students` WHERE std_id=$std_id";
$res = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($res);
}
?>

PHP how to delete a specific row from database with html table button

I am trying to delete a specific entry from the database with a button. I know this has already been asked several times, unfortunately the solutions don't really work for me. The goal would be, if I click on the button in the 3rd row, that this line is deleted. I have the problem that I always only delete the last ids or all ids at once.
Maybe someone can help me, thank you.
admin.php
<?php
include('connection.php');
include('read.php');
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
if (isset($_POST['delete'])) {
echo $row['ID'];
$deleteQuery = "DELETE FROM card WHERE id = " . $row['ID'];
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
?>
</table>
</div>
</div>
</form>
read.php
<?php
include('connection.php');
$statement = $pdo->prepare("SELECT * FROM card ORDER BY ID ASC");
$statement->execute();
$result = $statement->fetchAll();
if ($statement->rowCount() > 0) {
foreach ($statement->fetchAll() as $row) {
$id = $row['ID'];
$imagePath = $row["ImagePath"];
$sender = $row["Sender"];
$senderEmail = $row["SenderEmail"];
$receiver = $row["Receiver"];
$receiverEmail = $row["ReceiverEmail"];
$subject = $row["Subject"];
$text = $row["Text"];
$sendDate = $row["SendDate"];
$dispatched = $row["Dispatched"];
$category = $row['Category'];
}
}
?>
You can archive this by using get request without posting whole data to server.
if( !empty($_GET['id']) ){
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
header('location:youfilename.php');
exit;
}
?>
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td>Delete
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
You can modify your code like that :
<?php
include('connection.php');
include('read.php');
if( !empty($_POST) ){
foreach( $_POST as $key_post => $value_post ){
if (preg_match('/delete_/i',$key_post) ) {
$id_to_delete = (integer) str_replace('delete_','', $key_post);
$deleteQuery = "DELETE FROM card WHERE id = " . $id_to_delete;
$statement = $pdo->prepare($deleteQuery);
$statement->execute();
}
}
}
?>
<form action="admin.php" method="post">
<div class="table-wrapper">
<div class="table-scroll">
<table id="myTable">
<tr>
<th>ID</th>
<th>Kartentyp</th>
<th>Absender</th>
<th>Empfänger</th>
<th>Sendedatum</th>
<th id="smallCol">Verschickt</th>
<th id="smallCol">Bestätigung</th>
<th>Edit</th>
</tr>
<?php
foreach ($result as $row) {
if ($row['Dispatched'] == 0) {
$dispatched = 'Pending';
} else {
$dispatched = 'Versendet';
}
?>
<tr class="Alle <?php echo $row['Category']; ?> <?php echo $row['Dispatched']; ?>">
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['Sender']; ?></td>
<td><?php echo $row['Receiver']; ?></td>
<td><?php echo $row['SendDate']; ?></td>
<td><?php echo $dispatched; ?></td>
<td>Placeholder</td>
<td><input type="submit" name="delete_<?php echo $row['ID']; ?>" value="delete" >
<button data-target="modal1" class="modal-trigger">Modal</button>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</form>
Your error was you don't used the POST value to delete the row but the last ID store in the row variable that come from your query reading.
Becarfull too you make a wrong usage of the prepare function in PDO.

adding values to a different column after button click

I have a page where users can view the tickets they sent, they can cancel it as well, here's what it looks like:
What I want to happen is that when I click on the "Closed: Cancelled" button. It would show "Closed: Cancelled" in the assignee column as well. Currently this is what I only get when I click the button:
Here's my form:
<div class="container">
<div class="page-header">
<h3>My Tickets</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Time</th>
<th>Priority</th>
<th>Assignee</th>
<th>Subject</th>
<th>Problem</th>
<th>Status</th>
<th></th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT tickets.* FROM tickets INNER JOIN employee ON employee.id = tickets.employee_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." ".$row_message['lastname']; ?></td>
<td><?php echo $row_message['time']; ?></td>
<td><?php echo $row_message['priority']; ?></td>
<td><?php echo $row_message['assignee']; ?></td>
<td><?php echo $row_message['subject']; ?></td>
<td><?php echo $row_message['problem']; ?></td>
<?php if ($row_message['status']) : ?>
<td><?php echo $row_message['status']."".$row_message['assignee'];?></td>
<?php else : ?>
<td>
<form method="post" action="update-ticket-status-emp.php">
<input type="hidden" name="ticketno" value="<?php echo $row_message['ticketno']; ?>" />
<input type="submit" name="closedcan" value="Closed: Cancelled"></input>
</form>
</td>
<?php endif ; ?>
</tr>
<?php } ?>
</table>
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</div>
</div>
</div>
</div>
</div>
And here's the exec code:
<?php
if(isset($_POST['closedcan']))
{
$msg = "ClosedCan";
$status = $_POST['closedcan'];
$assignee = $_POST['closedcan'];
}
$ticketno=$_POST['ticketno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE tickets SET status = '$status' WHERE ticketno = '$ticketno'";
if(mysqli_query($con, $sql))
header("refresh:1; url=view-tickets-emp.php?msg=$msg");
else
var_dump(mysqli_error($con));
?>
PS: I know mysql is deprecated, I will change it eventually when I figure this out.
Modified the query in the exec code:
$sql = "UPDATE tickets SET status = '$status', assignee = '$assignee' WHERE ticketno = '$ticketno'";

PHP Database Filter

Im trying to have a simple select box that filters the database. I have successfully filled the database with all vallues on pageload, however I cant seem to get over the final hurdle of applying the filter. Also, once I have clicked the filter button, is it possible for the select text to stay at the text rather than returning to the default text?
[NOTE: I have pretty URL's enabled hence the no .php at the end]
The code is as followed:
<?php
$query = "SELECT * FROM Events";
echo $Type;
if (isset($_POST['filter'])) {
$Type = $_POST['value'];
$query .= " WHERE Type = '{$Type}'";
}
$result = mysql_query($query);
?>
<form action='/events' method="post" name="form_filter" >
<select class="eventList">
<option value="EVENT1">EVENT1</option>
<option value="EVENT2">EVENT2</option>
<option value="EVENT3">EVENT3</option>
<option value="EVENT4">EVENT4</option>
<option value="EVENT5">EVENT5</option>
<option value="EVENT6">EVENT6</option>
<input type="submit" name="filter" value="Filter">
</select>
</form>
<table class="table table-striped table-hover table-curved">
<thead>
<tr>
<th><b>Date</b></th>
<th><b>Event Name</b></th>
<th><b>Type</b></th>
<th><b>Region</b></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $Date = $row['Date']; ?></td>
<td><?php echo $Name = $row['Name']; ?></td>
<td><?php echo $Type = $row['Type']; ?></td>
<td><?php echo $Region = $row['Region']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Your <select> does not have a name:
<select class="eventList" name="event">
Use that name as index in the $_POST array:
$Type = $_POST['event'];
Update your code as follow
<?php
$query = "SELECT * FROM Events";
echo $Type;
if (isset($_POST)) {
$Type = $_POST['event'];
$query .= " WHERE Type = '{$Type}'";
}
$result = mysql_query($query);
?>
<form action='/events' method="post" name="form_filter" >
<select class="eventList" name="event">
<option value="EVENT1" <?php echo ($Type=="EVENT1")?'selected="selected"':'';?>>EVENT1</option>
<option value="EVENT2" <?php echo ($Type=="EVENT2")?'selected="selected"':'';?>>EVENT2</option>
<option value="EVENT3" <?php echo ($Type=="EVENT3")?'selected="selected"':'';?>>EVENT3</option>
<option value="EVENT4" <?php echo ($Type=="EVENT4")?'selected="selected"':'';?>>EVENT4</option>
<option value="EVENT5" <?php echo ($Type=="EVENT5")?'selected="selected"':'';?>>EVENT5</option>
<option value="EVENT6" <?php echo ($Type=="EVENT6")?'selected="selected"':'';?>>EVENT6</option>
<input type="submit" name="filter" value="Filter">
</select>
</form>
<table class="table table-striped table-hover table-curved">
<thead>
<tr>
<th><b>Date</b></th>
<th><b>Event Name</b></th>
<th><b>Type</b></th>
<th><b>Region</b></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysql_fetch_array($result)) { ?>
<tr>
<td><?php echo $Date = $row['Date']; ?></td>
<td><?php echo $Name = $row['Name']; ?></td>
<td><?php echo $Type = $row['Type']; ?></td>
<td><?php echo $Region = $row['Region']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
i have update this line
`if (isset($_POST)) {
and added the select name and code list code
$Type =$_POST['event'];
<select class="eventList" name="event">
<option value="EVENT1" <?php echo ($Type=="EVENT1")?'selected="selected"':'';?>>EVENT1</option>

Categories