2

I have this function in Utils.php

function GenerateMonthlyReport($connection, $month, $year, $objSheet)
 {     
     $months = array("Enero", "Febero", "Marzo", "Abril", "Mayo", "Junio" ,"Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");       
     $fromDate = $year . "-" . $month . "-01";
     $toDate = "";

   if ($month != 12)
    $toDate = $year . "-" . ($month+1) . "-01";
  else
   $toDate = ($year+1) . "-01-01";

 etc..
}

Then in another file createReport.php i call this function using $_POST parameters like this

 include "Utils.php";
 include "Connect.php";
 require_once('PHPExcel/Classes/PHPExcel.php');

 checkLogin();

 // Connect to Db
 $connection = openDb(); 

 // Input Data
 $month =  mysqli_real_escape_string($connection, $_POST["month"]);
 $year =  mysqli_real_escape_string($connection, $_POST["year"]);

 $objPHPExcel = CreateEmptyWorkbook();
 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007"); // create the writer
 $objSheet = $objPHPExcel->getActiveSheet();

 GenerateMonthlyReport($connection, $month, $year, $objSheet);

Problem: the parameters $year and $month that i am passing to the function GenerateMonthlyReport are null inside the function.

But if i do something like this:

 GenerateMonthlyReport($connection, "3", "2013", $objSheet);

they are passed correctly.

If i do a echo of $year and $month variables, they display values!!

Also: This strange behaviour only happens when i submit the Form from a Mobile Device. It does not happen from a PC browser.

EDIT: here is the Form HTML code:

    <form id="reportForm" action="/service/createMonthlyReport.php"  method="post">
   <table border="1">
   <tr>
   <td>
     Seleccione Per&iacute;odo
   </td>
   <td>
      <label>Mes</label><br/>
       <select id="monthM" name="month" > 
             <?php for ($i=1; $i<=12; $i++) :?>
               <option value="<?php echo $i; ?>"   <?php echo date("n") != $i ? "" : "selected" ?>>
                 <?php echo $monthName = date("M", mktime(0, 0, 0, $i, 10)); ?>
               </option>          
             <?php endfor; ?>
            </select>
            </td>

        <td>
        <label>Anio</label><br/>
            <select id="yearM" name="year" > 
             <?php for ($i=-5; $i<=5; $i++) :?>
                 <?php $yearData = (date("Y") + $i); ?>
               <option value="<?php echo $yearData; ?>"  <?php echo date("Y") != $yearData ? "" : "selected" ?>>
                 <?php echo $yearData; ?>
               </option>
             <?php endfor; ?>
            </select>                   
        </td>
        <td>    

            <input type="submit" value="Descargar Reporte Mensual" id="submitBtn" class="crearBtn" />
            </td>
    </tr>
    </table>
   </form>

openDB code:

