Printing php page with various values - php

I have a php page that gets values from my database called print.php which I use to display and print the values from my database.
print.php?id=100 loads the page with values from the database that has the id 100. each record on my database has a field to store the date the record was created. As for now I visit print.php?id=100, print.php?id=101, print.php?id=102, print.php?id=103 and so on to print the same page but with various values.
Is there a way for me to create a button which does this automatically?
This button should grab all records that have the current date and print all those pages.
Sorry for being unclear.
My print.php page echos values from my database like this:
Id: <?php echo $info['id']; ?>
Name: <?php echo $info['name']; ?>
Date: <?php echo $info['date']; ?>
Gender: <?php echo $info['gender']; ?>
So for example visiting print.php?id=100 loads these values:
Id: 100
Name: John
Date: 2013-04-09
Gender: Male
Cisiting print.php?id=101 loads these values:
Id: 101
Name: Sarah
Date: 2013-04-09
Gender: Female
and so on.
When visiting each page I hit CTRL+P to print that information.
Instead of doing this manually (visiting each page and sending it to printer) I would like to have a script which grabs all the records of current date and sends it to my printer.

I'm not sure what you want to archieve, but I think you want a form like this:
<form method="get" action="">
<input type="text" name="id" value="100"/>
<input type="submit"/>
</form>
this would redirect the user to print.php?id=100 or with the value given in the textbox
EDIT:
What I read from the comments on the question is this what you want to archieve:
<form method="get" action="">
<input type="text" name="date" value="<?=date('yyyy-mm-dd');?>"/>
<input type="submit"/>
</form>
this would show a form with a textfield with the date of today printed in.
then you should make a query and fetch it like this:
$date = check_date($_GET['date']);
$query = 'SELECT * FROM values WHERE date="'.$date.'"';
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
// print row
}
}
the function check_date() is up to you to create, this checks if the text is a valid date to make sure nobody can hack your database

