2012-08-01 160 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