open files(pdf,word,excel) in php(mysql database is used) - php

i am new to php and mysql.i have created a php page for uploading files and these files are saved to my local drive.In another page when a user selects a option files relating to the option are displayed in table format.one column will be link to the file in local drive.upto here i was successful.
Problem is that when clicking on the file hyperlink nothing is displayed.but when i copy address link of the file and paste in new window it is opening.please help me I am stuck for 2 weeks with problem.
To experiment I Have created simple php file just to see the hyperlink working.but no success.I will paste here the trails i have used
<?php echo "<a href=`file:///C|/Inetpub/wwwroot/help.gif' >hi</a>";?>-----not worked
<?php echo "<a href=http://www.google.com>hi</a>";?> ------ working.
even " " tags are not working if files are saved as php file.....i am using ISp server.my php files are saved in "C:\Inetpub\wwwroot\phpsample" path
please help me find solution
----my original code----
<form action="http://localhost/phpsample/upload/upload2.php" method='post'enctype='multipart/form-data'>
Instrument:<select name="flist">
<option>select</option>
<option>gt1</option>
<option>gt2</option>
<option>gt3</option>
<option>gt4</option>
</select>
</br>
Date: <input type="date" name="fdate" /></br>
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" />
</form>
<?php
global $dest;
global $name;
$dest='E:\divya';
$name=$_FILES["file"]["name"];
echo $dest;
move_uploaded_file($_FILES["file"]["tmp_name"],"$dest/$name");
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("upload_db", $con);
mysql_query("INSERT INTO upload_tb (Instrument, date_upload, filename)
VALUES('$_POST[flist]','$_POST[fdate]','$name')");
mysql_close($con);
?>
</body>
</html>
----my original code to download file from table----
<form action="http://localhost/phpsample/display/display.php" method='post'enctype='multipart/form-data'>
Instrument:
<select name="flist">
<option>select</option>
<option>gt1</option>
<option>gt2</option>
<option>gt3</option>
<option>gt4</option>
</select>
</br>
Date:
<input type="date" name="fdate" /></br>
<input type="submit" />
</form>
<?php
echo $_POST['fdate'];
echo $_POST['flist'];
$path='file:\\\C:\\Inetpub\\wwwroot\\phpsample\\display\\NewTextDocument.txt';
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("upload_db", $con);
$result = mysql_query("SELECT * FROM upload_tb WHERE Instrument='$_POST[flist]'");
echo "<table border='1'>
<tr>
<th>Slno</th>
<th>Name</th>
<th>comment</th>
</tr>";
fopen("c:\Inetpub\wwwroot\phpsample\display\NewTextDocument.txt","r");
while($row = mysql_fetch_array($result))
{
$filename=$row['filename'];
echo $filename;
echo "<tr>";
echo "<td>" . $row['Instrument'] . "</td>";
echo "<td>" . $row['date_upload'] . "</td>";
echo "<td>"."<a href= $path>". $row['filename'] ."</a>"."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>

Put this script into download.php in the same folder as upload2.php, and invoke it as
http://localhost/phpsample/upload/download.php?file=FILE
where FILE is the filename that was uploaded.
<?php
$con = mysqli_connect("localhost","root","", "upload_db");
if (!$con) die('Could not connect: ' . mysqli_connect_error());
$name = $_GET['file'];
$query_filename = mysqli_real_escape_string($dbh, $name);
$src = 'E:\divya';
$data = mysqli_query($con, "select filename from upload_tb where filename = '$query_filename'");
if (mysqli_num_rows($data) ! 1) die ('File not uploaded: ' . htmlentities($name));
$path = "$src/$name";
http_send_content_disposition($name);
$type = mime_content_type($path);
http_send_content_type($type);
http_send_file($path);
exit;
?>

Related

php: Unable to take value from the user by using submit button

This is my db : link link
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM org_insert");
echo "<!doctype html>
<html lang=\"en\">
<head>
<!-- Bootstrap CSS -->
<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css\">
<title>Hello, world!</title>
</head>
<body>
<table border='1'>
<tr>
<th>below_whom</th>
<th>name</th>
</tr>";
$row = mysqli_fetch_array($result);
#echo '<pre>'; print_r($row); echo '</pre>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['below_whom'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<div class="form-group">
<label for="usr">below_whom:</label>
<input type="text" name ="below_whom" id="below_whom" class="form-control">
</div>
<div class="form-group">
<label for="usr">name:</label>
<input type="text" name ="name" id="name" class="form-control">
</div>
<form method="post">
<input type="button" name="submit" id="submit" class="btn btn-primary" value="submit"/>
</form>
<?php
$con=mysqli_connect("localhost","root","","organisation");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['submit']))
{
if($below_whom !=''||$name !=''){
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
mysqli_close($con);
echo"</body>
</html>";
?>
The text under p tag isn't getting executed, ie, the program is not going inside the if statement itself. I have rechecked the syntax, what is the problem? Is the syntax incorrect? I am pretty sure the connection with sql is correct. I have also refereed to some articles, still I am stuck here.
use post variables before if loop as shown below
if(isset($_POST['submit']))
{
$below_whom=$_POST['below_whom'];
$name=$_POST['name'];
if($below_whom !=''||$name !=''){
$query=mysqli_query("INSERT INTO org_insert VALUES ('$below_whom','$name');");
$query_run = mysqli_query($con,$query);
echo "<p>query inserted.</p>";
}else{
echo "<p>Insertion Failed.</p>";
}
}
and in HTML Code add type as submit and start form tag before div as
<form method="post" action=""> and closes after input tag
<input type="submit" name="submit" id="submit" class="btn btn-primary" value="submit"/>
One of the issues that I am able to see is that Your query should be:
$query=mysqli_query("INSERT INTO `org_insert`(`below_whom`,`name`) VALUES ('$below_whom','$name')");
Hope this helps.
Change the mysqli_fetch_array to mysqli_fetch_assoc or add a parameter too
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

Session not passing POSTed data to second script - please advise

Tried asking this, it was marked duplicate with a suggestion. I employed the suggestions which still do not seem to be performing the desired result. Could someone actually read the following and advise where I am going wrong?
I am attempting to use sessions to pass the user selected data from this script (allotment.php) to a subsequent script (allotmentreport.php) wherein it is used in a query qualifier (e.g. ...WHERE tablecolumndata=session variable...). I am not getting an error from allotment.php upon selecting the option and clicking SUBMIT but data fails to pass to allotmentreport.php and returns an error for an undefined variable.
Is this and the line after it correct? Is there something I am missing otherwise?
$tourselect=(isset($_POST['submit']));
UPDATE The final and corrected code is displayed below for future users seeking a working example and easy to read solution:
<?php
session_start();
$host="localhost";
$username="HIDDEN";
$password="HIDDEN";
$dbname="bookings2015";
$con = mysql_connect($host, $username, $password, $dbname);
if (!$con)
{
die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
}
mysql_select_db($dbname, $con);
?>
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\phpproject1\head.php';
include 'config\menu.php';
?>
<div id="dataentry">
<div id="submit">
<?php
echo "Which Tour to check availability? ";?>
<br />
<br />
</br>
</br>
<form method="post" action="allotment_report.php">
<select name='TourCode'>
<?php
$tourselection = "SELECT DISTINCT TourCode FROM toursanddates ORDER BY TourCode";
$result = mysql_query($tourselection);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['TourCode'] . "'>" . $row['TourCode'] . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
<?php
?>
</div>
</div>
<div id="demographicborder">
<?php
include 'footer.php';?>
</div>
</div>
</body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</html>
and here is the allotment_report.php code
<?php
session_start();
$host="localhost";
$username="STILLHIDDEN";
$password="STILLHIDDEN";
$dbname="bookings2015";
$con = mysql_connect($host, $username, $password, $dbname);
if (!$con)
{
die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
}
mysql_select_db($dbname, $con);
include 'C:\xampp\htdocs\phpproject1\head.php';
include 'config\menu.php';
?>
<br />
<br />
<?php
//Table Header:
echo " <strong><u>Tour Availability</u>";
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr>
<td align=:"left"><b>Tour:</b></td>
<td align=:"left"><b>Start Date:</b></td>
<td align=:"left"><b>Seats Avail:</b></td>
<td align=:"left"><b>Rooms Avail:</b></td>
</tr>
';
if(isset($_POST['TourCode'])){
$tour=$_POST['TourCode'];
}
$status="ok";
$ar="SELECT TourCode, DATE_FORMAT (TourStart, '%m%d%y') AS TourStart, SeatsAvail, RoomsAvail FROM toursanddates WHERE TourCode='$tour' AND Status='$status' ORDER BY TourCode, TourStart ASC";
$result=mysql_query($ar);
$num_results = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
//Display the allotments fetched in above query
echo '<tr>
<td align=:"left">' . $row['TourCode'] . '</td>
<td align=:"left">' . $row['TourStart'] . '</td>
<td align=:"left">' . $row['SeatsAvail'] . '</td>
<td align=:"left">' . $row['RoomsAvail'] . '</td>
</tr>
';
}
echo '</table>';
//echo "</strong>Tour: ".($row['TourCode']);
//echo "</strong> Start Date: ".($row['TourStart']);
?>
<br />
<?php
echo "<br />";
echo "</p>";
?>
</br>
</br>
</div>
</form>
</div>
<div id="demographicborder">
<?php include 'footer.php';
?>
</div>
</div>
</body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</div>
</body>
<?php
//contained within allotment.php
$tourselect=(isset($_POST['tourselected']));
$_SESSION['tourselected']=$tourselect;
?>
It looks like you are expecting $_SESSION['tourselected'] to be set on allotment.php the when a user loads opens the page for the first time. However this is not the case. $_POST data is attached with an HTTP request. When you load alloment.php for the first time, the browser doesn't send any $_POST data to it. This would explain why $_SESSION['tourselected'] is unset when you get to your second script.
That said, if your only goal is to send data from the form built in alloment.php to alloment_report.php you shouldn't be using sessions at all. All of this can be done with only $_POST.
consider the following code:
<!--alloment.php-->
<form method="post" action="alloment_report.php">
<select name='TourCode'>
<?php
//Assume that $options contains stuff pulled from your database.
foreach($options as $option) {
echo "<option value='" . $option . "'>" . $option . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
When a user completes the form, and clicks submit, alloment_report.php (specified by action="alloment_report") gets the data sent from the form over $_POST (specified by method="post").
<!--alloment_report.php-->
<?php
if(isset($_POST['tourcode'])){
echo "yay! the tour has been selected!";
}
?>

Turning a mysql column into an array and using the array in a dropdown

Long time listener, first time caller. I'm having trouble with pulling a column called "Rep_IP" from a mysql table called "roster", turning it into an array, and then using that array to populate a dropdown in html. I've tried several suggestions listed here as well as other places and I'm not having any luck. The page shows up just fine but the dropdown has no options to select. I figured I would see if one of you could tell me what I am doing wrong here.
<html>
<body>
<form action="insert.php" method="post">
<p>Rep ID:</p>
<?php
$con = mysql_connect("localhost", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("rep_stats", $con);
$query = "SELECT Rep_ID FROM roster";
$result = mysql_query($query) or die ("no query");
$result_array = array();
echo "$query"
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
?>
<select name="Rep_ID">
<?php
foreach($result_array as $rep)
{
echo "<option value=" . $rep['Rep_ID'] . ">" . $rep['Rep_ID'] . "</option>";
}
?>
</select>
Issues Handled: <input type="number" name="IssuesHandled">
Hours Worked: <input type="number" step="any" name="HoursWorked">
<input type="submit">
</form>
</body>
</html>
As you can see, the drop down is part of a form that is used to create an entry in a new table as well. I don't know if that makes a difference but I figured I would point it out.
Try this.
<select name="Rep_ID">
<?php
while($row = mysql_fetch_assoc($result))
{
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
?>
</select>
Try this:
<?php
function select_list(){
$host = "host";
$user = "user";
$password = "password";
$database = "database";
$link = mysqli_connect($host, $user, $password, $database);
IF (!$link)
{
echo ('Could not connect');
}
ELSE {
$query = "SELECT Rep_ID FROM roster";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option value=" . $row['Rep_ID'] . ">" . $row['Rep_ID'] . "</option>";
}
}
mysqli_close($link);
}
$begin_form =
<<< EODBEGINFORM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Insert data </title></head>
<body>
<form action="insert.php" method="post" name="form1">
<p>Rep ID:</p>
<select name="reps" form="form1">
EODBEGINFORM;
$end_form =
<<< EODENDFORM
</select><br>
Issues Handled: <input type="text" size="12" name="IssuesHandled"><br>
Hours Worked: <input type="text" size="12" name="HoursWorked"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EODENDFORM;
echo $begin_form;
echo select_list();
echo $end_form;
?>
You will notice that I have used MYSQLI_ istead of MYSQL_ the reason is that this better for new code, see the comments above.
I debugged your code and ran I to a lot of problems.:-( The main problem was:
echo "$query" Your forgot the semicolon at the end of the line.
Good luck with you project.

advice on php post operation not running

I have donation page which when the user clicks donate it posts the data to a php file named test.php I am trying this out my first trying to echo the first name and last name but this is not working ultimately I want this php page to run a MySQL query to update the total_Donation row within a database, here is my main php page first.
Database code which sits at top of file
<?php
$con = mysql_connect("localhost","root","null");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("snr", $con);
$names_sql = "SELECT first_Name, last_Name FROM donate WHERE user_ID > 0";
$names_query = mysql_query($names_sql)or die(mysql_error());
$rsNames= mysql_fetch_assoc($names_query);
if(isset($_POST['donation']) && $_POST['donation'] != '')
{
$donation = mysql_real_escape_string($_GET['donation']);
$fname = mysql_real_escape_string($_GET['first_Name']);
$lname = mysql_real_escape_string($_GET['last_Name']);
$donate_sql = "UPDATE `donate` SET donate_Total = donate_Total + '{$donation}' WHERE first_Name = '{$fname}' AND last_Name = '{$lname}'";
}
mysql_close($con);
?>
Here is my form section of html
form method ="post" action="test.php">
<table>
<tr><td><label>Runner:</label></td>
<td>
<select>
<?php do{?>
<option> <?php echo $rsNames['first_Name'];?> <?php echo $rsNames['last_Name'];?></option>
<?php } while ( $rsNames= mysql_fetch_assoc($names_query))?>
</select>
</td>
</tr>
<tr><td><label>Donation £</label></td><td><input type="text" maxlength="9" value="0.00" name="donation"/></td></tr>
<tr><td><input id="submit" type="submit" value="DONATE"/></td></tr>
</table>
</form>
the option gets all the first names and last names fine when the user hits donate I want it to run the $donation_sql but all i get are errors saying unidentified index, I'm even trying the below in the test.php to simply just echo the first_Name this is giving the same error.
<?php
echo $_POST['first_Name'];
?>
Can someone please help me with this, thanks.
index.php
<?php
$con = mysql_connect("localhost","root","null");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("snr", $con);
$names_sql = "SELECT first_Name, last_Name FROM donate WHERE user_ID > 0";
$names_query = mysql_query($names_sql)or die(mysql_error());
?>
<form method ="post" action="test.php">
<table>
<tr><td><label>Runner:</label></td>
<td>
<select name="name">
<?php
while($list = mysql_fetch_array($names_query))
{
?>
<option value="<?php echo $list['first_Name'] . ' ' . $list['last_Name']; ?>">
<?php echo $list['first_Name'] . ' ' . $list['last_Name']; ?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr><td><label>Donation £</label></td><td><input type="text" maxlength="9" value="0.00" name="donation" /></td></tr>
<tr><td><input id="submit" type="submit" name="send" value="DONATE"/></td></tr>
</table>
</form>
test.php
<?php
$con = mysql_connect("localhost","root","null");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("snr", $con);
if(isset($_POST['donation']) && $_POST['donation'] != '')
{
$names = explode(' ',$_POST['name']);
$first_name= $names[0];
$last_name= $names[1];
$donation = mysql_real_escape_string($_POST['donation']);
$fname = mysql_real_escape_string($first_name);
$lname = mysql_real_escape_string($last_name);
$donate_sql = "UPDATE `donate` SET donate_Total = donate_Total + '" .$donation. "' WHERE first_Name = '" .$fname. "' AND last_Name = '" .$lname. "'";
echo 'DEBUG (remove after OK): <br>' .$donate_sql. '<br>';
$res = mysql_query($donate_sql);
echo 'Thanks ' .$first_name. ' ' .$last_name. '<br>';
}
mysql_close($con);
?>
That´s it!
\make sure you set name for select and you have valua attr in option tag
<select name="first_Name">
<otpion value="<?php echo $rsNames['first_Name'];?>"><?php echo $rsNames['first_Name'];?>
<?php echo $rsNames['last_Name'];?>
</option>
</select>
YOu need to give a name attribute to the select:
<select name="first_Name">
<?php while ( $rsNames= mysql_fetch_assoc($names_query)):?>
<option value="<?php echo htmlspecialchars($rsNames['first_Name']).' '.htmlspecialchars($rsNames['last_Name']);?>"> [option displayed to the user here]</option>
<?php endwhile;?>
</select>
And of course use the $_POST array, not the $_GET, since you're using the POST method.

Only one select list works on PHP form

Disclaimer: It's been a while since I last wrote any code. The quality of my code is likely to be sub-par. You've been warned.
I have a basic form that's meant to search flat files on our server. The "search engine" I created as two select lists: one for the file names and one for the customer site files come from.
For a reason I can't figure out, whatever option I select from the second select list is never captured when I hit Submit.
However, whatever option I select from the first select list is always captured.
What am I missing? I am sure it's starting right at me.... Any hints welcome. Thank you.
Here's my code:
<HTML>
<head><title>SEARCH TOOL - PROTOTYPE</title></head>
<body><h1>SEARCH TOOL - PROTOTYPE</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Filename (one item)</legend><select name="DBFilename" id="DBFilename">
<?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
$result = mysql_query("select distinct filename from search_test");
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></option> <?php } mysql_close($con); ?>
</select></fieldset>
<fieldset>
<legend>Site (one item)</legend><select name="DBSite" id="DBSite">
<?php $con = mysql_connect("localhost", "user", "pass"); if (!$con) { die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die(mysql_error());
$result = mysql_query("select distinct site from search_test");
while ($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['site']; ?>"><?php echo $row['site']; ?></option> <?php } mysql_close($con);
?>
</select></fieldset>
<input type="submit" name="submit" value="submit" >
<input type="button" value="Reset Form" onClick="this.form.reset();return false;" />
</form>
</body>
</HTML>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['DBFilename'])) {doFileSearch();}
elseif (!empty($_POST['DBSite'])) {doSite();}
}
function doFileSearch() {
$mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot";
$dir = opendir($mydir);
$DBFilename = $_POST['DBFilename'];
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die("Couldn't select the database.");
$getfilename = mysql_query("select filename from search_test where filename='" . $DBFilename . "'") or die(mysql_error());
echo "<table><tbody><tr><td>Results.</td></tr>";
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
echo '<tr><td>' . $filename . '</td></tr>';
}
echo "</table></body>";
}
function doSite() {
$mydir = $_SERVER['DOCUMENT_ROOT'] . "/filedepot";
$dir = opendir($mydir);
$DBSite = $_POST['DBSite'];
$con = mysql_connect("localhost", "user", "pass");
if (!$con) {die('Could not connect: ' . mysql_error());}
mysql_select_db("dev", $con) or die("Couldn't select the database.");
$getfilename = mysql_query("select distinct filename from search_test where site='" . $DBSite . "'") or die(mysql_error());
echo "<table><tbody><tr><td>Results.</td></tr>";
while ($row = mysql_fetch_array($getfilename)) {
$filename = $row['filename'];
echo '<tr><td>' . $filename . '</td></tr>';
}
echo "</table></body>";
}
?>
You have no part of your form with name='submit'
There for you will never get into the if statement that says: if (isset($_POST['submit'])) { because it will always be false
Complete re-re-write here => http://pastebin.com/raw.php?i=qi5F7X2e
There are several problems with my form.
a) Neither one of my SELECT lists are ever empty.
b) I never check for return the values from my functions.
This one is working.
Thanks to all for taking the time.

Categories