ds34222222 2015-04-29 18:55
浏览 17
已采纳

脚本添加不起作用

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);

  • 写回答

1条回答 默认 最新

  • doubi1818 2015-04-29 23:30
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