error updating record in database

旧巷老猫 提交于 2019-12-24 04:12:17

问题


Hello I am trying to submit a update request through my form and postman, but i keep getting the same thing in the response as it worked

updated

Nothing is changing in my db at all.

here my function

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() .'
            }
        }');
    }
};

here is what postman is what postman is returning

{ "error": { "message":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null } }

directly on sql its working fine

the query im running directly on sql is

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

the function is call through the below route in my api

here is how i a passing them

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

i tried printing $raw $data $id $apikey they all show correctly


回答1:


I figured it out,

i had the requested params

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

different than the ones being sent

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


来源:https://stackoverflow.com/questions/45595479/error-updating-record-in-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!