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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教