how to use a link to download a file in php - php

I am using the following code to fill the drop-down box and select a file and download it. Its working perfect.But i tried using every file to display as link and download it on clicking the link.
echo "<form id=\"form\" name=\"psform\" action=\"download_logic.php\" method=\"post\"><label>Select File:&nbsp</label><select name=\"file\" >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id=\"submit\" type=\"submit\" name=\"filesubmit\" value=\"Download\" /> </form>";
its giving me errors ....any help please....
I am using following code:
while ($row = mysql_fetch_array($result)) {
echo "" . $row['fileshare'] . "";
}
Here is the error in the <a> tag*** Error (unexpected T_ENCAPSED_AND_WHITESPACE)*

this should work:
echo '' . $row['fileshare'] . '';

I'm really sorry I can't tell you why it happens, but if you change all of the \" to ' it should work for you.
echo "<form id='form' name='psform' action='download_logic.php' method='post'><label>Select File:&nbsp</label><select name='file' >";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['location'] . "'>" . $row['location'] . "</option>";
}
echo "</select></label>";
echo"<br>";
echo "<input id='submit' type='submit' name='filesubmit' value='Download' /> </form>";
As for the next part, you can't add a variable to a string just by writing
"variable: . $take['variable']"
If you wish to add variables to string close the writen part of the string and then add the variable, like this:
"variable: ". $take['variable']
Here is how it sould look like:
while ($row = mysql_fetch_array($result)) {
echo "<a href='download_logic.php?f=". $row['location'] . $row['fileshare'] ."'>" . $row['fileshare'] . "</a>";
}

Related

I have 3 users with different id in the database and it keeps getting the last user id that been loop when I accept or reject?

