Below is the fully functioning code. It works when I call it via the browser. However, I can't get my Cron job to run it. I have tried coming up with any solution and am open to any ideas that will make this script fire off once every hour 24/7 365 so to speak.
Info:
Hostgator
PHP 5.4
Link for Hostgator Cron commands: http://support.hostgator.com/articles/cpanel/what-do-i-put-for-the-cron-job-command?utm_source=cPanel&utm_medium=message&utm_campaign=Cron%20Jobs
I have tried the following:
When doing any of these I have yet to have it add a sale to the database or send a single email:
/opt/php54/bin/php /home1/user/public_html/Sales/scripts/sales-notif_em.php - Could not open input file: /home1/user/public_html/scripts/sales-notif_em.php
php /home1/user/public_html/Sales/scripts/sales-notif_em.php - blank email with nothing but this showing: Content-type: text/html
php -q /home1/lotscav1/public_html/Sales/scripts/sales_notif_em.php - I don't get any emails from the system of course
/usr/bin/curl home1/user/public_html/scripts/sales-notif_em.php - curl: (3) malformed
any command (like php or /usr/bin/curl) that has the second half with http://website.c0m/Sales/scripts/sales_notif_em.php - causes a JSON Error when trying to add the Cron job
I have checked the following things:
File permissions are 655
Timestamp is less than an hour old under the customers table
Here is my code:
<?php
//find out current time and 1 hour ago
date_default_timezone_set('America/New_York');
$current_time = strtotime("now");
$hour_ago = strtotime('-1 hour');
//////////////////////////////////////////////////////////////////
/////////////////l// Connect to Sales Database ///////////////////
//////////////////////////////////////////////////////////////////
$mysqli_s = new mysqli("localhost", "user", "password",
"server_sales_data");
if ($mysqli_s->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli_s->connect_errno . ")
" . $mysqli_s->connect_error;
}
//////////////////////////////////////////////////////////////////
///////////////////// Connect to EM Database ////////////////////
//////////////////////////////////////////////////////////////////
$mysqli_em = new mysqli("localhost", "user", "password",
"server_dlgEM");
if ($mysqli_em->connect_errno) {
echo "Failed to connect to MySQL_EM: (" . $mysqli_em->connect_errno .
") " . $mysqli_em->connect_error;
}
//Grab store name
$dlg_store = "EM";
$em_request = "SELECT * FROM customers WHERE date BETWEEN '$hour_ago'
AND '$current_time'";
$em_result = mysqli_query($mysqli_em, $em_request) or die("Error No
Sales EM");
while ($em_row = mysqli_fetch_array($em_result)) {
$em_prod_num = $em_row["prod_num"];
$em_receipt = $em_row["receipt"];
//////////////////////////////////////////////////////////////////
///////////////////// Grab info for EM Sales ////////////////////
//////////////////////////////////////////////////////////////////
$request_s = "SELECT * FROM all_products WHERE
dlgprod_num='$em_prod_num' AND dlg_store='$dlg_store'";
$result_s = mysqli_query($mysqli_s, $request_s) or die("Error dlg
prod num EM");
while ($row_s = mysqli_fetch_array($result_s)) {
$sku_s = $row_s["sku"];
$dlgprod_num_s = $row_s["dlgprod_num"];
$book_title_s = addslashes($row_s["book_title"]);
$dlgprod_price_s = $row_s["dlgprod_price"];
$author_name_s = addslashes($row_s["author_name"]);
$author_email_s = $row_s["author_email"];
$publisher_s = $row_s["publisher"];
$dlg_store_s = $row_s["dlg_store"];
$add_sql_s = "INSERT INTO all_author_sales SET
`sku`='$sku_s',
`dlgprod_num`='$dlgprod_num_s',
`dlgprod_nam`='$book_title_s',
`dlgprod_price`='$dlgprod_price_s',
`author_name`='$author_name_s',
`author_email`='$author_email_s',
`publisher`='$publisher_s',
`dlg_store`='$dlg_store_s',
`dlgcustomer_receipt`='$em_receipt' ";
//create signature
$sig = "The Admin Team at www.website.com";
//to
$admin_email = "admin#website.com";
$to = array($author_email_s, $admin_email);
//setup email headers
$headers='From: ' . $admin_email . "\r\n" .
'Reply-To: ' . $admin_email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= $emailbody."\n\n";
//email subject and body
$subject = "Your book stats";
$message = "
Hi $author_name_s,<br />
I just wanted to send you a message and let you know that the book or
books below have just been purchased.<br /><br />
Store: $dlg_store_s<br />
Receipt: $em_receipt<br />
Sku Number: $sku_s<br /><br />
Book Title: $book_title_s<br />
Publisher: $publisher_s<br />
Product Number: $dlgprod_num_s<br />
Price: $dlgprod_price_s<br /><br />
Sincerely,<br />
$sig<br /><br />
To remove yourself from this notification, please send an email to
$admin_email with Unsubscribe in the subject line.
";
if ($mysqli_s->multi_query($add_sql_s) === TRUE) {
mail (implode(',', $to), $subject, $message, $headers);
} else {
echo "Error: " . $add_sql_s . "<br>" . $mysqli_s-
>error . "<br>" . $string;
}
}
}
?>
My bet is with wrong path to the php file you want to execute.
Try using terminal if you can, navigate to the folder where the file is placed and try running it from there via command line.
Also mind You are refering to two different names: sales-notif_em.php & sales-notif_me.php.
(Posted on behalf of the OP).
Thank you for all your help. I made a custom product with my email as the author for testing purposes. In the customers table I inserted products 111111 and 111110. However, in all_products I gave products 11111111 and 11111110. tHE end result is I made a mistake.
I hope you are able to learn something from this event. Knowledge is more important than my ego. Thank you!
Related
I am trying to make a registration page with around 10 details. But in no way am I able to update the database with those details. I have followed every procedure i found on every site including stackoverflow but still whenever i submit my form nothing happens. Like when i press the submit button it goes to another page and the address bar shows everything correctly but yet i don't know why there is no change in the database. I am new to JSP and Netbeans, so i am not really aware of all the things.
I used this as my guide http://www.studytonight.com/servlet/registration-form-example-in-servlet.php
P.S. I am new to JSP and netbeans, so i am not aware of a lot of things. Thanks in advance.
First Create a class like this
class DBController {
private $host = "Your IP";
private $user = "User Name";
private $password = "Password";
private $database = "Your Database";
function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function insertQuery($query) {
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
return $result;
}
}
}
?>
Create a text box
<input type="text" class="" name="patfirstname" value="<?php if(isset($_POST['patfirstname'])) echo $_POST['patfirstname']; ?>">
And a submit button
<input type="submit" name="submit" value="Submit" />
And in the same registration form create a post PHP function
<?php
if(isset($_POST['submit'])== "POST")
{
require_once("dbcontroller.php");
$db_handle = new DBController();
$query = "INSERT INTO new_table ( Name ) VALUES ( '" . $_POST["patfirstname"] . "');";
$result = $db_handle->insertQuery($query);
}
?>
Hope this helps..
Like the question states, I would like to create a new directory in the "Users" folder on the server when a user registers their account.
PHP:
$username = $_POST['username'];//STRING
$username = strip_tags($_POST['username']);
$username = stripslashes($_POST['username']);
$username = mysqli_real_escape_string($conn,$_POST['username']);
etc.
etc.
$sql = "INSERT INTO tbl_users
VALUES ('$username', '$password', '$countryCode','$mobileNumber','$email','$profilePicture','$fname','$mname','$lname','$gender','$birthday','$occupation','$signupdate',0)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully in tbl_users";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
I'm quite new to php and I don't really understand how most of the commands work.
I am making an app in java and I want the onClick to add data to a database as well as create the following file hierarchy:
Users(Already there)
Username(Of user registering)
Media
Pictures
Videos
Audio
To add a directory in "Users" folder,You can add this code:
mkdir("Users/new_folder",0755);
I am new to php .. anyway I have an application that require to provide the vacation information from the database to the supervisor and then allow the supervisor to enter his approval of this vacation ,
To do this, I use 2 php pages:
one to retrieve the employee vacation request information
and the other to insert the supervisor approval into the database
I want the application to insert this approval value in the record of this employeeID that I retrieve his information in this application.
The retrieval of employee information is done correctly, but the insert doesn't work !
I try this query but it didn't work (it didn't return any results when I exceute the query):
<?php
if(! $conn)
{
die('Could not connect: '.mysql_error());
}
mysql_select_db("a2202757_OURDATA",$conn);
$flag['code']=0;
if($r = mysql_query ("UPDATE request_vacation SET VacationApprove='".$_POST['Approval']."' WHERE IDEmployee=$id",$conn));
{
$flag['code']=1;
}
print(json_encode($flag));
mysql_close($conn);
?>
What is the problem ?
What happens is that you assign a variable ( = is an assignment for comparison you use == or ===) in an if statement.
When you want to check and assign at the same time use this:
if(false !== $r = $mysql_query("UPDATE .......") {
echo 'Update successfull';
}
else {
echo 'Update failed with error ' . mysql_error();
}
It is difficult to solve the problem if you do not provide any error message about the database connection. Please use the following code and using your host and username password to let php to print out the error message. It would be useful if you can provide further error information.
$conn = mysql_connect('localhost','username','password');
if(! $conn)
{
die('Could not connect: '.mysql_error());
}
$db_selected = mysql_select_db("a2202757_OURDATA",$conn);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
$flag['code']=0;
$r = mysql_query ("UPDATE request_vacation SET VacationApprove='".$_POST['Approval']."' WHERE IDEmployee=$id",$conn);
if (!$r) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
if($r != null);
{
$flag['code']=1;
}
print(json_encode($flag));
mysql_close($conn);
Try this
<?php
if (isset ( $_POST ['Approval'] )/* && isset ( $_POST ['IDEmployee'] )*/) {
mysql_connect ( 'server', 'username', 'password' ) or die ( mysql_error () );
mysql_select_db ( 'a2202757_OURDATA' );
// mysql_query ( "UPDATE request_vacation SET VacationApprove='" . $_POST ['Approval'] . "' WHERE IDEmployee= '" . $_POST ['IDEmployee'] . "'" ) or die ( mysql_error () );
// Hey, What is $id, I leave it up to you.
mysql_query ( "UPDATE request_vacation SET VacationApprove='" . $_POST ['Approval'] . "' WHERE IDEmployee= '" . $id . "'" ) or die ( mysql_error () );
if (mysql_affected_rows () != - 1) {
// some rows has affected
echo mysql_affected_rows () . 'rows has affected';
} else {
// No row has affected
}
} else {
die ( 'Approval '/* . 'and/or IDEmployee'*/ . ' are not properly set' );
}
?>
and, please give some more detail
I think your code formatting is somehow broken on stackexchange.
Below is an example of how to use queries, because you code seems to be fairly broken and nonsense.
Attention: Don't use that in a production environment because it includes some SQLInjection vulnerabilities.
$id = $_POST["IDEmployee"];
// this sets the VacationApprove clumn of request_vacation
// to the $_POST['Approval'] variable where IDEmployee matches
// the previously selected $id
$insRes = mysql_query("UPDATE request_vacation SET VacationApprove = '".$_POST['Approval']."' WHERE IDEmployee = '".$id."'")
I don't know what you are trying to achieve. Please be more specific about your problem.
I’m using PHPRunner and I'm trying to create a button to send an email to selected users with personalized body message. I found an example in PHPRunner support page (Send an email to selected users) but it is only with an hardcoded message, so I’m trying to modify the example.
I managed to send correctly the email with personalized message if I only select one user, but if I select more than one all of them will receive the same message.
This is what I have now:
$emails = array();
while( $data = $button->getNextSelectedRecord() )
{
if( $data["EMAIL_FIELD"] )
$emails[] = $data["EMAIL_FIELD"];
$body = $data["MESSAGE_FIELD"];
}
// send the email
$email = implode(", ", $emails);
$subject = "";
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
$result["txt"] = "Email were sent.";
// if error happened print a message on the web page
if( !$arr["mailed"] )
{
$errmsg = "Error happened: <br>";
$errmsg.= "File: " . $arr["errors"][0]["file"] . "<br>";
$errmsg.= "Line: " . $arr["errors"][0]["line"] . "<br>";
$errmsg.= "Description: " . $arr["errors"][0]["description"] . "<br>";
$result["txt"] = $errmsg;
}
What should I change? Thank you.
Problem:
In the foreach loop you are appending every new email address to one variable:
$emails[] = $data["EMAIL_FIELD"];
and you are assigning new value to $body variable (overriding the old one) in each iteration:
$body = $data["MESSAGE_FIELD"];
Therefore when you are calling runner_mail just once outside of the while loop, you will send same body (from the last iteration) to all recipients (you built an array of all recipients using while loop).
Solution:
You need to call runner_mail for each recipient (as the body is different). You can achieve it by moving the call to runner_mail inside the while loop:
while( $data = $button->getNextSelectedRecord() )
{
// Send only if email field not empty
if( $data["EMAIL_FIELD"] ) {
// This no longer should be an array of emails
$email = $data["EMAIL_FIELD"];
$body = $data["MESSAGE_FIELD"];
$subject = "";
// Email will be sent for each record
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
}
}
I need to add a code to my sendEmail.php bellow which would rotate evenly between two or more recipient. Right now all goes to one recipent "sendto" myemail#gmail.com. So I want to add more myemail-1#gmail.com, myemail-2#gmail.com and so on. This way each will receive fresh leads.
<?php
ob_flush();
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone_1']."- ".$_POST['phone_2']."- ".$_POST['phone_3'];
$_SESSION['liberation'] = $_POST['liberation'];
$sendto = "myemail#gmail.com";
$email = $_POST['email'];
$username= nl2br($_POST['username']);
$subject = "New lead from my website";
$headers = "From: <form#manysites.com> \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<p><strong>Name:</strong> ".$_POST['username']."</p>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$_POST['email']."</p>\r\n";
$msg .= "<p><strong>Phone No.: </strong> ".$_POST['phone_1']."- ".$_POST['phone_2']."- ".$_POST['phone_3']."</p>\r\n";
$msg .= "<p><strong> conviction date:</strong> ".$_POST['liberation']."</p>\r\n";
$msg .= "</body></html>";
#mail($sendto, $subject, $msg, $headers);
header("Location:continue.php");
?>
You can simply use the comma operator and then send it right away.. like this..
$sendto = 'myemail1#gmail.com,myemail2#gmail.com,myemail3#gmail.com';
mail($sendto, $subject, $msg, $headers);
Alternatively, you can use an implode() too..
$myemails = array('myemail1#gmail.com','myemail2#gmail.com','myemail3#gmail.com');
$sendto = implode(',',$myemails);
mail($sendto, $subject, $msg, $headers);
Sending them separately....
$myemails = array('myemail1#gmail.com','myemail2#gmail.com','myemail3#gmail.com')
foreach($myemails as $email)
{
mail($email, $subject, $msg, $headers);
}
I think the challenge here is that this script runs with no knowledge of what happened on any occasion that it previously ran. So, one option would be to store the information about who received the last lead. This way when the next person fills in your web form, you can retrieve the value of the last recipient and send the current lead to the next recipient in an array of defined recipients (then update the information about who was the last recipient for the next time).
If you don't have access to a database (or file or something else that is a 'permanent' type of storage) to store the value of the last recipient in, you could simply randomize who receives the current lead. The law of averages suggests that over time, the lead distribution should be relatively equal, though in practice this could result in one person getting all of the leads in a given period.
$recipients=array("recipient1#email.com","recipient2#email.com");
$randnum = mt_rand(0,1);
$sendto = $recipients[$randnum];