Hi, tried to update record in table and send email to the responsible person but no email is being sent. When current user select the responsible person from the option select and click Notify2 button, it suppose to update Responsible2 in improvement_plan table and send email to notify the Responsible2 person. Please advise.Below is the code:

<?php
error_reporting(E_ALL ^ E_NOTICE);
$con = mysql_connect("localhost","user","");


    if (!$con){


die("Can not connect: " . mysql_error());


    }


mysql_select_db("pq",$con);
$sql = "SELECT * FROM user";
$rs = mysql_query($sql) or die(mysql_error());
echo "<select name='responsible2'>";


    while($row = mysql_fetch_array($rs)){


echo "<option value=".$row['Position'].">".$row['Position']."</option>";
"<option value=".$row['Email']."</option>";


    }


$email=$row['Email'];
mysql_free_result($rs);
"</select>"; 



    if(isset($_POST['Notify2'])){



$Responsible2=$_POST['responsible2'];
echo $Responsible2;
$cemail=$_POST['Email']; 
echo $cemail;
mysql_query("INSERT INTO improvement_plan (Responsible2,Progressid) VALUES ('" . $_POST['responsible2'] . "','" . $_SESSION['Progressid'] . "')"); 
$Ipid = mysql_insert_id();

if(!empty($Ipid)) { 
$message = "New improvement plan added successfully"; 


    }



$sql ="SELECT Email FROM user where Userid='".$_SESSION['Userid']."'";
$rs = mysql_query($sql) or die(mysql_error());
if($row=mysql_fetch_array($rs))


    {


"Email:<input type='text' name='Email' value='".$row['Email']."'><br>";
mysql_free_result($rs);
echo $row['Email'];


    }  


require 'PHPMailer_5.2.4/PHPMailer_5.2.4/class.phpmailer.php';
$mail = new PHPMailer;


    $mail->IsSMTP();                                      // Set mailer to use SMTP


$mail->Host = 'smtp.office365.com';   // Specify main and backup server


    $mail->SMTPAuth = false;                               // Enable SMTP authentication


$mail->From = $row['Email'];
$mail->FromName = $_SESSION['Username'];


    $mail->AddAddress('$cemail');                               // Add a recipient


$mail->IsHTML(true);                                  // Set email format to HTML
$mail->Subject = 'system Notification';


    $mail->Body    = 'Improvement Plan';


$mail->AltBody = 'Improvement Plan.';



    if(!$mail->Send()) {


   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;


    }




    }


echo 'Message has been sent'; 
?>
<form name="" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">


        <input type="hidden" name="Progressid" value="<?php echo $_SESSION['Progressid']; ?>" >


    <table width="850" border="1">
      <tr align="center">
        <th width="80" scope="col">Responsible</th>
        <input type="submit" name="Notify2" value="Notify"> 
    </tr> 
<?php 
 $disable = '';


        $i = 0;


    $d = 0;

$con = mysql_connect("localhost","user","");
if (!$con)


    {


die('Could not connect: ' . mysql_error());


    }


mysql_select_db("pq", $con);    

$sql = "SELECT * FROM improvement_plan where  Ipid='" . $Ipid . "'";
$res_list = mysql_query($sql) or die(mysql_error());


    if( isset( $_SESSION['Progressid'] ) ) {


  $Progressid = $_SESSION['Progressid'];  


      }




    while($row_list = mysql_fetch_array($res_list)){


    $i++;       
?>  
 <td align="center"><div id="Responsible2<?php echo $i; ?>"><?php if($row_list['Responsible2'] != '0000-00-00'){ echo ($row_list['Responsible2']); }else{ echo '-'; } ?></div></td> 
 <td align="center"><div id="Progressid<?php echo $i; ?>"><?php if($row_list['Progressid'] != '0000-00-00'){ echo ($row_list['Progressid']); }else{ echo '-'; } ?></div></td>
 </tr>
<?php


    } 




    if($i == 0){


?>
<tr><td colspan="7">No data.</td></tr>
<?php
  $disable = 'disabled="disabled"';


    }  


?>
</table>
</div>

Dani AI

Generated

Brief diagnosis and a focused fix list that ties together points made by and (and supports @diafol's formatting remark). The primary causes are HTML/PHP structure and logic errors (select is rendered outside the form; session usage and closing tags are inconsistent), a wrong database action (an INSERT is used where an UPDATE is expected), and PHPMailer misconfiguration/usage (address passed as a literal string, SMTP auth disabled).

Concrete, actionable steps:

  • Ensure session_start() runs before any output when $_SESSION is used.
  • Put the <select name="responsible2"> inside the <form method="post" ...> and close the form. Make each <option> value carry a stable identifier (user id) or the email itself so the posted value is usable.
  • Replace the INSERT into improvement_plan with an UPDATE when changing an existing row. Example SQL pattern: UPDATE improvement_plan SET Responsible2 = ? WHERE Progressid = ?. After update, fetch the responsible user email with SELECT Email FROM user WHERE Userid = ?.
  • Stop using deprecated mysql_*. Move to mysqli or PDO and prepared statements to avoid SQL injection and to get reliable affected-row/info APIs.
  • Fix the PHPMailer usage: pass the recipient variable (not a quoted string), set SMTPAuth to true for Office365, use TLS and port 587, and authenticate with a valid SMTP account. Example settings to check: host smtp.office365.com, SMTPAuth = true, SMTPSecure = 'tls', Port = 587. Set From to the authenticated account or another allowed sender. Enable $mail->SMTPDebug while testing and inspect $mail->ErrorInfo on failure.

Quick debug checklist: enable error_reporting(E_ALL), var_dump($_POST) right after submit to confirm values, log the exact SQL being executed (with parameters), confirm UPDATE affected rows, and then run PHPMailer with debug enabled. These steps directly address the structural errors pointed out by and the readability/formatting request from , and will make it clear why no email was sent.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

Please reformat your code with proper indenting, this is impossible to read. In addition, use the Code button in the editor as your original post did not contain a proper codeblock until I edited it.

First of all, supporting @diafol's comment on the 'proper indenting' issue. Proper indenting easier the job for us.

Then the problem in this program:
1. The <select> and the looping for its <options> is outside the <form> tag where it is imposible for the selection to be 'post' to $_SERVER['PHP_SELF'].
2. The html tags no match with each other and causing layout messed up. Example, there is no closing of <form> tag or not open for <td> and having </td> in while loop.
3. There is no update queries to update the table from your codes.
4. Code like this doesn't work

echo "<option value=" . $row['Position'] . ">" . $row['Position'] . "</option>";
"<option value=".$row['Email']."</option>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.