function openDb() {
 $sever = "myserver";
 $database_name = "dbname";
 $user = "user";
 $pass = "mypass";

 $connection = new mysqli($server, $user, $pass, $database_name) or die("DB connection failed!");;
 $connection->set_charset("utf8");
 mysqli_query($connection, 'SET NAMES utf8');
 mysqli_query($connection, 'SET CHARACTER SET utf8');

 return $connection;
}
Herno
  • 1,497
  • 2
  • 21
  • 40
  • 1
    Are your value populated in `$_POST`? – Mike Brant Mar 18 '13 at 23:00
  • yes they are. And also, after applying the mysqli_real_Escape_string function. the values are correct. It seems that it is impossible to pass arguments to a function using variables. This is driving me mad. – Herno Mar 18 '13 at 23:02
  • are you sure after mysqli_real_escape they are intact? Sorry , really surprised. – Volkan Mar 18 '13 at 23:05
  • Yes, i also tried this: GenerateMonthlyReport($connection,$_POST["month"], $_POST["year"], $objSheet); and still, the variables are empty inside the funcion. – Herno Mar 18 '13 at 23:07
  • Perhaps an encoding issue? Before you call the function, try hex dumping the string, such as with http://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php – azhrei Mar 18 '13 at 23:15
  • Try checking the encoding is what you expect, with http://fr.php.net/mb_check_encoding. Possibly related is http://stackoverflow.com/questions/5270591/post-utf-8-encoded-data-to-server-loses-certain-characters – azhrei Mar 18 '13 at 23:25
  • @user1569279 Can you try to `var_dump()` the post values from your phone and comment out the `GenerateMonthlyReport()` call to see what the values are? – HellaMad Mar 18 '13 at 23:25
  • mb_check_encoding returns true in both cases. The binary hex dump is the same in both cases too(when submitting from my phone and from my PC). For example i submit year=2013 and the Hex is 32303133 – Herno Mar 18 '13 at 23:31
  • @user1569279 Also can you post the HTML of the form (or whatever you're getting the POST values from)? Might be a browser compatibility issue. – HellaMad Mar 18 '13 at 23:35
  • @Ihsan I hope you don't feel that I was condescending to you, I was just trying to understand what you were trying to accomplish. Apologies. – HellaMad Mar 18 '13 at 23:48
  • @Ihsan I understand that, I wasn't intending to sound harsh. I don't want to continue this in the comments much longer though. I'm not the person who asked the question, though, so I don't know what you mean by me not deserving help? As far as why I was questioning your answer, this site is intended for accurate answers and as far as I could tell it wasn't accurate. – HellaMad Mar 18 '13 at 23:54
  • I think something is happening in OpenDB() and `$connection` is null when call `mysqli_real_escape_string`. So, more info on OpenDB() and checkLogin(), please. Maybe the Android browser is failing to send the cookie or login properly. Or maybe the high latency of the cell network is causing an issue. I would rule that out really. But, the cell network might be adding headers that are confusing the session somehow. Could you post an example of the header sent with each request, PC and Android, as well? – Motomotes Mar 19 '13 at 00:23
  • 1
    ` – ITroubs Mar 19 '13 at 01:01
  • Yes you are right. Thanks for pointing it. I still have not found an answer for this strange beahivour. – Herno Mar 19 '13 at 17:38
  • Any news? Have you tried out my suggestions? – Walter Tross Apr 02 '13 at 20:44
  • Walter, i'll be checking your suggestion this week.Thanks for the help! – Herno Apr 03 '13 at 20:53

1 Answers1

1

Your DB encoding is UTF-8, but you avoid characters above 128 in your code by using HTML entities like &iacute;. I wonder if some ISO 8859-1 in your UTF-8 workflow could be breaking PHP or one of its extensions. Make sure you have the proper header() for UTF-8, and that everything (e.g., hidden form variables) is properly UTF-8 encoded.

Anyway, a variable (two in your case) that is null when it shouldn't be, could be the result of a wrong reference count. References are the low level mechanism by which a variable's value is not copied when it is assigned to another variable, but both variables point to the same value, which has a reference count of 2 (or more). If, e.g., the reference count is not incremented on assignment, but is decremented when a variable goes out of scope, it could become 0 when it should be 1, and the value is erroneously freed.

I think I have already seen a reference count bug. It could have been in a database extension (possibly the sybase extension, but I'm not sure). If so, it was probably in a *_fetch_*() function.

Try to take out pieces of your code. Try leaving out the DB calls (including checkLogin(), I guess), and/or the PHPExcel part, and/or others, before getting to the function where you have the problem (GenerateMonthlyReport()).

Check if you have PHP references (&) in your code. If so, try to do without them (they are not bad in themselves, but being complicated at the low level, they are more likely to trigger a bug of the above kind).

In order to rule out name collisions, try also the following: rename year and month to year0 and month0 in the form and in $_POST, $year and $month to $year1 and $month1 in the main, and $year and $month to $year2 and $month2 in GenerateMonthlyReport().

Walter Tross
  • 12,237
  • 2
  • 40
  • 64