2014-02-15 547 views
-1

更新:所有值現在都返回「A」時,他們應該都是不同的值。 我想要做的是,通過郵寄請求發送一個值表到我的網站,該網站將表格數據保存到一個文本文件。我終於有了它,所以整個表打印到文本文件,但所有的值是A.我做錯了什麼?順便說一下,我嘗試了很多方法和許多其他「類似問題」,但都沒有爲我工作。提前致謝。PHP json_encode()返回空值

CODE:

<?php 
$foo = file_get_contents("php://input"); 
$stuff = json_decode($foo, true); 

function createtxt($cttext, $location,$stuff) 
{ 
    $ccontent = $cttext; 
    if (file_exists($location)) { 
     echo "The file $ffilename exists"; 
    } else { 
     $fp = fopen($location, "wb"); 
     fclose($fp); 
       $formdata = array(
       'user'=> $cttext['user'], 
       'secretkey'=> $cttext['secretkey'], 
       'isanadmin'=> $cttext['isa'], 
       'firsttime'=> $cttext['firsttime'], 
       'display'=> $cttext['dis'], 
       'test'=> $cttext['test'], 
       'test2'=> $cttext['test2'], 
       'type'=> $cttext['type'] 
       ); 
       $jsondata = json_encode($formdata); 
       file_put_contents($location, $jsondata); 
     echo 'Created User Text File Named "data.txt" '; 
    } 
} 

function lookforuser($NAME,$stuff) 
{ 
    if (!file_exists('users/' . $NAME)) 
     mkdir('users/' . $NAME); 
    echo 'User Folder Created '; 
    createtxt($NAME, 'users/' . $NAME . '/data.txt',$stuff); 
} 

lookforuser($stuff['user'],$stuff); 
?> 

輸出文本文件:

{"user":"A","secretkey":"A","isanadmin":"A","firsttime":"A","display":"A","test":"A","test2":"A","type":"A"} 

回答

0

您傳遞$cttext,但獲得$stuff

由於$stuff未定義,因此數組解引用也爲null。

您可能想要通過$stuff作爲第三個參數。

+0

解決! 現在輸出爲: {「user」:「ASCORE」,「secretkey」:「secretkeytheyentered」,「isanadmin」:「0」,「firsttime」:「1」,「display」:「0」 test「:」Works!「,」test2「:」Works!「,」type「:」0「} 更新所有$ cttext []爲$ stuff [] n更新腳本 – ASCORE

0

$stuff是從來沒有自己的函數中定義。這是一個全局變量,但是你不會將它傳遞給你的函數;該函數僅知道其參數,內部初始化的變量以及通過global明確引用的變量。但是,由於您撥打lookforuser然後createtxt,您確實將內容$stuff更改爲createtxt

所以,最好的辦法解決這一問題:$cttext每次在功能createtxt使用$stuff及時更換$stuff。由於您編寫代碼的方式,您已經將$stuff的值傳遞給createtxt,所以這將起作用。

全碼:

function createtxt($cttext, $location) 
{ 
    $ccontent = $cttext; 
    if (file_exists($location)) { 
     echo "The file $ffilename exists"; 
    } else { 
     $fp = fopen($location, "wb"); 
     fclose($fp); 
       $formdata = array(
       'user'=> $cttext['user'], 
       'secretkey'=> $cttext['secretkey'], 
       'isanadmin'=> $cttext['isa'], 
       'firsttime'=> $cttext['firsttime'], 
       'display'=> $cttext['dis'], 
       'test'=> $cttext['test'], 
       'test2'=> $cttext['test2'], 
       'type'=> $cttext['type'] 
       ); 
       $jsondata = json_encode($formdata); 
       file_put_contents($location, $jsondata); 
     echo 'Created User Text File Named "data.txt" '; 
    } 
} 

function lookforuser($NAME) 
{ 
    if (!file_exists('users/' . $NAME)) 
     mkdir('users/' . $NAME); 
    echo 'User Folder Created '; 
    createtxt($NAME, 'users/' . $NAME . '/data.txt'); 
} 

lookforuser($stuff['user']); 
?> 
+0

$ stuff是全局的,但它作爲arg傳遞給'lookforuser()',它將它傳遞給另外兩個函數。當他們通過調用鏈獲得變量名時,我會被OP的完全混淆。 $ NAME - > $ cttext和whatnot。 –

+0

@MarcB不,它傳遞'$ stuff'的*值*,而不是變量本身。 '$ stuff'在'lookforuser'或'createtxt'中是未定義的,但$ stuff中的值可以分別通過$ NAME和$ cttext來訪問。 –

+0

'lookforuser($ stuff ['name'],$ stuff)'。看起來像它傳遞給我...... –