There are 3 id that been view from this table
$sql = mysqli_query($conn, "SELECT * FROM user_appointment WHERE event = '' ");
while($row = mysqli_fetch_assoc($sql)){
$id = $row["id"];
$date = $row["date"];
$office = $row['office'];
echo "<table>";
echo "<tr>";
echo "<td colspan='2'> <strong>Name: </strong>" . $row['first_name'] . " " . $row['middle_name'] . " " . $row['last_name'] . "</td>";
echo "<td><strong>You're request is: </strong>" . $row['event'] . "</td>";
echo "</tr>";
echo "<tr><td colspan='3'> <strong>Address: </strong>" . $row['address'] . " </td></tr>";
echo "<tr><td colspan='3'> <strong>Office to go: </strong>" . $row['office'] . " </td></tr>";
echo "<tr>";
echo "<td> <strong>Contact#: </strong>" . $row['phone'] . "</td>";
echo "<td> <strong>Request made from: </strong>" . $row['curdate'] . "</td>";
echo "<td> <strong>Time request: </strong>" . $row['time'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><strong><i>Message: </i></strong><br>". $row['message'] . "</td>";
echo "</tr>";
echo "<tr> <td colspan='3'>";
echo "<center><form method='GET'>
<div class='center'>
<label for=''>Select Date:</label><br>
<input type='date' name='userDate' id='userDate' value='' required>
</div><br>
<button type='submit' name='approveSubmit' class='btn btn-success'>ACCEPT</button>
<button type='submit' name='rejectSubmit' class='btn btn-danger'>REJECT</button>";
echo "</form> </center>";
echo "</td></tr>";
echo "</table>";
echo 'Either I choose one of the users, it still getting the user id that been loop last';
if(isset($_GET['approveSubmit'])){
isset($_GET['userDate']);
$date = $_GET['userDate'];
header("location: ../approve_insert.php?id=$id&date=$date");
}
if(isset($_GET['rejectSubmit'])){
header("location: ../reject_insert.php?id=$id");
}
}
You are not passing the correct $id to your header: Location(...
To solve this you would need to pass the id of the user to the form as well, so this value become available when an user is clicked.
You can do this by adding an extra hidden input to the form you are creating
<input type='hidden' name='id' value='".$id."' />
Also there is no need to place the code that controls the action you want to do inside the loop that creates the table. Just place it above (or below) the code that generates the table
<?php
if(isset($_GET['approveSubmit'])){
$date = $_GET['userDate'];
header('location: ../approve_insert.php?id='.$_GET['id'].'&date='.$date);
exit;
}
if(isset($_GET['rejectSubmit'])){
header('location: ../reject_insert.php?id='.$_GET['id']);
exit;
}
$sql = mysqli_query($conn, "SELECT * FROM user_appointment WHERE event = '' ");
while($row = mysqli_fetch_assoc($sql)){
$id = $row["id"];
$date = $row["date"];
$office = $row['office'];
echo '... table start ...';
echo "<center><form method='GET'>
<div class='center'>
<label for=''>Select Date:</label><br>
<input type='date' name='userDate' id='userDate' value='' required>
</div><br>
<button type='submit' name='approveSubmit' class='btn btn-success'>ACCEPT</button>
<button type='submit' name='rejectSubmit' class='btn btn-danger'>REJECT</button>
<input type='hidden' name='id' value='".$id."' />
";
echo "</form> </center>";
echo '... table end ...';
}
Keep in my mind you would still need to sanitize the input of $_GET['id'] and $_GET['userDate'] before using it in your code/queries
My assumption at this point is that multiple users meet the conditions at the end of your loop. If your goal is to redirect to the location specified, from the first header location call, you'd have to prevent the loop from continuing. Typically this would be done with exit().
header("location: ../reject_insert.php?id=$id");
exit();
Also, you're going to get an error that you can't set headers because you've already output body content. The header("location...") can only be called before your echo ... statements.

On submit form Display data into table using echo in PHP

I have a table, displayied with an echo, where, for each row I'd like to have a button to change a "status" field (I want to update just one field). I am trying to insert a form button into my echo but I don't get anything.
This is my echo code:
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['fullname'] . '</td>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['country'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['website'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo "<td>" . $row['status'] . " </td>";
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
updatestatus.php
// check if the form has been submitted.
if (isset($_POST['submit'])) {
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id'])) {
// get form data, making sure it is valid
$id = $_POST['id'];
$status = $_POST['status'];
if ($status == 0) {
$status == 1;
mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
or die(mysql_error());
} else {
$status == 0;
mysql_query("UPDATE personalguestbook SET status='$status' WHERE id='$id'")
or die(mysql_error());
}
header("Location: view.php");
}
}
So, if my status field is = 0, I want to change it = 1 and update into my database, and vice versa.
Thanks for your attention
Your form isn't sending anything to updatestatus.php, you can add the data to POST in a hidden form like so:
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
This is the simple fix to your code above, there are still some security flaws and efficiency methods others have mentioned in the comments.
Check this PHP code
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['fullname'] . '</td>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['country'] . '</td>';
echo '<td>' . $row['email'] . '</td>';
echo '<td>' . $row['website'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo "<td>" . $row['status'] . " </td>";
echo "<td>";
echo "<form action='updatestatus.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='id' />";
echo "<input type='hidden' value='".$row['status']."' name='status' />";
echo "<td><input type='submit' name='submit' value='Submit'></td>";
echo "</form>";
echo "</td>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
updatestatus.php
// check if the form has been submitted.
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$status = $_POST['status'];
if ($status == 0){
$status == 1;
mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='".$id."'")
or die(mysql_error());
} else {
$status == 0;
mysql_query("UPDATE personalguestbook SET status='".$status."' WHERE id='$id'")
or die(mysql_error());
}
header("Location: view.php");
}
}

How to display an option from a selectbox?

I'm trying to create a table that shows the time and the task a user has to do.
All this is saved in a database. For each row and column there is a selectbox(Usernames) and an <input>(for comments).
Here is a screenshot: http://snag.gy/NYwsJ.jpg
The name of each selectbox is the id of the time and task
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
(e.g: name="1_23")
The problem is when I load the page it is supposed to display all the records that are already saved in the database.
For the <select> I tried using this an if:
if ($x["fiUser"] == $z["idUser"]){echo "selected";}
And here is the table:
<?php
echo"<div class='dv_Table'><table><tr><th></th>";
foreach (SelectTime() as $r) {
echo "<th>" . $r['dtTime'] . "</th>";
}
echo "</tr>";
echo "<form method='post'>";
$userselect = "";
foreach (SelectCalTask() as $x) {
echo "<tr><td>" . $x['dtTask'] . "</td>";
foreach (SelectTime() as $y) {
echo "<td>";
echo "<select name='" . $x['idTask'] . "-" . $y['idTime'] . "'>
<option value='0'> -- None -- </option> ";
foreach (SelectUser_Name() as $z) {
echo "<option value='" . $z["idUser"] . "'";
if ($x["fiUser"] == $z["idUser"]) {
echo "selected";
} echo" >" . $z["dtFirstName"] . " " . $z["dtLastName"] . "</option> ";
}
echo " </select>
<input type='text' name='" . $x['idTask'] . "_" . $y['idTime'] . "' value='" . "_" . $y['idTime'] . "'></td>";
}
echo "</tr>";
}
echo "</table></div>";
echo "<input type='submit' name='updateCalendar' value='Update'></form>";
?>
Better, would be if could send an Update query right after the "onchange" or right after finishing the comment in the input.
Is that possible ?
Thank you in advance
*
Better, would be if could send an Update query right after the
"onchange" or right after finishing the comment in the input. Is that
possible ?
*
Why not?
$('.yourValue').on('change', function (e) {
$.ajax({
type: "POST",
url: "/path-to-update-query",
success: function (data) {
}
});
});

