Print page output into a file - php

I have a reviewfile.php whereby i read the file uploaded and print results based on the conditions i coded.
I would like to know how to put my page results into a file.
This is so that i can download as pdf later on.
Below is my codes
if(isset($_POST['reviewFile'])){
if (isset($_POST['fileSelected'])){
$SelFile = $_POST['fileSelected'];
echo "<b>Filename:</b> $SelFile";
$query=$con->prepare("SELECT * from file where file_name = '$SelFile'");
$query->bind_result($file_id, $uid, $file_name, $file_size);
$query->execute();
$query->fetch();
$query->close();
$path = 'C:/xampp/htdocs/fwrule/fw/fileuploads/';
$file_to_get = $path.$SelFile;
$_SESSION['tmpfname'] = $file_to_get;
$_SESSION['excelReader'] = PHPExcel_IOFactory::createReaderForFile($_SESSION['tmpfname']);
$_SESSION['excelObj'] = $_SESSION['excelReader']->load($_SESSION['tmpfname']);
$_SESSION['worksheet'] = $_SESSION['excelObj']->getSheet(0);
$_SESSION['lastRow'] = $_SESSION['worksheet']->getHighestRow();
$_SESSION['highestColumn'] = $_SESSION['worksheet']->getHighestColumn();
//loop through each row of the worksheet in turn
for ($_SESSION['row'] = 3; $_SESSION['row'] <= $_SESSION['lastRow']; ++$_SESSION['row']) {
//read a row of data into an array
$rowData = $_SESSION['worksheet']->rangeToArray('A'.$_SESSION['row']. ':'.$_SESSION['highestColumn'] . $_SESSION['lastRow'], NULL, TRUE, FALSE);
//insert rows into database
$InsertData = $con->prepare("INSERT INTO file_data(id, file_id, no, name, source, destination, hit_counts, service, action, track, install_on, time, comment)
VALUES('', '$file_id', '".$rowData[0][0]."', '".$rowData[0][1]."', '".$rowData[0][2]."', '".$rowData[0][3]."', '".$rowData[0][4]."', '".$rowData[0][5]."', '".$rowData[0][6]."', '".$rowData[0][7]."', '".$rowData[0][8]."', '".$rowData[0][9]."', '".$rowData[0][10]."')") or die($con->error);
$InsertData->execute() or die ($InsertData->error);
$InsertData->store_result();
$InsertData->close();
}
if ($DuplicatedRows = $con->prepare("SELECT id
from file_data a
join (SELECT source, destination, hit_counts, service
from file_data
group by source, destination, hit_counts, service
having count(*) > 1 ) b
on a.source = b.source
and a.destination = b.destination
and a.hit_counts = b.hit_counts
and a.service = b.service;"))
{
$DuplicatedRows->bind_result($id);
$DuplicatedRows->execute();
$DuplicatedRows->store_result();
while($DuplicatedRows->fetch())
{
$rowNumforDuplicates = ($id+2);
$info1 = "Rule is duplicated";
echo "<tr>";
echo "<td>".$rowNumforDuplicates."</td>";
echo "<td>".$info1."</td>";
echo "</tr>";
}
$numRow = $DuplicatedRows->num_rows;//Checking rows in database
if ($numRow == 0)
{
echo "all perfect!";
}
//echo $numRow;
$DuplicatedRows->free_result();
$DuplicatedRows->close();
}
else{
?>
<html>
<script>
window.alert("Error message:: %s\n", $con->error");
</script>
</html>
<?php
}
if ($checkforAny = $con->prepare("SELECT id FROM file_data WHERE (source collate latin1_swedish_ci = 'any') or (destination collate latin1_swedish_ci = 'any')"))
{
$checkforAny->bind_result($id);
$checkforAny->execute();
$checkforAny->store_result();
while($checkforAny->fetch())
{
//row number in excel as the file is reviewed from line 3 onwards, hence 2 is added to the ID value
$rowNumforAny = ($id+2);
$info2 = "Source or Destination contains 'Any'";
echo "<tr>";
echo "<td>".$rowNumforAny."</td>";
echo "<td>".$info2."</td>";
echo "</tr>";
//echo "<br>";
//echo "The one with Any: $rowNumforAny";
}
$checkforAny->free_result();
$checkforAny->close();
}
else
{
?>
<html>
<script>
window.alert("Error message:: %s\n", $con->error");
</script>
</html>
<?php
}
if ($checkforComments = $con->prepare("SELECT id FROM file_data WHERE comment = '' "))
{
$checkforComments->bind_result($id);
$checkforComments->execute();
$checkforComments->store_result();
while($checkforComments->fetch())
{
$rowNumforComments = ($id+2);
$info3 = "Comment for rule is missing";
echo "<tr>";
echo "<td>".$rowNumforComments."</td>";
echo "<td>".$info3."</td>";
echo "</tr>";
}
echo "</table>";
//echo 'Save results as PDF';
$checkforComments->free_result();
$checkforComments->close();
}
else
{
?>
<html>
<script>
window.alert("Error message:: %s\n", $con->error");
</script>
</html>
<?php
}
if($DuplicatedRows == true && $checkforAny == true && $checkforComments == true)
{
//Truncate table "file_data" after each review so that data will not overlap.
$delAllData = $con->prepare("TRUNCATE file_data");
if ($delAllData->execute())
{
}
$delAllData->close();
}
else
{
?>
<html>
<script>
window.alert("Error message:: %s\n", $con->error");
</script>
</html>
<?php
}
}
if (!isset($_POST['fileSelected']))
{
?>
<html>
<script>
window.alert("Please select a file to review!");
window.location = 'https://localhost/fwrule/fw/filerecords.php';
</script>
</html>
<?php
}
}

You already have queries that find where problems EXIST, so why not use a NOT EXISTS...
select fd1.id
from file_data fd1
where not exists
(
SELECT 1 FROM file_data fd2 WHERE (source collate latin1_swedish_ci = 'any' or destination collate latin1_swedish_ci ='any') and fd2.id = fd1.id
)
and not exists
(
SELECT 1 FROM file_data fd3 WHERE comment = '' and fd3.id = fd1.id
)
and not exists
(
SELECT 1
from file_data a
join ( SELECT source, destination, hit_counts, service
from file_data
group by source, destination, hit_counts, service
having count(*) > 1 ) b
on a.source = b.source
and a.destination = b.destination
and a.hit_counts = b.hit_counts
and a.service = b.service
and a.id = fd1.id
)

Related

PHP MYSQL count two tables row and insert

I have two tables and I want to count how many 1 is in 1-2 table. If it is same amount than you can insert 1 if not than you can not insert 1 in to the table.
My code insert data even when it is equal or not:
<?php
$msg = '';
if(isset($_POST['rogzit_in'])) {
$barcode = $_POST['barcode'];
// Belépés ellenőrzése
if(!isset($_POST['barcode'])) {
$sql_barcodeInEll = "SELECT COUNT(barcode) FROM log_in WHERE barcode = '$barcode'";
$result_barcodeInEll = mysqli_query($dbCon, $sql_barcodeInEll);
$sql_barcodeOutEll = "SELECT COUNT(barcode) FROM log_out WHERE barcode = '$barcode'";
$result_barcodeOutEll = mysqli_query($dbCon, $sql_barcodeOutEll);
if(mysqli_num_rows($result_barcodeInEll) != $result_barcodeOutEll) {
$msg = '<h4 class="col-12 text-center mb-3 text-danger">Már bejelntkezett!</h4>';
}
}
if(isset($_POST['barcode'])) {
$sql_beszuras = "INSERT INTO log_in(barcode) VALUES ('$barcode')";
if(mysqli_query($dbCon, $sql_beszuras)) {
?>
<script type="text/javascript">
window.location = "home.php/";
</script>
<?php
}
}
}
?>
Thank you for the help!

How to retrieve data after insert with new value in another table

<?php include('dbcon.php');
include('header.php');
//variable
$clientID='';
$billAmount='';
$arrear='';
$monthlyBill='';
$surcharge='';
//data add
$clientID=$_POST['clientID'];
$sqll = "SELECT `client`.`clientId` , (
`arrear` + `surcharge` + `monthlyBill`) AS 'billamount', (
FROM `billifno`
JOIN `client` ON `billifno`.`clientID` = `client`.`clientID` WHERE `billifno`.`clientID`='".$_POST["clientID"]."'";
$result = mysqli_query($con,$sqll);
// if ($result->num_rows > 0)
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$clientId = $row['clientId'];
$arrear = $row['arrear'];
$monthlyBill= $row['monthlyBill'];
$billAmount = $row['billAmount'];
$surcharge = $row['surcharge'];
}
//echo $billAmount;
$sql = "INSERT INTO `billIfno`
(`clientID`, `billAmount`,`arrear`, `monthlyBill`,`surcharge`) VALUES
('$clientID','$billAmount','$arrear','$monthlyBill','$surcharge')";
if ($con->query($sql)=== true)
{
echo "Recorded!!!";
}
else
{
echo "Not Recorded !!!";
}
$con->close();
?>
Hare I am taking some value and process it using query then again insert into billInfo table but it not inserting the new values like
If I echo billAmonu it print the correct value that is calculated by query bt that value is not inserting into database.

how to check the exisitng data from database before inserting

This is my code and it does not work, it's not inserting into the database. Please help me fix this problem.
$orgexist = $_POST['orgName1'];
$_SESSION['id'] = $_POST['id'];
$orgid = $_POST['id'];
$orgnme = $_POST['orgName1'];
$orgdesc = $_POST['orgDesc'];
$orgcat = $_POST['cat'];
$orgdept = $_POST['coldept'];
$orgvis = $_POST['vision'];
$orgmis = $_POST['mision'];
//get the value of category from database
//echo $orgdept;
$dept = "SELECT `col_id`, `col_description` FROM `college` WHERE `col_description` = '$orgdept'";
$deptresult = mysql_query($dept);
while ($rows = mysql_fetch_array($deptresult)) {
$getcol = $rows['col_id'];
//echo $getcol;
}
$sqlorg = mysql_query("SELECT * FROM `organization`");
while ($orgrows = mysql_fetch_array($sqlorg)) {
//$dborgid = $orgrows['org_id'];
$dborgnme = $orgrows['org_name'];
}
if ($dborgnme == $orgexist) {
echo "<script type='text/javascript'>
alert('Organization Name Already Used by other Organization');
history.back();
</script>";
} else {
$orginsrt = mysql_query("INSERT INTO `organization`(`org_id`,`org_name`,`org_desc`,`category`,`vision`,`mission`,`col_id`,`image`) VALUES ('$orgid','$orgexist','$orgdesc','$orgcat','$orgvis','$orgmis','$getcol','$image')");
echo "<script type='text/javascript'>
alert('Proceed to next Step');</script>";
//require ('orgsignup.php');
header('Location:orgsignup2.php');
//echo "Not in the Record";
}
}
You're using deprected functions, replace mysql_query by mysqli_query.
For more references see http://php.net/manual/en/function.mysql-query.php

query wongt output to table. correct syntax

What am i doing wrong here?
(Know its mysql)
this else is not working it look like. user can insert same modul id many times.
$besvarelse = $_GET['besvarelse'];
$modulid = $_GET['modulid'];
$username = $_GET['username'];
$tilkobling = kobleTil();
if (!empty($besvarelse) ) {
$insert = mysql_query("INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')");
if($insert) {
echo "<h1>Levering OK</h1><br>";
}
}
else {
die("<h1>Du har lever før</h1>");
}
?>
Try with this and post the errors that PHP shows, please:
//Assuming that connection to mysql server its done somewhere with mysql_connect()
error_reporting(E_ALL); ini_set('display_errors', 1);
explode($_GET);
echo "<pre>";
print_r($_GET); //Just to see what's on the variables...
echo "</pre>;
// $tilkobling = kobleTil(); Need to explain this line
if (!empty($besvarelse) ) {
$sql = "INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')";
if($insert = mysql_query($sql))
{
echo "<h1>Levering OK</h1><br>";
}
else {
die("<h1>Du har lever før</h1>");
}
} // you were missmatching the IF closing bracket
?>
try to check for exist rows like
$besvarelse = $_GET['besvarelse'];
$modulid = $_GET['modulid'];
$username = $_GET['username'];
$tilkobling = kobleTil();
if (!empty($besvarelse) ) {
// check exist moduleid
$check = mysql_query("select * from oppgave where modulid = '$modulid'");
$conut_rows= mysql_num_rows($check);
if($conut_rows > 0) {
echo "module id already exist";
}
else {
$insert = mysql_query("INSERT INTO oppgave (besvarelse, modulid, username) VALUES ('$besvarelse', '$modulid','$username')");
if($insert) {
echo "<h1>Levering OK</h1><br>";
}
else {
die("<h1>Du har lever før</h1>");
}
}
}

get names of online users

as i have mentioned at my earlier post, we are creating a chat for a specific website. Now this chat would have to retrieve the names of the users online and would automatically update once one user would log out of the chat. we were able to create this with the use of PHP alone and right now we are trying to use jquery to avoid often refreshing.so far, this is what we have:
<?php
session_start(); //Configuation
?>
<link rel="stylesheet" type="text/css" href="http://www.pinoyarea.com/videochat/css/winterblues.css">
<?php
$name = $_SESSION['username'];
$room = $_SESSION['room'];
$user = $_SESSION['user'];
if($name == NULL || $room == NULL || $user = NULL)
{
echo "<script>window.location = 'http://www.pinoyarea.com/index.php?p=member/signup';</script>";
}
include "connect.php";
$timeoutseconds = 60; // length of session, 20 minutes is the standard
$timeoutseconds_idle = 30;
$timestamp=time();
$timeout=$timestamp-$timeoutseconds;
$timeout_idle=$timestamp-$timeoutseconds_idle;
$PHP_SELF = $_SERVER['PHP_SELF'];
if (!empty($_SERVER["HTTP_CLIENT_IP"]))
{
//check for ip from share internet
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
// Check for the Proxy User
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
$ip = $_SERVER["REMOTE_ADDR"];
}
$temp = #mysql_query("SELECT * FROM useronline WHERE online_name='$name' AND online_user='$user' AND online_room='$room'");
$rowie = #mysql_num_rows($temp);
// Add this user to database
$loopcap = 0;
while ($loopcap<3){
if($rowie == 0 AND $name != NULL)
{
#mysql_query("insert into useronline values('$timestamp','$ip','$PHP_SELF','$name','$room','$user')");
}
else
{
} // in case of collision
$timestamp = $timestamp+$ip{0}; $loopcap++;
}
// Delete users that have been online for more then "$timeoutseconds" seconds
mysql_query("delete from useronline where timestamp<$timeout");
//Modified
// Select users online
$result = #mysql_query("select distinct online_name from useronline");
$result2 = #mysql_query("SELECT distinct online_name FROM useronline WHERE online_room='$room'");
$user_count = #mysql_num_rows($result2);
mysql_free_result($result);
#mysql_close();
// Show all users online
echo '<table name="tableonline" width="180px">';
if ($user_count==1)
{
echo '<tr><th>';
echo '<font size="1px" style="font-family:arial;"><strong>'.$user_count.' Toozer Online</th></tr>';
}
else
{
echo '<tr><th>'.$user_count.' Toozers Online</strong></font></th></tr></table>';
}
echo "</table><br /><table width='180px'>";
while($cell = mysql_fetch_array($result2))
{
$timestamping = $cell["timestamp"];
if($timestamping >= $timeout_idle && $timestamping < $timeout)
{
$src = "http://www.pinoyarea.com/images/videochat/user-offline.png";
}
else
{
$src = "http://www.pinoyarea.com/images/videochat/user-online.png";
}
echo '<tr><td><img src="'.$src.'"/><font size="1px" style="text-decoration:none;font-family:tahoma;"></td><td>'.$cell["online_name"].'</font>';
echo '<br /></td></tr>';
}
echo '<table>';
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script>
<script>
$(document).ready(function() {
$("#tableonline").load("online_users.php");
var refreshId = setInterval(function() {
$("#tableonline").load('online_users.php?randval='+ Math.random());}, 3000);
});
</script>
//<META HTTP-EQUIV="Refresh" CONTENT="10">
You have to put a boolean value for the user in the DB (in the user table). when he get's login ie when he enter's to his profile page the BOOL value should be changed to 1 and when he get logout change the vlaue to 0.

Categories