I have a separate file that generates a PDF, saves select information to a database, creates the PDF and then emails it to the user. What I am trying to do is echo the sql statement on the confirmation page.
request.php -> PDF-generation.php -> confirmation.php
The user never sees the PDF generation page.
Here is the function:
function LogToDatabase($holding_id, $to_email, $from_email, $subject, $message, $pdf) {
//Save record of PDF here
$db = new wpdb(DB_USER, DB_PASSWORD, 'webtransaction', DB_HOST);
$sql = "INSERT INTO 504_pdf_storage (`holding_id`, `to_email`, `from_email`, `subject`, `message`, `pdf`)
VALUES(" . $holding_id . "," . $to_email ."," . $from_email ."," . $subject ."," . $message ."," . $pdf . ");";
return $db->insert_id;
}
I want to echo the variable $sql from PDF-generation.php onto the confirmation.php but a simple "echo $sql;" gives an error saying "Undefined variable"
Please help and thanks!
EDIT: Here is the top of the confirmation page.
require_once('../../../../wp-load.php');
require_once('../includes/phpmailer/class.phpmailer.php');
require_once('../classes/504-send-request.php');
require_once ('../classes/pdf_generator.php');
$mh = new MobileHomeRequest();
$data = $mh->GetTempData($_POST['id']);
$holding_id = $data->SaveToDatabase();
$pdf = new PDFGen(null,$data);
echo $sql;
get_header();
if(!$pdf->RenderPDF()) { ?>
<script>
alert('Couldn\'t send your email because our servers seem to be really lame today.');
</script>
<?php } ?>
Save the value in a session variable, and then echo out the session variable on the confirmation page. You'll need session_start() at the top of each page.
function LogToDatabase($holding_id, $to_email, $from_email, $subject, $message, $pdf) {
//Save record of PDF here
$db = new wpdb(DB_USER, DB_PASSWORD, 'webtransaction', DB_HOST);
$sql = "INSERT INTO 504_pdf_storage (`holding_id`, `to_email`, `from_email`, `subject`, `message`, `pdf`)
VALUES(" . $holding_id . "," . $to_email ."," . $from_email ."," . $subject ."," . $message ."," . $pdf . ");";
//add this
$_SESSION['savedSQL'] = $sql;
return $db->insert_id;
}
Then on the confirmation page, it's a simple:
echo htmlentities($_SESSION['savedSQL'], ENT_QUOTES);
Side Note
As mentioned in the comments, a var_dump($sql) followed by exit() would be much better. Also note the htmlentities() to protect against XSS, since it is unknown where the variables in the query are coming from.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
So I have a program that the user inputs text into a text box, and then that text is created into a file. Each time the submit button is pressed, the database creates a job and is given a jobID. I was wondering if there was anyway to make the name of the file the job_id.fasta instead of just calling it new.fasta. This way I am able to store more files! Thanks in advance. I will post the code of creating the file and storing it.
<form method="POST", action="/~cs4380sp15grp4/home/blast.php" class="form-inline">
<textarea id="BlastSearch" type="textarea" name="BlastSearch" class="form-control" placeholder="Protein sequence in fasta format"></textarea>
<button id="run" type="submit" name="submit" class="btn btn-primary"><span class="glyphicon glyphicon-wrench"></span> Run BLAST</button>
</form>
<?php
session_start();
require_once '../secure/database.php';
$mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($mysqli->connect_error){
exit('CON Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error);
}
//Insert the values into the database
if(isset($_POST['submit'])){
$sequence = $_POST['BlastSearch'];
//create a new .fasta file and put the sequence the user wants to search for in that file
$file = 'uploads/new.fasta';
$current = $sequence;
file_put_contents($file, $current);
?>
You can simply insert record in database before new file creation and get job_id using $mysqli->insert_id to make the name of the file job_id.fasta
<?php
session_start();
require_once '../secure/database.php';
$mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($mysqli->connect_error){
exit('CON Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error);
}
//Insert the values into the database
if(isset($_POST['submit'])){
$sequence = $_POST['BlastSearch'];
$query = "INSERT INTO `jobs` (`BlastSearch`) VALUES ('".$sequence."')";
$mysqli->query($query);
$job_id = $mysqli->insert_id;
//create a new .fasta file and put the sequence the user wants to search for in that file
$file = "uploads/".$job_id.".fasta";
$current = $sequence;
file_put_contents($file, $current);
}
?>
Assuming job_id is the automatically inserted ID, you could use last_id http://php.net/manual/en/mysqli.insert-id.php:
//Insert the values into the database
// Fictitious MySQL
$sql = "INSERT INTO search (keyword) VALUES ('mysearch')";
$last_id = "";
if ($mysqli->query($sql) === TRUE) {
$last_id = $mysqli->insert_id;
} else {
echo "Error: " . $sql . "<br>" . $mysqli->error;
die();
}
// End fictitious MySQL
if(isset($_POST['submit'])){
$sequence = $_POST['BlastSearch'];
//create a new .fasta file and put the sequence the user wants to search for in that file
$file = 'uploads/'.$last_id.'.fasta'; // < Use insert_id here.
$current = $sequence;
file_put_contents($file, $current);
Here is my problem,
I have a PHPmailer object that works fine in the file "enviar1.php",but when I serialize this object,store it in a session variable and try to unserialize and use it in another file,it doesnt work.the issue is that the email is sent,but with no subject,no body and no attachments.
File:"enviar1.php"
[...]
$email = new PHPMailer();
$email->CharSet = 'UTF-8';
$emaildes = "";
$campusEsc = addslashes($campus);
$query = "SELECT email FROM bibliotecarios WHERE campus = '$campusEsc'";
$emailBibliotecario = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($emailBibliotecario))
$emaildes = $row['email'];
$email->isSMTP();
$email->Host ="ssl://smtp.googlemail.com";
$email->SMTPAuth = true;
$email->Port = 465;
$email->Username = "someemail";
$email->Password = "somepass";
$email->From = "someemail";
$email->FromName = "Sistema de Encaminhamento de Ficha";
$email->AddAddress($emaildes, "bibliotecario do campus " . $campus);
$email->IsHTML(true);
$email->Subject = "ficha catalografica para revisao";
$mensagem = "<p>ficha do(a) aluno(a):" . $nome . " " . $sobrenome . "(" .$emailAluno . ") do campus de " . $campus . "</p>" ;
$mensagem .= "</br>";
$mensagem .= "</br>";
$email->Body = $mensagem;
$email->AddStringAttachment($string, 'ficha Catalografica - ' . $nome . " " . $sobrenome . '.odt');
$email->AddStringAttachment($string2, 'ficha Catalografica - ' . $nome . " " . $sobrenome . '.docx');
$email->AddAttachment($_FILES["arquivo"]["tmp_name"],$_FILES["arquivo"]["name"] );
session_start();
$_SESSION['nome'] = $nome; //I need these variables to create TBS a object in "preview.php"
$_SESSION['sobrenome'] = $sobrenome;
$_SESSION['email'] = $emailAluno;
$_SESSION['titulo'] = $titulo;
$_SESSION['subtitulo'] = $subtitulo;
$_SESSION['ano'] = $ano;
$_SESSION['folhas'] = $folhas;
$_SESSION['ilus'] = $ilus;
$_SESSION['tipo'] = $tipo;
$_SESSION['curso'] = $curso;
$_SESSION['campus'] = $campus;
$_SESSION['orientacao'] = $orientacao;
$_SESSION['coorientacao'] = $coorientacao;
$_SESSION['assunto1'] = $assunto1;
$_SESSION['assunto2'] = $assunto2;
$_SESSION['assunto3'] = $assunto3;
$_SESSION['assunto4'] = $assunto4;
$_SESSION['assunto5'] = $assunto5;
if($autorizacao != "true") //this is a checkbox from the previus form
{
if ($email->Send()) //here the email works like a charm
header("Location: preview.php");
else
echo "Erro ao enviar o email";
}
else
{
$_SESSION['emailParcial'] = serialize($email); //I stored in a session variable to edit the object later
header("Location: termo.php");//just another page with a form
}
[...]
the problem is in this file "enviar2.php",the email is sent to the correct destiny but empty,no subject,no body and none attachments.
file:"Enviar2.php"
<?php
ini_set("display_errors", true);
error_reporting(-1);
require 'configuracoes.php';
include_once('tbs_class.php');
include_once('tbs_plugin_opentbs.php');
function __autoload( $className ) {
require_once('./phpmailer/class.phpmailer.php');
}
[...]
$emailcompleto = new PHPMailer();
session_start();
$emailcompleto = unserialize($_SESSION['emailParcial']);
echo $emailcompleto->Body; //I put this to test the value and its ok
echo $emailcompleto->Subject; //the value is ok,the same as the $email object from "enviar1.php" file
if ($emailcompleto->Send()) //the email is sent but i get a empty email.
header("Location: preview.php");
else
echo "Erro ao enviar o email" . $email->ErrorInfo;
[...]
I dont know why it is not working,I used a echo and printed the Properties of the $emailcompleto object,they have correct values,it has the same subject and body of the $email object from the "enviar1.php" file has,but in the end i get a empty email.
someone have a clue of what is happening?
doc of class PHPmailer: http://www.tig12.net/downloads/apidocs/wp/wp-includes/PHPMailer.class.html#det_methods_AddAddress
It's easy....
PHPMailer can send a pre-created email if it's separated into Header and Body. PHPMailer can also create a Header and Body separate of each other:
$header = $mailer->createHeader();
$body = $mailer->createBody();
Once you have that data - you can store it in any manner you choose. Calling it up at a later time to send using any of the 3 PHPMailer Send() methods:
// SMTP
$mailer->smtpSend($header,$body);
// Sendmail
$mailer->sendmailSend($header,$body);
// PHP Mail
$mailer->mailSend($header,$body);
Basically, this code works but it displays both the sender and receiver in the same color
what do i want? i want the sender colour to be different with the receiver message color
<?php
require_once 'cn.php';
session_start(); // startsession .line17 (username || usernameto='.$username.')
//messages sent or received by the $_SESSION or $_GET["ident"]..line 26
require_once 'protect.php';
$username=$_SESSION['UserID'];
$fiveMinutesAgo = time() - 1000;
$to=$_SESSION['UserTo'];
$sql = 'SELECT
username, message_content,message_file, message_time,usernameto
FROM
messages
WHERE
message_time > ' . $fiveMinutesAgo . '
and ((username='.$username.'
and usernameto='.$to.') or (usernameto='.$username.'
and username='.$to.'))
ORDER BY
message_time';
$result = mysql_query($sql, $cn) or
die(mysql_error($cn));
//mysql_query ("Update messages set message_file= replace(message_file,' ','');
while ($row = mysql_fetch_assoc($result)) {
$hoursAndMinutes = date('g:ia', $row['message_time']);
$mycontent=$row['message_content'];
$myfile=$row['message_file'];
$playfile="<A href=upload/$myfile> $myfile </A>";
/*************************************************************************************************/
echo '<i><p style="font-family:arial;color:purple;font-size:15px;"><strong>' . $row['username']. '</strong>: <em>(' . $hoursAndMinutes . ')</em> ' . $playfile . $mycontent.'</i></p>';
}
?>
Assuming that $username and your column username contains a userID: Check if $username is the same as $row['username']. If it is, your message is from the sender.
if ($row['username'] == $username)
$color = 'red';
else
$color = 'purple';
echo '<i><p style="font-family:arial;color:'.$color.';font-size:15px;"><strong>' . $row['username']. '</strong>: <em>(' . $hoursAndMinutes . ')</em> ' . $playfile . $mycontent.'</i></p>';
Also: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
http://www.php.net/manual/en/function.mysql-query.php
you need to know when the sendername is the same people who has the session open if the sendername else is the receiver
I am doing the following in my PHP
public $message = "<p>A new user has requested to signup for Company Name</p>
<p><strong>Name:</strong>". $name ."</p>
<p><strong>Email Address:</strong>" . $email . "</p>
<p><strong>Contact Number:</strong>" . $contact_number . "</p>
<p>Please check the attached file, for further details.</p>
<p>Thanks,<br/>Company Name Email Centre</p>";
As you can see I am tring to create a small segment of HTML interspersed with some PHP variables, however I am getting the following error,
Parse error: syntax error, unexpected '.', expecting ',' or ';'
It says this on line 31 which is the second <p> tag, however I cannot see a problem
You should not use var/string concatenation in a class var definition. Why don't you write a method such as getMessage() or printMessage() or something else ?
public $message= "<p>A new user has requested to signup for Company Name</p>\
<p><strong>Name:</strong>". $name ."</p>\
<p><strong>Email Address:</strong>" . $email . "</p>\
<p><strong>Contact Number:</strong>" . $contact_number . "</p>\
<p>Please check the attached file, for further details.</p>\
<p>Thanks,<br/>Company Name Email Centre</p>";
/* Try this it will work ... ( u need to escape lines using \ ) */
PHP terminates a string at newline. You need to concatenate everytime a newline begins:
public $message = "<p>A new user has requested to signup for Company Name</p>"
."<p><strong>Name:</strong>". $name ."</p>"
."<p><strong>Email Address:</strong>" . $email . "</p>"
."<p><strong>Contact Number:</strong>" . $contact_number . "</p>"
."<p>Please check the attached file, for further details.</p>"
."<p>Thanks,<br/>Company Name Email Centre</p>";
Try it this way. and have a look at http://php.net/manual/de/language.types.string.php
public $message = "<p>A new user has requested to signup for Company Name</p>" .
"<p><strong>Name:</strong>". $name ."</p>" .
"<p><strong>Email Address:</strong>" . $email . "</p>" .
"<p><strong>Contact Number:</strong>" . $contact_number . "</p>" .
"<p>Please check the attached file, for further details.</p>" .
"<p>Thanks,<br/>Company Name Email Centre</p>";
You can't do that in a class attribute definition, instead define the attribute then give it a value in the class constructor:
class Test {
public $message;
public $name = 'Demo Name';
public function __construct() {
$this->message = 'anything you want' . $this->name; # etc
}
}
In the actual definition you can only give litterals, no variable evaluations.
Cheers
I am trying to create a web page using php. The problem is that sometimes I get server errors and sometime I get nothing in return at all. At one point the server changed the file type itself. I have NO idea what the problem can be.
And since I have actually no idea what the problem is I paste the entire file here, even though I expect that it's the first few rows that is the problem (I put it here: http://www.iandapp.com/smic/subscription.php) :
<?php header('Content-Type: text/html; charset=ISO-8859-1');
echo("<div id='subscribe'>");
$mail = $_POST['email1'];
//Set the locale format, etc. of date and time
date_default_timezone_set('Europe/Stockholm');
setlocale(LC_TIME, "sv_SV");
//Create the db-connection
$mysqli = mysqli_connect("mydb", "myuser", "mupsw", "myschema", "3306");
//If verifying the subscription, makse sure the db is updated accordingly
if($_GET['newid'] != ""){
//Make the subscriber a verified subscriber
$result = mysqli_query($mysqli,"UPDATE users SET subscriber = 1 WHERE id = " . $_GET['newid']);
if($result){
echo("<p>Welcome to our newsletter! We will send you information about any new application or update. This will not happen too often, but once it does we hope you will have the opportunity to look into our site again.<p/>");
echo("<p><br/>If you wish to unsubscribe from this mail-list, please visit out subscription page: http://" . $_SERVER['HTTP_HOST'] . "/subscription.php <p/>");
}else{
echo("<p>Somthing went wrong, please click the link again!<p/>");
}
}elseif($_POST['email1'] != ""){ //Only do things if there is an e-mail posted
//Make sure the ID is unique
do{
$rand_int = rand(100000000, 999999999);
$result = mysqli_query($mysqli, "SELECT id FROM users WHERE id = " . $rand_int);
$no_of_rows = mysqli_num_rows($result);
}while($no_of_rows != 0);
echo("RAND :".$rand_int);
//Create query for saving the new user e-mail
$query = "INSERT INTO users(id, email, first_name, surname) VALUES ('" . $rand_int . "', '".$_POST['email1']."', '". $_POST['first_name']."','".$_POST['surname']."')";
$re = mysqli_query($mysqli, $query);
echo("Result: ".$re);
//Check if mail already exist (Error code 1062 = dublicate entries for unique fields)
$existing = 0;
if(mysqli_errno($mysqli)==1062){
echo("It seems like your e-mail already is registered. Perhaps you have been a subscriber earlier? By clicking the link that you will receive in your mail box you subscribe to the newsletter again. ");
$existing = 1;
$result = mysqli_query($mysqli,"SELECT id FROM users WHERE email = '" . $_POST['email1'] . "'");
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$rand_int = $row['id'];
mysqli_query($mysqli,"UPDATE users SET waiting_for_unsubscribe = 0 WHERE email = '" . $_POST['email1'] . "'");
if(mysqli_errno($mysqli)){
echo("Error code " . mysqli_errno($mysqli) . "<br/>");
echo("Error text " . mysqli_errno($mysqli) . "<br/>");
}
//echo("ID------ " . $row['id'] . " ri---- " . $rand_int);
}
//Create the link for the user to verify the subscription
$url = "http://" . $_SERVER['HTTP_HOST'] . "/smic/subscription.php?newid=" . $rand_int;
$mymail = $_POST['email1'];
$esubject = "Please verify your subscription to iAndApp's newsletter ";
$body = "Click the link in order to verify your subscription (If you cannot click the link, just copy it and paste it into the adress field of your browser): " . $url;
$eemail = "NoReply#iandapp.com";
$sent = mail($mymail,$esubject,$body,"From: $eemailn");
if($sent){
if($existing != 1){
echo "<br/>An e-mail with a link has been sent to ". $mymail . ". Please click the link in order to verify your subscription";
}
}else{
echo "Something went wrong. Please try again and make sure you enter a correct e-mail adress.";
}
$existing = 0;
mysqli_free_result($result);
//If unsubscribing
}elseif($_POST['unsubscribedmail'] != ""){
//echo("Unsubscribe........");
//echo($_POST['unsubscribedmail']);
$result = mysqli_query($mysqli,"UPDATE users SET waiting_for_unsubscribe = 1 WHERE email = '" . $_POST['unsubscribedmail'] . "'");
//echo("Err code " . mysqli_errno($mysqli));
//echo("Err text " . mysqli_error($mysqli));
if(mysqli_errno($mysqli)){
echo("Error code " . mysqli_errno($mysqli) . "<br/>");
echo("Error text " . mysqli_errno($mysqli) . "<br/>");
}
$result = mysqli_query($mysqli,"SELECT id FROM users WHERE email = '" . $_POST['unsubscribedmail'] . "'");
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
//echo("ID------ " . $row['id']);
//Create the link for the user to verify that he/she unsubscribes
$url = "http://" . $_SERVER['HTTP_HOST'] . "/smic/subscription.php?unsubscribeid=" . $row['id'];
$mymail = $_POST['unsubscribedmail'];
$esubject = "Please verify that you want to unsubscribe from iAndApp's newsletter ";
$body = "Click the link in order to verify that you want to unsubscribe from iAndApp's newsletter (If you cannot click the link, just copy it and paste it into the adress field of your browser): " . $url;
$eemail = "NoReply#iandapp.com";
$sent = mail($mymail,$esubject,$body,"From: $eemailn");
if ($sent) echo "<br/>A mail with a link has been sent to ". $mymail . ". Please click the link in order to verify that you will unsubscribe from the newsletters.";
else echo "Something went wrong. Please try again and make sure you enter a correct e-mail adress.";
mysqli_free_result($result);
}elseif($_GET['unsubscribeid'] != ""){
$result = mysqli_query($mysqli,"UPDATE users SET subscriber = 0, waiting_for_unsubscribe = 0 WHERE id = " . $_GET['unsubscribeid']);
if($result){
echo("<p>You have now unsubscribed to the newsletter. Thank you for this time and we hope to see you again in the future. /iAndApp.<p/>");
}else{
echo("<p>Somthing went wrong, please click the link again!<p/> ".$result);
}
}else{
echo("<div class="subscribe">
<h4>Subscribe</h4>
<p>Subscribe to iAndApp's newsletter in order to get information about new and updated iPhone games and iPhone applications, that has been released by iAndApp. </p>
<form action="subscription.php" name="subscribe" method="post" onsubmit="return isValidEmailAndEqual()">
<p class="formlabel">Förnamn</p> <input type="text" name="first_name"/><br/>
<p class="formlabel">Efternamn</p> <input type="text" name="surname"/> <br/>
<p class="formlabel">E-mail</p> <input type="text" name="email1"/>
<br/>
<p class="formlabel">Repeat e-mail</p> <input type="text" name="email2"/> <br/>
<input class="inputsubmit" type="submit" value="Subscribe"/>
</form>
</div>");
echo("<div class="footer"></div>");
echo("<div class="subscribe">
<h4>Unsubscribe</h4>
<p>Fill in your e-mail address and submit in order to unsubscribe from iAndApp's newsletter. </p>
<form action="subscription.php" name="unsubscribe" method="post" onsubmit="return isValidEmail()">
<p class="formlabel">E-mail</p> <input type="text" name="unsubscribedmail"/><br/>
<input class="inputsubmit" name="submitbutton" type="submit" value="Unsubscribe"/>
</form>
</div>");
}
mysqli_close($mysqli);
echo("</div>");
?>
Of course, I don't expect you to go through and debug the entire application, but perhaps for the more expreienced folks out there, you know where to look.
I'm starting to get a bit stressed out here...
Please advice what the problem is and how I can solve it.
Thank you in advance!
eror in code:
echo("<div class="subscribe">
<h4>Subscribe</h4>
<p>Subscribe to i
in 1-st line you just close the string by 2-nd quote
you just need to correct code
You have to escape double quotes inside html tags or use single quotes. Right now, echo thinks it's closed after "<div class=". Use one of these:
echo "<div class=\"class\">";
or
echo '<div class="class">';
or
echo "<div class='class'>";
If you dont get any reposne from server (document is clear) or you have server errors but this is not all the time and sometimes script works fine it could be memory limit problem.
do{
$rand_int = rand(100000000, 999999999);
$result = mysqli_query($mysqli, "SELECT id FROM users WHERE id = " . $rand_int);
$no_of_rows = mysqli_num_rows($result);
}while($no_of_rows != 0);
This part of code dosn't look good for me, I suggest you to use auto_increment at id field in db (so id will be unique all the time)