As I understand (it's not very clear) you want records for a specified date. Then you have to send a parameter for that date but not a single record id.
So, you can send a date with print.php?date=09-04-2013 for example and in you PHP script your query would be:
$date = $_GET['date']; // validate it
$sql = "SELECT * FROM table WHERE date = '$date'";
// fetch result
// print with a foreach loop as you like
Or if you want the current date only you don't need to pass a parameter at all. Just use CURDATE() if your date field contains only the date or NOW() if it contains date and time:
$sql = "SELECT * FROM table WHERE date = CURDATE()";
// fetch result
// print with a foreach loop as you like
More: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Still couldn't understand whether you have a problem with the HTML or with the PHP part.
If you are asking just for html forms and how to create a button you can check here:
http://www.w3schools.com/php/php_forms.asp
It's pretty simple to understand.

Related

Writing single page logging tool in php

Im making a small php webpage which I plan to use to track on which subjects a helpdesk receives calls. My database has 3 important fields: id, name, and amount for each subject.
On my page I have a form with a dropdown list where you select a type of call and click submit. The idea is that every time you click submit the page reloads and the amount in the database for the chosen id is heightened by 1.
The form gives me the id and name for each call:
<form method="post" action="index.php">
<select class="select" id="calltype" name="calltype">
<?php
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value=".$row["ID"].">".$row["NAAM"]."</option>".PHP_EOL;
}
}
?>
</select></br>
<input class="input" type="submit" name="Submit" value="Submit">
</form>
This part works, if I echo $_POST['calltype'] I get the correct ID. What I can't get to work is the update statement which I want to heighten the counter, like:
if(isset($_POST['calltype']{
mysqli_query("UPDATE calls SET amount=(amount+1), WHERE id = $_POST['calltype']");
}
How would I go about this? I tried several methods but can't get it to work
besides for the extra comma, interpolation with the POST array like this is risky. maybe try:
mysqli_query("UPDATE calls SET amount=(amount+1) WHERE id = " . mysqli_real_escape_string($link, $_POST['calltype']) . " ;");

Select date() HTML5 and match with SQL date

I am sooo stuck on this, cannot figure it out. I am trying to have the user select the date using the date selector in HTML5 and then query the sql database to find the date and output the results in a table below. I have been working on this for literally hours, cannot get past it, please help.
HTML
Date for retrieve: <input type="date" name="pickupDate" value="<?php echo $pickupDate;?>">
<span class="error">
</p>
<p>
Show date item for retrieve:
<input type="radio" name="input" value="requestDate1">Request Date
<input type="radio" name="input" value="pickupDate1">Pickup Date
</p>
<p><input type="submit" value="Show"></p>
$requestDate = mysqli_real_escape_string($db, isset($_POST["pickupDate"]));
PHP
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["input"] == "requestDate1") {
$query = "SELECT requestDate FROM request WHERE requestDate = '$requestDate'";
echo "working";
I think your problem is with this selection query:
$query = "SELECT requestDate FROM request WHERE requestDate = '$requestDate'";
Please change it in this way:
$query = "SELECT requestDate FROM request WHERE requestDate = '" . $requestDate . "';";
Or if it's not working then check the date format both on frontend and backend, and if it's needed change the date format on backend to match it with frontend.
This is simple:
SQL uses the YMD HIS format to store date in to it's field, and HTML5 uses DDMMYYYY so if you want to use this date format, your required to change the format, I suggest you go through this answer in stackoverflow for more details...
HTML 5 Date format

Having variable values shown in a textbox in php using sql

I am trying to have a variable from one .php document to display in a textbox in another. In the process, the value of the variable is going back to a database to grab information relating to the variable.
To be more specific, the user selects a StudentID from a dynamically populated drop down box on the first form, then is taken to another form where the selected StudentID is shown in the first textbox, and other data (such as email address, phone no.) are shown in different textboxes underneath. This then allows the user to modify the data shown and save it back to the database.
The reason I am running into problems is the variable value is originally coming from a dynamically populated drop down box. From what I have tried, I do not think the variable is being passed to the second form. This is because I have tried different variations of code to get the data to show in the textboxes and nothing shows each time.
Here is the code from the drop down box:
$query="SELECT StudentID FROM Personal_Details";
$result = mysql_query ($query);
echo '<select name= "StudentID">';
//Printing the list box select command
while($nt=mysql_fetch_array($result)){ /*Array or records stored in $nt*/
echo "<option value=$nt[StudentID]>$nt[StudentID]</option>";
/*Option values are added by looping through the array*/
}
echo "</select>"; //Closing of lost box
?>
Being new to this, I am assuming the variable is "StudentID".
If this is the case, this is the code used on the second .php document to get the value of the varaible to display in a textbox, along with the other data:
$query=mysql_query("SELECT Email, Phone FROM Personal_Details WHERE StudentID = '$StudentID'") or die(mysql_error());
while ($row = mysql_fetch_row($query)) {
$email = $row[1];
$phone = $row[2];
}
?>
(There are some html code here to set up the form. I skipped over that and went straight to the code relating to the problem)
<form name="modstudent" method="post" action="modstudent2.php">
Student ID: <input name="StudentID" type="text" value="<?php printf("%s",$StudentID);?>"><br>
Email: <input name="email" type="text" value="<?php printf("%s",$email);?>"><br>
Phone: <input name="phone" type="text" value="<?php printf("%s",$phone);?>"><br>
Any help will be greatly appreciated. Thanks in advance.
UPDATE
The problem seems to be the variable from the first page not being passed over to the second page. To make sure it was a variable problem, a fixed value was substituted into the code below:
$query=mysql_query("SELECT Email, Phone FROM Personal_Details WHERE StudentID = '/StudentID-here/'") or die(mysql_error());
What needs to change so the variable can be passed over? Thanks in advance.
echo "<option value=".$nt[StudentID].">".$nt[StudentID]."</option>"; or use <option value=echo($nt[StudentID])>echo($nt[StudentID])</option>;

Exporting html (mysql populated) table to excel file

I am trying to query a mysql database and display data in a table. That part is working. At the moment, it is set up for displaying the
results within the certain date range.
I now want to take the table and make a button that allows you to export it to an Excel file. Before I added the option of choosing a date range, you were able to export to Excel, but now it seems that the second file does not know what table I am talking about. I tried using POST to send the values of the data and re-query on the other page.
When I click the button to export, the excel document that is downloaded is empty, (though it has a size). Any help please?
-----Query mysql---------
<html><head><title>New Production Rejections</title></head></html>
<?php
include("config.php");
//get serial from submitted data
//$serial = $_POST['sNumber'];
//if the submitted data is empty
$serial = $_POST['entryDate'];
$dateEnd = $_POST['entryDate2'];
//parse the serial from the link in tracker
?>
<form method="post" action="<?php echo "queryNewProdRejections.php?"?>">
Search between dates: (Format: YYYY-MM-DD)<input type='text' size='20' maxlength='20' name='entryDate'> - <input type='text' size='20' maxlength='20' name='entryDate2'>
<input type="submit" value="Search Date Range"><br/></form>
<?php
//query based on approved date that is nothing, repaired date that is nothing,
//tech is a real tech, location that is not Revite (RVP), action was to replace,
//and the status is not (declined or skipped).
$query = "SELECT *
FROM `rma`
WHERE `origin` NOT LIKE 'Field_failure'
AND `origin` NOT LIKE 'DOA_at_Customer'
AND `origin` NOT LIKE 'Sweden_Fail_VI'
AND `entry` > '$serial' AND `entry` < '$dateEnd'";
$data = mysql_query($query) or die(mysql_error());
//Create a table with the array of data from repairs, based on the previous query
echo "<table border='1'><tr><th>RMA</th><th>Product</th><th>Serial</th><th>Origin</th><th>Return To</th><th>Credit Num</th><th>Order</th><th>Entry Date</th><th>Tech</th><th>Traking Num</th></tr>";
while($row = mysql_fetch_array($data)){
print "<tr><td>".$row['intrma']."</td><td>".$row['product']."</td><td>".$row['serial']."</td><td>".$row['origin']."</td><td>".$row['retto']."</td><td>".$row['creditnum']."</td><td>".$row['ordernum']."</td><td>".$row['entry']."</td><td>".$row['tech']."</td><td>".$row['tracknum']."</td></tr>";
}
print "</table>";
?>
<html>
<form method="post" action="saveQueryToExcel.php">
<input type='hidden' name='ent_1' value="<?php echo $_POST['entryDate']; ?>">
<input type='hidden' name='ent_2' value="<?php echo $_POST['entryDate2']; ?>">
<input type="submit" value="Save to Excel">
</form>
</html>
---------------Print to Excel File -- (saveQueryToExcel.php)
<html><head><title>New Production Rejections</title></head></html>
<?php
error_reporting(0);
$dateBeg=$_POST['ent_1'];
$dateEnd=$_POST['ent_2'];
//Connect to the database, repairs in maprdweb
include("config.php");
//query based on approved date that is nothing, repaired date that is nothing,
//tech is a real tech, location that is not Revite (RVP), action was to replace,
//and the status is not (declined or skipped).
$query = "SELECT *
FROM `rma`
WHERE `origin` NOT LIKE 'Field_failure'
AND `origin` NOT LIKE 'DOA_at_Customer'
AND `origin` NOT LIKE 'Sweden_Fail_VI'
AND `entry` > '$dateBeg' AND `entry` < '$dateEnd'";
$data = mysql_query($query) or die(mysql_error());
//Create a table with the array of data from repairs, based on the previous query
header('Content-type: application/vnd.ms-excel');
echo "<table border='1'><tr><th>RMA</th><th>Product</th><th>Serial</th><th>Origin</th><th>Return To</th><th>Credit Num</th><th>Order</th><th>Entry Date</th><th>Tech</th><th>Traking Num</th></tr>";
while($row = mysql_fetch_array($data)){
print "<tr><td>".$row['intrma']."</td><td>".$row['product']."</td><td>".$row['serial']."</td><td>".$row['origin']."</td><td>".$row['retto']."</td><td>".$row['creditnum']."</td><td>".$row['ordernum']."</td><td>".$row['entry']."</td><td>".$row['tech']."</td><td>".$row['tracknum']."</td></tr>";
}
print "</table>";
?>
PHPexcel works great for exporting data to an actual Excel document.
It appears you are just generating an HTML table with your result.. which isn't exactly Excel format.
You should take the same code, and instead of printing <tr> lines with it, send it to fputcsv. Use the standard output as the file handle. You need to set proper html headers for the output to be sent as a csv, along the lines of this post: force file download.
Take a look a this class. It is able to solve the problem.
https://github.com/GSTVAC/HtmlExcel
$xls = new HtmlExcel();
$xls->addSheet("Names", $names);
$xls->headers();
echo $xls->buildFile();

Generate a query that changes a single element on multiple rows PHP MySQL

I'm working on a homework assignment that involves PHP. Basically, I have a page that contains a web form that renders items pulled from a database. There's checkboxes on each row and 2 radio buttons. The user selects "accept" or "deny" and when submit is clicked, the items that are checked are supposed to change to that approval status. All of the items in the form are submitted into post. I thought that post is an array so I could just use a while loop with a counter so that the loop traverses through the array and when it gets to the last index (which should contain approve or deny). A query is generated that changes all of the previous indexes to approve or deny. I'm sorry if this isn't making much sense.
Here's a picture for more clarification
Here's the code I used to generate the webform:
<?php
#create a query string
$query = "SELECT * FROM Request WHERE superemail = '$user'";
#echo $query;
#run the query
$result = mysqli_query($link, $query) or die('error querying');
while($row = mysqli_fetch_array($result)){
#print out each row of the queryi
#line up the query results with temporary strings
$change = $row['KEY'];
$name = $row['first']. " " . $row['last'];
#echo $name;
$email = $row['email'];
#echo $email;
$type = $row['type'];
#echo $type;
$duration = $row['duration'];
$status = $row['status'];
#create a table row with the query results
echo "<tr><td><input type=checkbox name=$change /></td>
<td>$name</td>
<td>$email</td><td>$type</td>
<td>$duration</td><td>$status</td></tr>";
} #end while
?>
<label for=update>Change status to:</label><br />
<input type=radio name=update value=A />Approved<br />
<input type=radio name=update value=D />Denied<br />
<input type = submit value = "Change Status" />
Each input tag (your checkboxes and your radio button) in your html should have a name attribute, such as: <input type='radio' name='accept' value='1' />. When the form is submitted and processed by PHP, your script will be able to access that info at $_POST['accept'] (or $_GET['accept'] depending on the form's method).
So you should be able to specify a name for the radio button, then check to see if there is a value in the POST array at that index.
I am going to assume a few things. First, that it is a design goal of yours to only process items in your approval queue that you choose to select, leaving the others alone. Second, that you want to change their status to what is chosen in a radio button. Third, that you want both a code snippet and an explanation as to how the task got accomplished.
So, here goes.
You have a series of checkboxes; I assume they have the same name. If you wish for the values to be passed to PHP as an array, you absolutely need to name the inputs whatever[] with emphatic emphasis on the []! This is what creates an array from the checkbox to appear in the $_POST/$_GET. Only those items selected will appear in the array. The value ought to be something useful (A value in the corresponding database table, say) which you can select for and query on...after sanitizing the input, of course.
So, your HTML input tag should look like this:
<input type="checkbox" name="process[]" value="<?php echo $employeeName ?>" >
You should have this coming back at you...
$_POST['process'][array(0=>name1, 1=>name2/*...etc*/)]
...which you can loop through with a foreach at your leisure.

Categories