错误更新数据库

时间:2017-08-09 15:56:16

标签: php mysql pdo

您好我正在尝试通过我的表单和邮递员提交更新请求,但我在响应中继续获得相同的功能

  

更新

我的数据库中根本没有任何变化。

这里我的功能

function update(Request $request, Response $response) {
    $id = $request->getAttribute('id');
    $name = $request->getParam('name'); 
    $start = $request->getParam('start');
    $end = $request->getParam('end');


    $sql = "UPDATE table_timing SET
                name  = :name,
                start  = :start,
                end  = :end

            WHERE table_timing.id = :id";

    try {
        $db = new db();
        $db = $db->connect();
        $stmt = $db->prepare($sql);

        $stmt->bindParam(":id", $id);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':start', $start);
        $stmt->bindParam(':end', $end);


        $stmt->execute();

        $db = null;

        $response->getBody()->write
            ('
            {
                "notice":
                {
                    "status":"200",
                    "message":"Updated"
                }
            }');

    } catch(PDOException $e) {
        $response->getBody()->write
        ('
        {
            "error":
            {
                "message":'. $e->getMessage() .'
            }
        }');
    }
};

这是邮递员正在返回的邮递员

  

{"错误":{"消息":SQLSTATE [23000]:完整性约束   违规行为:1048栏'名称'不能为空}}

直接在sql上运行正常

直接在sql上运行的查询是

UPDATE `table_timing` SET `name`='2016',`start`='2017-06-18 21:29:06',`end`='2017-06-18 21:29:06' WHERE `id`='1'

该功能是通过我的api

中的以下路线调用的

这是我如何传递它们

if (isset($_GET['id']) ) { 
    $id = (int) $_GET['id']; 
    if (isset($_POST['submit'])) { 

        $sName = '';
        $sStart = '';
        $sEnd = '';


        if(isset($_POST['sname'])){
            $sName = $_POST['sname'];
        }                       
        if(isset($_POST['sstart'])){
            $sName = $_POST['sstart'];
        }
        if(isset($_POST['send'])){
            $sName = $_POST['send'];
        }

            //SQL queries
            $raw = [ 
                    'name' => $sName,
                    'start' => $sStart,
                    'end' => $sEnd
            ];
            $data = http_build_query ($raw);

            $result = file_get_contents(
            BASE_URL . '/timing/update/'.$id, 
            false,                                  
            stream_context_create(array(
                PROTOCOL => array(
                    'method' => 'PUT',
                    'header' => array(
                        'Authorization: Bearer '.$token,
                        'Content-Length: ' . strlen($data),
                        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
                    ),                                   
                    'content' => $data
                )
            ))
        );

        $response = json_decode($result, false);
        echo $response->notice->message;
    }
}   

我尝试打印$ raw $ data $ id $ apikey他们都正确显示

1 个答案:

答案 0 :(得分:0)

我明白了,

我有请求的参数

$id = $request->getAttribute('id');
$name = $request->getParam('name'); 
$start = $request->getParam('start');
$end = $request->getParam('end');

与发送的不同

 $raw = [ 
       'name' => $sName,
       'start' => $sStart,
       'end' => $sEnd
        ];