简体   繁体   English

PHP传递参数以实现奇怪的行为

[英]PHP passing parameters to function strange behaviour

I have this function in Utils.php 我在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 然后在另一个文件createReport.php我使用$_POST参数调用此函数

 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. 问题:我传递给函数GenerateMonthlyReport的参数$year$month在函数内部为null

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!! 如果我对$year$month变量进行回显,它们将显示值!

Also: This strange behaviour only happens when i submit the Form from a Mobile Device. 另外:仅当我从移动设备提交表单时,才会发生这种奇怪的行为。 It does not happen from a PC browser. 它不是通过PC浏览器发生的。

EDIT: here is the Form HTML code: 编辑:这是表单HTML代码:

    <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: openDB代码:

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;
}

Your DB encoding is UTF-8, but you avoid characters above 128 in your code by using HTML entities like &iacute; 您的数据库编码为UTF-8,但是通过使用&iacute;这样的HTML实体&iacute;代码中的128个以上的字符&iacute; . I wonder if some ISO 8859-1 in your UTF-8 workflow could be breaking PHP or one of its extensions. 我不知道您的UTF-8工作流程中的某些ISO 8859-1是否会破坏PHP或其扩展之一。 Make sure you have the proper header() for UTF-8, and that everything (eg, hidden form variables) is properly UTF-8 encoded. 确保您具有适用于UTF-8的适当的header() ,并且所有内容(例如,隐藏的表单变量)均经过正确的UTF-8编码。

Anyway, a variable (two in your case) that is null when it shouldn't be, could be the result of a wrong reference count. 无论如何,一个变量(在您的情况下为两个)在不应该为null情况下可能是错误的引用计数的结果。 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). 引用是一种低级机制,在将变量的值分配给另一个变量时,该机制不被复制,但是两个变量都指向相同的值,该值的引用计数为2(或更大)。 If, eg, 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. 例如,如果引用计数在赋值时未增加,但在变量超出范围时减小,则当应为1时可能变为0,并且错误地释放了该值。

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). 它可能在数据库扩展中(可能是sybase扩展,但我不确定)。 If so, it was probably in a *_fetch_*() function. 如果是这样,它可能在*_fetch_*()函数中。

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() ). 在进入有问题的函数( GenerateMonthlyReport() )之前,尝试checkLogin()数据库调用(包括checkLogin() ,和/或PHPExcel部分和/或其他)。

Check if you have PHP references ( & ) in your code. 检查代码中是否包含PHP引用( & )。 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() . 为了排除名称冲突,尝试以下方法:重命名yearmonthyear0month0在形式和$_POST$year$month$year1$month1为主,和$year$monthGenerateMonthlyReport()$year2$month2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM