2013-02-18 109 views
0
echo json_encode(array(utf8_encode("success"))); 

該代碼返回null,我試圖調試項目,並希望它返回一個變量後,但它甚至不會用繩子json_encode返回null

繼承人的完整代碼工作:http://www.bludevelopment.com/php/getdata.txt

問題出在uploadinstalldata函數中。 該應用程序爲每個功能單獨調用它。

任何幫助都很讚賞!

function uploadInstallData(){ 

if (isset($_POST["Timestamp"]) && isset($_POST["PMINo"]) && isset($_POST["GPS"])){ 
//$result = array(utf8_encode('value', 'success')); 


if ($_POST['cmd'] == "uploaddata"){ 

$con = mysql_connect("localhost","bludevel_PMI","password1"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("bludevel_PMIForm", $con); 

$sql="INSERT INTO JobData (startTime, PMINo, Address, BeforePhoto, ExtraPhoto1,     ExtraPhoto2, ExtraPhoto3, AbletoInstall, InstallProblem, BBoxStatus, 
NewLocation, BBoxPhoto, Occupied, BasementFinished, BuildingType, ServiceSize,  ServiceType, HousepipeSize, HousepipeType, SSControlValve, NewMeter, 
NewMeterSize, NewTransmitter, MIULocation, MeterInstallType, MtrLocated, MtrDirection1, MtrSideof1, MtrDistance, MtrDirection2, MtrSideof2, AccessNotes, 
BldgWidth, BldgDepth, Review, StreetValve, HouseValve, AuthorizedWork, InspectorsName, NewStreetValve, AddPiping, Installedby, AfterPhoto, AfterPhoto2, 
CustomerIncentive, ConfirmSignal, InstallNotes, EndTime, GPS) 
    VALUES 
      ('".$_POST[Timestamp]."', 
      '".$_POST[PMINo]."', 
     '".$_POST[Address]."', 
      '".$_POST[BeforePhoto]."', 
      '".$_POST[ExtraPhoto1]."', 
      '".$_POST[ExtraPhoto2]."', 
     '".$_POST[ExtraPhoto3]."', 
     '".$_POST[AbletoInstall]."', 
     '".$_POST[InstallProblem]."', 
     '".$_POST[BBoxStatus]."', 
     '".$_POST[NewLocation]."', 
     '".$_POST[BBoxPhoto]."', 
     '".$_POST[Occupied]."', 
     '".$_POST[BasementFinished]."', 
     '".$_POST[BuildingType]."', 
     '".$_POST[ServiceSize]."', 
     '".$_POST[ServiceType]."', 
     '".$_POST[HousepipeSize]."', 
     '".$_POST[HousepipeType]."', 
     '".$_POST[SSControlValve]."', 
     '".$_POST[NewMeter]."', 
     '".$_POST[NewMeterSize]."', 
     '".$_POST[NewTransmitter]."', 
     '".$_POST[MIULocation]."', 
     '".$_POST[MeterInstallType]."', 
     '".$_POST[MtrLocated]."', 
     '".$_POST[MtrDirection1]."', 
     '".$_POST[MtrSideof1]."', 
     '".$_POST[MtrDistance]."', 
     '".$_POST[MtrDirection2]."', 
     '".$_POST[MtrSideof2]."', 
     '".$_POST[AccessNotes]."', 
     '".$_POST[BldgWidth]."', 
     '".$_POST[BldgDepth]."', 
     '".$_POST[Review]."', 
     '".$_POST[StreetValve]."', 
     '".$_POST[HouseValve]."', 
     '".$_POST[AuthorizedWork]."', 
     '".$_POST[InspectorsName]."', 
     '".$_POST[NewStreetValve]."', 
     '".$_POST[AddPiping]."', 
     '".$_POST[Installedby]."', 
     '".$_POST[AfterPhoto]."', 
     '".$_POST[AfterPhoto2]."', 
     '".$_POST[CustomerIncentive]."', 
     '".$_POST[ConfirmSignal]."', 
     '".$_POST[InstallNotes]."', 
     '".$_POST[EndTime]."', 
     '".$_POST[GPS]."')"; 


echo json_encode(array(utf8_encode("success"))); 
return true; 
$res = mysql_query($sql); 
if (!res) 
    { 
    die('Error: ' . mysql_error()); 
    } 

if (!$mysql->error) { 
} 

mysql_close($con); 
} 
}} 
+1

其實,這代碼返回'[ 「成功」]' – jeroen 2013-02-18 20:20:23

+0

'回聲json_encode(陣列(函數utf8_encode( 「成功」) ));'完美地工作 – vikingmaster 2013-02-18 20:20:26

+0

難道是因爲我有另一個類json_encode的實例,即使它在一個單獨的函數中,並且從來沒有調用過? – user1899201 2013-02-18 20:22:02

回答

3

正如我所提到的,你不應該使用ext/mysql擴展名爲新的代碼。直到它從互聯網上所有那些古老的PHP教程中清除出來,我們都不會看到人們走到「爲什麼我的代碼不再工作」的末尾,因爲使用它會在PHP 5.5中拋出E_DEPRECATED,並且它將被徹底刪除更高版本。

這使用mysqli擴展名和prepared queries(您也可以使用PDO)。

$db = new mysqli($dbip, $dbuser, $dbpass, $dbname); 

if ($query = $db->prepare("INSERT INTO table_name (x, y) values (?, ?)")) { 
    $query->bind_param("ss", $_POST["x"], $_POST["y"]); // s = string, i = int 
    $query->execute(); 
} else { 
    echo json_encode(array("error" => "malformed query")); 
    return false; 
    // in PHP, false or null both signify an error in a non-boolean context 
} 
echo json_encode(array("success" => "query attempted")); 
return true; 

當然,你不想盲目地認爲查詢是成功的,就像我在這裏(因此「嘗試」)。

您的JSON實際上似乎並沒有失敗,但最好是將數組中的值賦予一個關聯鍵。另外,utf8_encode數組給出null。看到這個:

json_encode(array("query attempted")); // ["success"] 
json_encode(array("success" => "more info")); // {"success":"more info"} 
json_encode(utf8_encode(array("success" => "more info"))); // null 

這樣做是更好的做法,可以幫助調試複雜的json,當它可以返回許多不同的東西。回答你的問題的「零」部分就是這個。

此外,ISO-8859-1(又名latin1)是Unicode的一個子集(as as ASCII)。它會乾淨地編碼爲JSON。儘管(例如,用戶輸入),您應該明確地使用JSON粘貼任何東西utf8_encode


我錯過了當我第一次回答另一個重要的事情(我不能相信我錯過了這一點):

. $_POST[NewLocation] . 

這是不妥當的PHP。它可能工作取決於如何嚴格配置PHP,但數組中的鍵被引用爲字符串,而不是作爲常量引用。這可以並將在錯誤日誌(Undefined T_CONSTANT NewLocation; 'NewLocation' assumed)中引發錯誤。

使用$_POST["NewLocation"](單或雙引號的工作,但要記住,他們在PHP中有着不同的含義)