export student result in php to excel - php

I made a Student Result page. I wanted to export the results to excel. For that I made excel.php. When I click on the export button it is showing that there is no results. But results are showing in the webpage. Please find the attached screenshot of results and help me to solve the issue.
rsltmng.php
<?php
error_reporting(0);
session_start();
include_once '../oesdb.php';
/************************** Step 1 *************************/
if(!isset($_SESSION['admname'])) {
$_GLOBALS['message']="Session Timeout.Click here to Re-LogIn";
}
else if(isset($_REQUEST['logout'])) {
/************************** Step 2 - Case 1 *************************/
//Log out and redirect login page
unset($_SESSION['admname']);
header('Location: index.php');
}
else if(isset($_REQUEST['dashboard'])) {
/************************** Step 2 - Case 2 *************************/
//redirect to dashboard
header('Location: admwelcome.php');
}
else if(isset($_REQUEST['back'])) {
/************************** Step 2 - Case 3s *************************/
//redirect to Result Management
header('Location: rsltmng.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<title>Manage Results - OES</title>
</head>
<body>
<div id="logmsk" style="display: block;">
<?php include("header.php"); ?>
<div id="page3">
<form name="rsltmng" action="rsltmng.php" method="post">
<div id="navigationbar">
<?php if(isset($_SESSION['admname'])) {
// Navigations
?>
<li><input type="submit" value="Logout" name="logout" class="aclass2" title="Log Out"/></li>
<?php if(isset($_REQUEST['testid'])) { ?>
<li><input type="submit" value="Back" name="back" class="aclass2" title="Manage Results"/></li>
<?php }else { ?>
<li><input type="submit" value="DashBoard" name="dashboard" class="aclass3" title="Dash Board"/></li>
<?php } ?>
</div>
<div><p style="font-weight:bold; color:#F00; text-align:center"><?php if($_GLOBALS['message']){ echo ($_GLOBALS['message']);}?></p></div>
<?php
if(isset($_REQUEST['testid'])) {
/************************** Step 3 - Case 1 *************************/
// Defualt Mode: Displays the Detailed Test Results.
$result=executeQuery("select t.testname,DATE_FORMAT(t.testfrom,'%d %M %Y') as fromdate,DATE_FORMAT(t.testto,'%d %M %Y %H:%i:%S') as todate,sub.subname,IFNULL((select sum(marks) from question where testid=".$_REQUEST['testid']."),0) as maxmarks from test as t, subject as sub where sub.subid=t.subid and t.testid=".$_REQUEST['testid'].";") ;
if(mysql_num_rows($result)!=0) {
$r=mysql_fetch_array($result);
?>
<table cellpadding="5" cellspacing="10" border="0" align="center" style="background:#ffffff url(../images/page.gif);text-align:left;line-height:20px;">
<tr>
<td colspan="2"><h3 style="color:#0000cc;text-align:center;"><u>Test Summary</u></h3></td>
</tr>
<!-- <tr>
<td colspan="2" ><hr style="color:#ff0000;border-width:4px;"/></td>
</tr>-->
<tr>
<td>Test Name:</td>
<td><b><?php echo htmlspecialchars_decode($r['testname'],ENT_QUOTES); ?></b></td>
</tr>
<tr>
<td>Subject Name:</td>
<td><b><?php echo htmlspecialchars_decode($r['subname'],ENT_QUOTES); ?></b></td>
</tr>
<tr>
<td>Validity:</td>
<td><b><?php echo $r['fromdate']." To ".$r['todate']; ?></b></td>
</tr>
<tr>
<td>Max. Marks:</td>
<td><b><?php echo $r['maxmarks']; ?></b></td>
</tr>
<tr><td colspan="2"><hr style="color:#ff0000;border-width:2px;"/></td></tr>
<!--<tr>
<td colspan="2"><h3 style="color:#0000cc;text-align:center;">Attempted Students</h3></td>
</tr>
<tr>
<td colspan="2" ><hr style="color:#ff0000;border-width:4px;"/></td>
</tr>-->
</table>
<table><tr><td><h3 style="color:#0000cc;text-align:center;"><u>Attempted Students</u></h3></td></tr></table>
<?php
$result1=executeQuery("select s.fname,s.contactno,s.emailid,IFNULL((select sum(q.marks) as om from studentquestion as sq, question as q where sq.testid=q.testid and sq.qnid=q.qnid and sq.answered='answered' and sq.stdanswer=q.correctanswer and sq.stdid=st.stdid and sq.testid=".$_REQUEST['testid']." order by sq.testid),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid']." order by om DESC;" );
if(mysql_num_rows($result1)==0) {
echo"<h3 style=\"color:#0000cc;text-align:center;\">No Students Yet Attempted this Test!</h3>";
}
else {
?>
<table cellpadding="5" cellspacing="10" class="datatable">
<tr>
<th>Student Name</th>
<th>Contact Number</th>
<th>Email-ID</th>
<th>Obtained Marks</th>
<th>Result(%)</th>
</tr>
<?php
while($r1=mysql_fetch_array($result1)) {
?>
<tr>
<td><?php echo htmlspecialchars_decode($r1['fname'],ENT_QUOTES); ?></td>
<td><?php echo htmlspecialchars_decode($r1['contactno'],ENT_QUOTES); ?></td>
<td><?php echo htmlspecialchars_decode($r1['emailid'],ENT_QUOTES); ?></td>
<td><?php echo $r1['om']; ?></td>
<td><?php echo ($r1['om']/$r['maxmarks']*100)." %"; ?></td>
</tr>
<form method="post" action="excel.php"><input type="submit" name="export_excel" class="aclass3" value="Export Result" /></form>
<?php
}
}
}
else {
echo"<h3 style=\"color:#0000cc;text-align:center;\">Something went wrong. Please logout and Try again.</h3>";
}
?>
</table>
<?php
}
else {
/************************** Step 3 - Case 2 *************************/
// Defualt Mode: Displays the Test Results.
$result=executeQuery("select t.testid,t.testname,DATE_FORMAT(t.testfrom,'%d %M %Y') as fromdate,DATE_FORMAT(t.testto,'%d %M %Y %H:%i:%S') as todate,sub.subname,(select count(stdid) from studenttest where testid=t.testid) as attemptedstudents from test as t, subject as sub where sub.subid=t.subid;");
if(mysql_num_rows($result)==0) {
echo "<h3 style=\"color:#0000cc;text-align:center;\">No Tests Yet...!</h3>";
}
else {
$i=0;
?>
<table cellpadding="10" cellspacing="10" class="datatable">
<tr>
<th>Test Name</th>
<th>Validity</th>
<th>Subject Name</th>
<th>Attempted Students</th>
<th>Details</th>
</tr>
<?php
while($r=mysql_fetch_array($result)) {
$i=$i+1;
if($i%2==0) {
echo "<tr class=\"alt\">";
}
else { echo "<tr>";}
echo "<td>".htmlspecialchars_decode($r['testname'],ENT_QUOTES)."</td><td>".$r['fromdate']." To ".$r['todate']." PM </td>"
."<td>".htmlspecialchars_decode($r['subname'],ENT_QUOTES)."</td><td>".$r['attemptedstudents']."</td>"
."<td class=\"tddata\"><a title=\"Details\" href=\"rsltmng.php?testid=".$r['testid']."\"><img src=\"../images/detail.png\" height=\"30\" width=\"40\" alt=\"Details\" /></a></td></tr>";
}
?>
</table>
<?php
}
}
closedb();
}
?>
</form>
</div>
<div style="position: relative; clear:both; z-index:10; height:3em; margin-top:-3em"><?php include("footer.php"); ?></div>
</div>
</body>
</html>
excel.php
<?php
$connect = mysqli_connect("localhost", "root", "", "ssmc_ae_drsunny");
$output = '';
$_REQUEST['testid']=isset($_REQUEST['testid']);
if(isset($_POST["export_excel"]))
{
$sql=executeQuery("select s.fname,s.contactno,s.emailid,IFNULL((select sum(q.marks) as om from studentquestion as sq, question as q where sq.testid=q.testid and sq.qnid=q.qnid and sq.answered='answered' and sq.stdanswer=q.correctanswer and sq.stdid=st.stdid and sq.testid=".$_REQUEST['testid']." order by sq.testid),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid']." order by om DESC;" );
$result=mysqli_query($connect, $sql);
if(mysqli_num_rows($result)>0)
{
$output .= '
<table cellpadding="5" cellspacing="10" class="datatable">
<tr>
<th>Student Name</th>
<th>Contact Number</th>
<th>Email-ID</th>
<th>Obtained Marks</th>
<th>Result(%)</th>
</tr>
';
while ($row=mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row['fname'].'</td>
<td>'.$row['contactno'].'</td>
<td>'.$row['emailid'].'</td>
<td>'.$row['om'].'</td>
<td>'.$row['om'].'/'.$row['maxmarks'].'*100."%"</td>
</tr>
';
}
$output .= '</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
echo $output;
}
else
{echo "NO RESULT"; }
}
?>

$_REQUEST['testid']=isset($_REQUEST['testid']);
This line actually makes the $_REQUEST['testid'] variable into 1 or 0.
So the query gets executed but no result gets displayed as there is no such row int the table.

Related

PHP/MySQL: Sort database results by clicking on hyperlink in the page

I have a page that executes a query from a database. There are 5 columns, and I want the first and last column to be a sorting hyperlink. This is my code. I want to be able to click on column "price" or "course ID" to sort them by the category. When I click the link, nothing happens, but the ?sort=price shows on the url. I am fairly new to PHP and MySQL and I cannot find anything online. Thank you in advance.
<?php
require_once ('mysqli_connect.php');
//grab data from database
$course = "SELECT course.courseID, course.courseTitle, course.credit,
book.bookTitle, book.price, book.isbn13
FROM course
INNER JOIN coursebook ON course.courseID=coursebook.course
JOIN book ON coursebook.book=book.isbn13
ORDER BY course.courseID";
$course_response = mysqli_query($dbc, $course);
$isbn = "SELECT isbn13 FROM book";
$isbn_response = mysqli_query($dbc, $isbn);
// Close connection to the database
mysqli_close($dbc);
?>
<!DOCTYPE html>
<html>
<!-- the head section -->
<head>
<title>Book Catalog</title>
</head>
<!-- the body section -->
<body>
<main>
<div>
<h1 align='center'>Book Catalog</h1>
<?php $array = array(); // make a new array to hold data
$index = 0;
?>
<?php if($course_response){ ?>
<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"><a href="index.php?sort=course"><b>Course #</b>
</td>
<td align="left"><b>Course Title</b></td>
<td align="left"><b>Book Image</b></td>
<td align="left"><b>Book Title</b></td>
<td align="left"><b>Price</b>
</td>
</tr>
<?php
if ($_GET['sort'] == 'price') {
$query .= " ORDER BY book.price";
}elseif ($_GET['sort'] == 'course') {
$query .= " ORDER BY Description";
}
?>
<?php while($row = mysqli_fetch_array($course_response)){ ?>
<tr>
<td align="left">
<a href="www.barnesandnoble.com">
<?php echo $row['courseID']; ?></a></td>
<td align="left"> <?php echo $row['courseTitle']; ?> (<?php echo
$row['credit']; ?>)</td>
<td align="left"> <a href='bookDetails.php'>
<?php $isbnrow = $row['isbn13']; ?><img src='images/<?php echo
$isbnrow?>.jpg' width='90px' height='100px'></a></td>
<td align="left"> <?php echo $row['bookTitle']; ?></td>
<td align="left"> <?php echo "$" . $row['price']; ?></td>
</tr>
<?php } ?>
<?php } else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
} ?>
</table>
</div>
</main>
</body>
</html>
You are not passing the get paramaters to the mysql query to order the results.
update your codes by following code,
<?php
require_once ('mysqli_connect.php');
//grab data from database
$order_by = null;
if($_GET){
switch($_GET['sort']){
case 'price' :
$order_by = 'ORDER BY book.price ASC';
break;
case 'course' ;
$order_by = 'ORDER BY course.courseID DESC';
break
}
}
$course = "SELECT course.courseID, course.courseTitle, course.credit,
book.bookTitle, book.price, book.isbn13
FROM course
INNER JOIN coursebook ON course.courseID=coursebook.course
JOIN book ON coursebook.book=book.isbn13 ";
$course .= (!is_null($order_by)) ? $order_by : "ORDER BY course.courseID ASC";
$course_response = mysqli_query($dbc, $course);
if(!$course_response){
die("Error in query: " . mysqli_error($dbc));
}
?>
<!DOCTYPE html>
<html>
<head><title>Book Catalog</title></head>
<body>
<main>
<div>
<h1 align='center'>Book Catalog</h1>
<?php if($course_response){ ?>
<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"><a href="index.php?sort=course"><b>Course #</b></td>
<td align="left"><b>Course Title</b></td>
<td align="left"><b>Book Image</b></td>
<td align="left"><b>Book Title</b></td>
<td align="left"><b>Price</b></td>
</tr>
<?php while($row = mysqli_fetch_array($course_response)){ ?>
<tr>
<td align="left"><?php echo $row['courseID']; ?></td>
<td align="left"> <?php echo $row['courseTitle']; ?> (<?php echo $row['credit']; ?>)</td>
<td align="left"> <a href='bookDetails.php'><?php $isbnrow = $row['isbn13']; ?><img src='images/<?php echo $isbnrow?>.jpg' width='90px' height='100px'></a></td>
<td align="left"> <?php echo $row['bookTitle']; ?></td>
<td align="left"> <?php echo "$" . $row['price']; ?></td>
</tr>
<?php } ?>
<?php } else { echo 'results not found!'; } ?>
</table>
</div>
</main>
</body>
</html>
<?php
// Close connection to the database
mysqli_close($dbc);
?>

Browse through answers with "next page" and "previous page"

This is the code below! It echos out all answers for a specific question in my forum. I want about 6 answers shown on each page. Hence if there are 12 answers on one specific question then it should show the first six from the start then if the user clicks "next page" it should show the user the next 6 answers. I don't know how to fix this step in my progress. Any help is more than appreciated! Thanks.
<?php
$tbl_name2="forum_answers"; // Switch to table "forum_answer"
$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";
$result2=mysqli_query($con, $sql2)or die(mysqli_error($con));
while($rows=mysqli_fetch_array($result2)){
?>
<!DOCTYPE HTML SYSTEM>
<table id="answers" width="400" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td id="answerid">#<?php echo $rows['a_id']; ?></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" >
<tr>
<?php
INCLUDE 'dbh.php';
$query = mysqli_query($conn,"SELECT * FROM forum_answers LEFT JOIN users ON forum_answers.a_name = users.username WHERE forum_answers.a_id=".$rows['a_id']." AND forum_answers.question_id=".$rows['question_id']);
while ($row = mysqli_fetch_assoc($query)) {
if ($row ['image'] == "") {
echo "<img src='bilder/default.jpg'";
?><td width="77%" bgcolor="#F8F7F1"> <?php echo $rows['a_name']; ?></td><?php
}
else {
echo "<img src='bilder/".$row ['image']."'";
?> <td width="77%" bgcolor="#F8F7F1"> <?php echo $rows['a_name']; ?></td><?php
}
echo "<br>";
}
?>
<td id="datetimeanswer" bgcolor="#F8F7F1"><?php echo $rows['a_datetime']; ?></td>
</tr>
<tr>
<td id="answertext" bgcolor="#F8F7F1"><strong>Answer:</strong></td>
<td bgcolor="#F8F7F1"><?php echo $rows['a_answer']; ?>
<button id="removeanswer" name="remove">Remove</button>
<button id="removeanswer">Edit</button>
</td>
</tr>
</table></td>
</tr>
</table>

Ping selected IPs from checkbox on PHP-MySQL query

I have an idea and wonder if it can be or not on PHP since I'm new to PHP. I need to ping selected computer name or IP address and give me the status on ping status box as online or offline.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Books</title>
</head>
<body>
<form action="Untitled-1.php" method="post">
<input type="text" name="search"/>
<input type="submit" value="search"/>
<input type="button"value="ping"/>
</form>
</form>
<br/>
<table border="1">
<thead>
<tr>
<th>username</th>
<th>desktop</th>
<th>ip address</th>
<th>select pc's</th>
<th>ping status</th>
</tr>
</thead>
<?php
include("db.php");
$word = isset ($_POST['search']) ? $_POST['search'] : "";
$result=mysql_query("SELECT * FROM pc WHERE desktop like '%$word%'");
while($test = mysql_fetch_array($result)) {
$id = $test['user_id'];
?>
<tr align='center'>
<td><font color='black'><?php echo $test['username'] ?></font></td>
<td><font color='black'><?php echo $test['desktop'] ?> </font></td>
<td><font color='black'><?php echo $test['ip_address'] ?></font></td>
<td><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td>
<td><font color='black'><?php echo $test['ping_status'] ?></font></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
please help
Pass ip in this function it give status of host.
<?php
function ping_host($host){
exec("ping -c 4 " . $host, $output, $result);
return $result==0?"online":"offline";
}
echo ping("www.google.com"); // function call
?>
Note:Here 4 is number of ping you want you can change according to requirement in linux system if you not set this it will ping forever.
thanks alot Sunil for try to help me , i found the selution and it's work fine with me now
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<?php
require("db.php");
?>
<body>
<form method="post">
<table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example">
<div class="alert alert-info">
<strong><i class="icon-user icon-large"></i></strong>
</div>
<thead>
<tr>
<th>username</th>
<th>desktop</th>
<th>ip address</th>
<th>select</th>
<th>status</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from pc")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['ip_address'];
?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['desktop'] ?></td>
<td><?php echo $row['ip_address'] ?></td>
<td>
<input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> </td>
<td><?php echo $row['ping_status'] ?></td>
</tr>
<?php }
if (isset($_POST['submit'])){
if(!empty($_POST['selector'])){
foreach($_POST['selector'] as $id){
if (!$socket = #fsockopen($id, 80, $errno, $errstr, 30))
{ $status= "offline";
echo $status; }
else
{ $status= "online";
echo $status;
fclose($socket); }
$sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'";
$query=mysql_query($sql);
header("Location:test.php");
}
}
}
?>
</tbody>
</table>
<button class="btn btn-success" name="submit" type="submit">
ping
</button>
</form>
</body>
</html>
please find the last solution ,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<?php
require("db.php");
?>
<body>
<form method="post">
<table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example">
<div class="alert alert-info">
<strong><i class="icon-user icon-large"></i></strong>
</div>
<thead>
<tr>
<th>username</th>
<th>desktop</th>
<th>ip address</th>
<th>select</th>
<th>status</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from pc")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['ip_address'];
?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['desktop'] ?></td>
<td><?php echo $row['ip_address'] ?></td>
<td>
<input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> </td>
<td><?php echo $row['ping_status'] ?></td>
</tr>
<?php }
$sql1="UPDATE pc
SET ping_status = NULL
WHERE ping_status is not null";
$query1=mysql_query($sql1);
function pingAddress($id) {
$pingresult = exec("ping -n 1 $id && exit", $output, $result);
//echo $result. "<br/>";
global $status;
if (($result == 0)){
if(count(preg_grep('/Destination host unreachable/i', $output)) == 0){
$status="online <br/>";
echo $status;
}else
$status="offline <br/>";
echo $status;
}
elseif ($result == 1){
$status="offline <br/>";
echo $status;
}
}
if (isset($_POST['submit'])){
if(!empty($_POST['selector'])){
foreach($_POST['selector'] as $id){
echo $id."";
pingAddress($id);
$sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'";
$query=mysql_query($sql);
header("Location:test.php");
}
}
}
?>
</tbody>
</table>
<button class="btn btn-success" name="submit" type="submit">
ping
</button>
</form>
</body>
</html>

mysql_fetch_array(), is not work in my program

i need to fetch multiple row or single row from mysql table, but my code retrieve data from table only multiple rows as given in my where clause condition is true, not fetch single row even my condition is true. plz suggest me anyone. (i know only little bit english)
<?php
include 'connection/db_connection.php';
$sqlquery=mysql_query("select * from sadmin_invoiceno order by sno desc");
$row1=mysql_fetch_assoc($sqlquery);
$invoice=$row1['invoice_no'];
/* if($row1['invoice_no']<=9)
{
$myvalue=$row1['invoice_no'];
$idvalue="".$myvalue;
}
if($row1['invoice_no']>9 && $row1['sno']<100)
{
$myvalue=$row1['invoice_no'];
$idvalue="".$myvalue;
} */
$sql="SELECT * FROM sadmin_sales where invoice_no='".$invoice."'";
$result = mysql_query($sql);
$count=mysql_num_rows($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>IT Flower invoice</title>
<link rel="stylesheet" href="assets/css/style_popup.css" media="all" />
<link href="assets/css/utopia-white.css" rel="stylesheet">
</head>
<body>
<div class="invoice">
<header class="clearfix">
<div id="logo">
<img src="assets/img/utopia-logo1.png">
</div>
<div id="company">
<h2 class="name">Indian Traditional Flowers</h2>
<div>10 - 3500 McNicoll Ave., <br>Toronto, Ontario ON M1V 4c7</div>
<div>+1.647.836.9999</div>
<div>info#itflowers.com</div>
</div>
</header>
<main>
<div id="details" class="clearfix">
<div id="client">
<div class="to">INVOICE TO:</div>
<h2 class="name"><?php
$sql2=mysql_query("SELECT franchies, customer FROM sadmin_sales where invoice_no='".$invoice."'");
$res1=mysql_fetch_array($sql2);echo $res1['customer'];
$franchies= $res1['franchies'];
$sql3=mysql_query("SELECT * FROM sadmin_customer where email='".$franchies."'");
$res2=mysql_fetch_array($sql3);
echo $res2['shop_name'];
?></h2>
<div class="address"> <?php echo $res2['owner_name']; ?> </div>
<div class="address"><?php echo $res2['address']; ?></div>
<div class="email"><?php echo $res2['email']; ?></div>
<div class="email"><?php echo $res2['phone']; ?></div>
</div>
<div id="invoice">
<h1>INVOICE NO: <?php echo $invoice; ?></h1>
<div class="date">Date of Invoice: <?php echo $date = date('d/m/Y h:i:s a'); ; ?></div>
</div>
</div>
<table class="inventory" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr style="text-align: center;">
<th class="no">#</th>
<th class="desc">DESCRIPTION</th>
<th class="unit">UNIT PRICE</th>
<th class="qty">QUANTITY</th>
<th class="total">TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$tax=0;
$ship=0;
$paid=0;
$bal=0;
$grand=0;
$sum=0;
for($i=1; $i<$count;)
{
while($row = mysql_fetch_array($result))
{
$franchies = $row['franchies'];
echo "<td class='no'>".$i."</td> ";
echo "<td class='desc'>" . $row['product_name'] . "</td>";
echo "<td class='unit'>" . $row['unit_price'] . "</td>";
echo "<td class='qty'>" . $row['quantity'] . "</td>";
echo "<td class='total' >".$row['total']."</td>";
echo "</tr>";
$sum=$sum + $row['total'];
$i++;
//$tax=$sum *.13;
$include=$row['tax_type'];
if ($include =='includetax')
{
$tax1=mysql_query("select * from sadmin_tax where tax_type like 'include%'");
$res1=mysql_fetch_array($tax1);
$tax2=$res1['percentage'];
$tax=$sum * $tax2/100;
$sum1=$sum - $tax;
}
else
{
$tax3=mysql_query("select * from sadmin_tax where tax_type like 'exclude%'");
$res3=mysql_fetch_array($tax3);
$tax4=$res3['percentage'];
$tax=$sum * $tax4/100;
$sum1=$sum + $tax;
}
$ship=$row['shipping_amt'];
$paid=$row['paid_amt'];
$grand=$sum1 + $ship;
$bal=$grand - $paid;
}
}
?>
</tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td colspan="2">SUBTOTAL</td>
<td>$ <?php echo $sum; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">TAX 13%</td>
<td>$ <?php echo $tax; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">SHIP</td>
<td>$<?php echo $ship; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">PAID</td>
<td>$<?php echo $paid; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">BALANCE</td>
<td>$<?php echo $bal; ?></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">GRAND TOTAL</td>
<td>$<?php echo $grand; ?></td>
</tr>
</tfoot>
</table>
<button onclick="myFunction()" class="btn btn-primary" style="width: 106px;float: right;">Print this page</button>
<!-- <div id="thanks">Thank you!</div>
<div id="notices">
<div>NOTICE:</div>
<div class="notice">A finance charge of 1.5% will be made on unpaid balances after 30 days.</div>
</div>
</main>
<footer>
Invoice was created on a computer and is valid without the signature and seal.
</footer> -->
</div>
<script>
function myFunction() {
window.print();
}
</script>
</body>
</html>
I'm not certain I understand your question fully, are you saying you have multitple rows but only want one?
If so , check out MySQL "LIMIT " which will limit your SQL query to X number of rows, such as SELECT * FROM sadmin_customer where email='".$franchies." LIMIT 1
This will return only 1 row, the first row where the conditions are true.

Cannot fix the select/delete function. PHP/MySQL/JavaScript

guys I have the following script for adding, editing and deleting content in mysql, and showing it at index.php file. So here is my index.php file:
<script>function goDel()
{
var recslen = document.forms[0].length;
var checkboxes=""
for(i=1;i<recslen;i++)
{
if(document.forms[0].elements[i].checked==true)
checkboxes+= " " + document.forms[0].elements[i].name
}
if(checkboxes.length>0)
{
var con=confirm("Are you sure you want to delete");
if(con)
{
document.forms[0].action="delete.php?recsno="+checkboxes
document.forms[0].submit()
}
}
else
{
alert("No record is selected.")
}
}
function selectall()
{
// var formname=document.getElementById(formname);
var recslen = document.forms[0].length;
if(document.forms[0].topcheckbox.checked==true)
{
for(i=1;i<recslen;i++) {
document.forms[0].elements[i].checked=true;
}
}
else
{
for(i=1;i<recslen;i++)
document.forms[0].elements[i].checked=false;
}
}
</script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td><hr size="1" noshade></td>
</tr>
<tr>
<td>
<form action="" method="post" name="" id="">
<table width="600" border="1" align="center" cellpadding="2" cellspacing="2">
<tr>
<td><input name="topcheckbox" type="checkbox" class="check" id="topcheckbox" onClick="selectall();" value="ON">
Select All </td>
<td colspan="3" align="center">Add New Branch </td>
</tr>
<tr>
<td><strong>Delete</strong></td>
<td><strong>Branch Name </strong></td>
<td><strong>Short Name </strong></td>
<td><strong>Update</strong></td>
</tr>
<?
include("conn.php");
$sql="select sn,branchname,shortname from $branch order by sn";
$result=mysql_query($sql,$connection) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
?>
<tr>
<td><input name="<? echo $row['sn']; ?>" type="checkbox" class="check"></td>
<td><? echo $row['branchname']; ?></td>
<td><? echo $row['shortname']; ?></td>
<td>Update</td>
</tr>
<? } ?>
</table>
</form></td>
</tr>
</table>
Here is my delete.php file:
<?php
include("conn.php");
$recsno=$_GET["recsno"];
$data=trim($recsno);
$ex=explode(" ",$data);
$size=sizeof($ex);
for($i=0;$i<$size;$i++) {
$id=trim($ex[$i]);
$sql="delete from $branch where sn='$id'";
$result=mysql_query($sql,$connection) or die(mysql_error());
}
header("location: index.php");
?>
The problem is when I check 1 row, and click delete, it is deleting all of the rows, like I've clicked select all (which I didn't do). Thank you in advance.
It seems Delete query not getting value of $id, you have to store values in $id.
just GET id values
$id = $_REQUEST["sn"];
then your delete query
$sql="delete from $branch where sn='$id'";

Categories