脚本添加不起作用

Introduction

I am trying to make a API. Reading from the db works fine... Writing to it however is failing.

The weird part is when i copy the query error_log print-out and paste it into PHPMyAdmin, It added the object correctly.

Code

index.php

...
 else if($tag == 'addIngredient'){
    $name = $_POST['name'];
    $cal = $_POST['cal'];
    $fat = $_POST['fat'];
    error_log("name=".$name." cal=".$cal."fat=".$fat);
    if(!$db->addIngredient($name,$cal,$fat)){
        $response["error"] = TRUE;
        $response["error_msg"] = "addIngredient failed";
    }

 }
...

BDFunctions.php

...
public function addIngredient($name, $cal, $fat){
    $query= "INSERT INTO `ingredient` (`name`, `cal`, `fat`) VALUES('".$name."', ".$cal.", ".$fat.");";
    error_log($query);
    $result = mysql_query($query);
    if($result){return true;}
    return false;
}
...

I get these print outs in my apache log:...

[Wed Apr 29 20:46:58 2015] [error] [client 192.168.1.9] name=testName cal=50fat=20 [Wed Apr 29 20:46:58 2015] [error] [client 192.168.1.9] INSERT INTO ingredient (name, cal, fat) VALUES('testName', 50, 20);

You should catch the mysql_error() someplace in there. I also added sprintf() to help prepare the statement better. Also I see no reference to your DB Link ($link, $con, etc)

public function addIngredient($name, $cal, $fat){
    $query= sprintf("INSERT INTO `ingredient` (`name`, `cal`, `fat`) VALUES ('%s', %d, %d);",
       $name,
       $cal,
       $fat
    );
    error_log($query);
    $result = mysql_query($query);
    if($result){return true;}
    error_log(mysql_errno($link) . ": " . mysql_error($link));
    return false;
}