2012-08-01 166 views
-4

招呼球员的价值,我有这个问题我不能比较这两个字符串

<?php 
$dir = '/var/www/devData/test'; 

// create new directory with 777 permissions if it does not exist yet 
// owner will be the user/group the PHP script is run under 
if (!file_exists($dir)) { 
    mkdir ($dir, 0777); 
} 



     $flag = 0; 
     $stringData = $_POST['data']; // echo = null 
     $file = "/var/www/devData/test/ciao.txt"; 
     $fh = fopen($file, 'a+') or die("can't open file"); 

     while(!feof($fh)) { 
        $theData = fgets($fh, filesize($file)); 
        array_push($arr,$theData); 
        $i++; 
     } 
     for($j=0; $j<count($arr); $j++) 
     if($stringData == $arr[j]){ // is the problem 
      $flag = 1; 
     } 
     if($flag = 0){ 
      fwrite($fh, $stringData); // fwrite works perfectly even if i try to print $string the result is null 
     } 
     fclose($fh); 



?> 

有人可以给我解释一下如何解决这个问题呢?这个脚本的目的是避免用户编写两次同样的事情

+0

所以我们应该去猜测为什么'$ _POST ['data']'未定义?我的猜测:*你的表格有什么问题。 – phant0m 2012-08-01 10:17:13

+0

不是因为我可以将$ _POST的内容写入正常的文件 – Edivad 2012-08-01 10:20:14

+0

我看到这是用'ajax'标记的,您是否将数据作为文章传递?你正在做什么样的Ajax请求?你是否在使用像jQuery这样的库..这么多的问题! – Dale 2012-08-01 10:20:31

回答

3
 if($stringData == $arr[j]){ // is the problem 
          ^^^ 
         is missing a $ 

激活错误报告,这应该已经自动向你指出:

error_reporting(E_ALL); 
ini_set('display_errors', true); 
+0

deceze谢谢,但即使与$ j就像每次$标志= 1,所以每次两个字符串的比较是每次都是真实的。并且不可能 – Edivad 2012-08-01 10:41:25

+0

然后再进行一些更多的逐步调试。在每一步打印出不同的变量来找出它们的值以及为什么它们具有它们所做的值。 – deceze 2012-08-01 10:42:17

+0

alrady done奇怪的是,我打印的写入值,必须是1,但是0,所以我检查了文件,数据被正确存储。看起来像是在数据发送之前尝试打印值。真的我不知道是两天,我有这种问题 – Edivad 2012-08-01 10:44:59