0
echo json_encode(array(utf8_encode("success")));

that code is returning null, I am trying to debug a project and would like it to return a post variable, but it won't even work with a string

heres the full code:http://www.bludevelopment.com/php/getdata.txt

The problem is in the uploadinstalldata function. The app calls it separately for each function.

Any help is much Appreciated!

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);
}
}}
Amelia
  • 2,967
  • 2
  • 24
  • 39
user1899201
  • 123
  • 1
  • 1
  • 8
  • 1
    Actually, that codes returns `["success"]` – jeroen Feb 18 '13 at 20:20
  • `echo json_encode(array(utf8_encode("success")));` works perfectly – sybear Feb 18 '13 at 20:20
  • Could it be because i have another instance of json_encode in the class even though it's in a separate function and never called? – user1899201 Feb 18 '13 at 20:22
  • Can you reproduce the issue with a PHP file that contains **only** `echo json_encode(array(utf8_encode("success")));`? – Tchoupi Feb 18 '13 at 20:22
  • You should try to locate where the problem occurs and post that code here with a clear description of what you expect it to do and what it actually does. – jeroen Feb 18 '13 at 20:24
  • It works fine with just that – user1899201 Feb 18 '13 at 20:26
  • It's suppose to insert data into a mysql database, it's not working so i'm tryng to debug it by echoing a json string – user1899201 Feb 18 '13 at 20:35
  • You shouldn't be using deprecated extensions (ext/mysql) in new code. Try MySQLi/PDO – Amelia Feb 18 '13 at 20:41
  • 1
    Also, holy bananas that code is rife with possible exploits. – Amelia Feb 18 '13 at 20:41
  • tried that, but it doesn't work with mysqli – user1899201 Feb 18 '13 at 20:41
  • yes, i'm aware that it's not secure, it's for testing purposes – user1899201 Feb 18 '13 at 20:42
  • The fact that you are using json suggests that you are doing this with ajax. You probably have errors / warnings so you should look in the net tab of the developer tools to see what the script returns. Or alternatively develop using a normal post at first and add ajax later. – jeroen Feb 18 '13 at 20:42
  • @user1899201 This works with mysqli, and a scenario like this is what it is most commonly used for. What are you talking about? Also, **never**, ***ever***, think that development = no security. It will bite you badly later on when you have to secure something that could have been secure from the moment you coded it. – Amelia Feb 18 '13 at 20:45
  • Im using objective-c, and i'm positive it's completely error free – user1899201 Feb 18 '13 at 20:45
  • I suppose i will try mysqli, and figure out why it isn't working – user1899201 Feb 18 '13 at 20:49
  • I can give you an example. I'll be simplifying your query in my answer, though. – Amelia Feb 18 '13 at 20:49
  • ok, thanks, I wouldn't excpect you to write the whole thing. – user1899201 Feb 18 '13 at 20:50

1 Answers1

3

As I mentioned, you should not be using the ext/mysql extension for new code. Until it is purged from all those ancient PHP tutorials on the internet, we wont see the end of people going "Why doesn't my code work any more", because using it throws E_DEPRECATED in PHP 5.5, and it will be removed completely in later versions.

This uses the mysqli extension, with prepared queries (you can also use 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;

Of course, you don't want to blindly assume that a query was successful like I do here (hence "attempted").

Your JSON doesn't actually seem to be failing, but it is probably best to give the values in the array an associative key. Also, utf8_encodeing an array gives null. See this:

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

Doing this is better practice and can help debug complex json when it can return many different things. The "null" part that answers your question was pretty much this.

Also, ISO-8859-1 (aka latin1) is a subset of Unicode (as is ASCII); it will encode cleanly to JSON. You should utf8_encode anything that you stick in JSON explicitly, though (such as user input).


Another vital thing I missed when I first answered (I can't believe I missed this):

. $_POST[NewLocation] .

This isn't proper PHP. It might work depending on how strictly PHP is configured, but keys in arrays are quoted as strings, not unquoted as constants. This can and will throw an error in the error log (Undefined T_CONSTANT NewLocation; 'NewLocation' assumed).

Use $_POST["NewLocation"] (single or double quotes work, but remember they have different meanings in PHP)

Community
  • 1
  • 1
Amelia
  • 2,967
  • 2
  • 24
  • 39