0

An Excel file is uploaded from a html form the regno and email information are read from the excel file.Each user is then sent an email along with a message.The regno and email are inserted in the database. gmail is used for sending emails $frmid is the from email address and $password is the password for it. Here is the code.

<?php
 require_once("function/PHPExcel/Classes/PHPExcel/IOFactory.php");
require_once("function/Mail/class.phpmailer.php");
require_once("function/connection.php");
if($_SERVER['REQUEST_METHOD']=='POST' && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
    $msg=$_POST['msg'];
    $arr=array();
    $frmid=htmlspecialchars(trim($_POST['frm_id']),ENT_QUOTES,'UTF-8');
    $password=htmlspecialchars(trim($_POST['password']),ENT_QUOTES,'UTF-8');
    $subject=htmlspecialchars(trim($_POST['subject']),ENT_QUOTES,'UTF-8');
    $name=$_FILES['uploaded_file']['tmp_name'];     
    try {
        $inputFileType = PHPExcel_IOFactory::identify($name);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($name);     
        $sheet = $objPHPExcel->getSheet(0); 
        $highestRow = $sheet->getHighestRow();
        for ($row = 1; $row <= $highestRow; $row++){ 
            $email = $sheet->getCell('B'.$row )->getValue();    
            $regno = strtoupper($sheet->getCell('A'.$row )->getValue());
            $arr[]=array($regno,$email);
            $message=$msg;
            $mail = new PHPMailer(); 
            $mail->IsSMTP(); 
            $mail->SMTPDebug = 0;
            $mail->SMTPAuth = true; 
            $mail->SMTPSecure = 'tls'; 
            $mail->Host = "smtp.gmail.com";
            $mail->Port = 587; 
            $mail->IsHTML(true);
            $mail->Username = $frmid;
            $mail->Password = $password;
            $mail->SetFrom($frmid);
            $mail->Subject = $subject;
            $mail->MsgHTML($msg);
            $mail->AddAddress($email);
                if(!$mail->Send()){
                    die("Failed to send to Email Id ".$email."\n<br/>");
                }
            }
        } catch(Exception $e) {
        die('Server Error');
    }
    $db=connection();
    $query="INSERT INTO table(regno,email) VALUES ";
    $query=buildinsert($query,$highestRow);
    $stmt=$db->prepare($query);
    $result=$stmt->execute($arr);
    if($stmt->rowCount()==$highestRow){
        redirect("page.php?result=1");
    }else{
        redirect("page.php?result=0");
    }
}
?>

The functions used are

<?php
function buildinsert($query,$rows){
$placeholder=array();
 for($i=0;$i<$rows;$i++){
    $placeholder[]="(?,?)"; 
 }
 return($query.implode(",",$placeholder));
}

function connection(){
        try{
            return (new PDO("mysql:host=localhost;dbname=dbms,","root","passwd"));
        }catch(Exception $e){
            die( "An error occured".$e->getMessage());
        }
    }

    function redirect($link){
        header("Location:".$link);
    }


?>

The problem is I`m not being redirected after the operation completes.The mails are sent successfully and the content is also added but the page does not change.I get same page as before

AAB
  • 1,594
  • 2
  • 25
  • 40
  • 1
    If you add `die;` at the end of `redirect()`, do you still have the problem? – Jon Jun 05 '14 at 23:09
  • Let me try adding die at the end of redirect – AAB Jun 05 '14 at 23:10
  • It's very unusual to *not* die after issuing a `Location` header. So unusual that after all these years I 'm not even sure what happens. – Jon Jun 05 '14 at 23:11
  • Then you have a more basic problem. Make sure warnings are enabled, and if you get "headers already sent" see [here](http://stackoverflow.com/q/8028957/50079). FYI, I 've voted to close this question as "lacking sufficient information to diagnose the problem". – Jon Jun 05 '14 at 23:16
  • when I click view source chrome does not show the source it shows the message confirm from resubmission(page not loaded/dead symbol) after I click refresh the source is displayed as 'Failed to send to Email Id
    '
    – AAB Jun 05 '14 at 23:18
  • @Jon Please help me find the error I have tried for a long time now.can I change SMTPdebug =1 and show you what are the message I get? – AAB Jun 05 '14 at 23:20
  • @Jon The code works fine when I run it in wamp but after I upload on hostinger then it stops redirecting – AAB Jun 05 '14 at 23:21
  • Sorry, I can't debug your code for you. What you need to do is very basic, there's documentation on how to turn warnings on pretty much everywhere (including here). – Jon Jun 05 '14 at 23:22
  • No probs just don`t close this question cause I`m gonna get a ban if you do :P – AAB Jun 05 '14 at 23:30

1 Answers1

0

Found the Problem.The phpmailer,phpexcel are fine the problem was with connection.php

After I added buildinsert function I left a newline,The page was not redirecting because of the newline space between

Thanks Jon!

AAB
  • 1,594
  • 2
  • 25
  • 40