Simple but driving me nuts

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
MikeKJ
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Mar 06, 2017 9:38 am

Mon Mar 06, 2017 9:42 am

$sent = wp_mail( $from, $subject, $message );
// If status correct then state the email has been sent
if ( $sent ) {
echo "Mail sent to $name at $email";
mysqli insert statment here
} else {
echo "Mailing failed";
}

for some weird reason the insert works fine so $sent must have evaluated to true but the echo statement dont output to screen! Any clues please?
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 998
Joined: Mon Feb 22, 2016 5:52 pm

Mon Mar 06, 2017 11:39 am

Do you have display errors switched on?

I suspect you are getting a syntax error on line 5.
MikeKJ
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Mar 06, 2017 9:38 am

Tue Mar 07, 2017 1:44 am

No errors reported, some expanded code snippet below, still the echo statements do not display to screen but the insert statement does get run so $sent must evaluate to true

error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = "SELECT * from table_name where name='$name' and email='$email'";
$result = $conn->query($sql);
$count = mysqli_num_rows($result);
if ($count == 0){
// some stuff here
$sent = wp_mail( $from, $subject, $message );
// If status correct then state the email has been sent
if ( $sent ) {
echo "Mail sent to $name at $email";
$time = time();
$digest = md5($key . $time);
$link = mysqli_connect($hostname, $username, $password, $dbname);
$sql = "INSERT INTO table_name (name, email, date, hash) VALUES ('$name', '$email', '$date', '$digest')";
mysqli_query($link, $sql);
echo "\r\n\nData of the offer entered successfully\n";
mysqli_close($conn);
} else {
echo "Mailing failed";
} // end if sent
} // end count
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 998
Joined: Mon Feb 22, 2016 5:52 pm

Tue Mar 07, 2017 1:56 pm

Try the following:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo 'Entering script:<br>';

$sql = "SELECT * from table_name where name='$name' and email='$email'";
$result = $conn->query($sql);
$count = mysqli_num_rows($result);
if ($count == 0){
// some stuff here
$sent = wp_mail( $from, $subject, $message );
// If status correct then state the email has been sent

/*
  Try using this line (below) and similar throughout your code to check that the values are what you think they are
*/
echo 'The value of $sent : ',$sent;

if ( $sent ) {
echo "Mail sent to $name at $email";
$time = time();
$digest = md5($key . $time);
$link = mysqli_connect($hostname, $username, $password, $dbname);
$sql = "INSERT INTO table_name (name, email, date, hash) VALUES ('$name', '$email', '$date', '$digest')";
mysqli_query($link, $sql);
echo "\r\n\nData of the offer entered successfully\n";
mysqli_close($conn);
} else {
echo "Mailing failed";
} // end if sent
} // end count
Does the email get sent?
NigelRen
php-forum GURU
php-forum GURU
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Wed Mar 08, 2017 12:33 am

I know this isn't a direct answer to your question, but something that may be worth spending some time setting up is debugging.
I use Eclipse to develop PHP (and others) and use XDebug to debug on a 'remote' server. It allows me to examine variables and step through code. I used http://bogdan-albei.blogspot.co.uk/2010 ... g-and.html to help me configure it.
One thing to be careful about though is that sometimes it can get over eager and wants to debug php phpMyAdmin :-/
MikeKJ
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Mar 06, 2017 9:38 am

Wed Mar 08, 2017 1:01 am

Hi Hyper

Yes the mail does get sent and the insert inside the if $sent is done as well
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 998
Joined: Mon Feb 22, 2016 5:52 pm

Thu Mar 09, 2017 1:48 pm

As Nigel says, you need a method to debug what is going on, this is why I suggested the last script with the echos.

To move on:

what is displayed on the screen?
Have you confirmed that the email has been sent by receiving it yourself?
do these lines get executed (from the suggested script)?

Code: Select all

echo 'Entering script:<br>';
echo 'The value of $sent : ',$sent;
Debugging is a process of elimination and confirmation.

Can you also add the following to your script before the (if ($sent)):

Code: Select all

echo '<br>$name: ', $name, '<br>$email: ', $email;
Post Reply