PHP form update mysql database returns all database records after editing one record

so I have this code that other Stack members have helped me fine tune and correct some errors, the code all works as it should but there is one small detail, after successfully editing one record and clicking the update button ALL of the existing records that are in the database load into the page. Here is my code below:
<?php
$con = mysql_connect("localhost", "root", "M1q2w3e4r");
if (!$con) {
die("Can not connect: " . mysql_error());
}
mysql_select_db("inventory",$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
};
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
$sql = "SELECT * FROM invoice".$where;
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Inv #</th>
<th>From</th>
<th>To</th>
<th>Type</th>
<th>Notes</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action='edit.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='inv_number' value='" . $record['inv_number'] . "'> </td>";
echo "<td>" . "<input type='text' id='from' name='from_date' value='" . $record['from_date'] . "'> </td>";
echo "<td>" . "<input type='text' id='to' name='to_date' value='" . $record['to_date'] . "'> </td>";
echo "<td>" . "<input type='text' name='date_type' value='" . $record['date_type'] . "'> </td>";
echo "<td>" . "<input type='text' name='notes' value='" . $record['notes'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='id' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close($con);
?>
I know it has to do with the form action="edit.php", as it refreshes the page the id number in the url is pulled out. So I tried this:
echo "<form action='edit.php?edit=<?php echo $_REQUEST["id"]; ?>' method='post'>";
but this only led to my edit.php to display as a blank page. If anyone can help me figure out how to prevent all the database records from being displayed in the page after clicking the update button it would really help.
I might do this, for example, if I just wanted to show the record that was just updated:
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
$where = ' where id='.$_POST[id];
}
else {
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
}
You could also use REQUEST instead of GET and make a hidden input field with the name "edit" in your form.

Looping through $_POST Variable

I have a PHP script that generates some form on the page I have. I also have a table and submit button within the form which all works fine and the data is sent to the next page as intended. Now on the next page I have a PHP code that reads the $_POST variable. Here is my code:
echo "<form name=\"myForm\" id=\"myForm\" method=\"post\" action=\"\">";
echo "<table>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Group</th>";
echo "</tr>";
while($row = mysql_fetch_array($qryRes))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td><input name=\"aName" . $row['id'] . "\" id=\"aName" . $row['id'] . "\" type=\"text\" value=\"" . $row['aName'] . "\" /></td>";
echo "<td><select name=\"gID" . $row['id'] . "\">";
while($gRow = mysql_fetch_array($grpsQry))
{
if($gRow['id'] == $row['groupID'])
echo "<option id=\"" . $gRow['id'] . "\" selected=\"selected\">" . $gRow['gName'] . "</option>";
else
echo "<option id=\"" . $gRow['id'] . "\">" . $gRow['gName'] . "</option>";
}
echo "</select></td>";
echo "</tr>";
mysql_data_seek( $grpsQry, 0 );
}
echo "</table>";
echo "<input type=\"submit\" name=\"submitForm\" value=\"Save\" />";
echo "</form>";
Then comes my PHP Code to read the $_POST:
if(isset($_POST['submitForm']))
{
print_r($_POST);
foreach($_POST as $x)
echo $x . "<br />";
}
Everything works fine, except that I need to read only the text field in the foreach loop as I will generate a query on them and will compare the values I will have with the value returned from the List ..
As yuo can see, the textfield is named aName then the id of the user. I need to read this only. But I will use it to generate a query which will be then compared to the value of the List gID.
try this:
foreach($_POST as $key => $value){
if(substr($key,0,5) == 'aName')
echo $value . "<br />";
}

Categories