POST - Slim PHP

Asked

Viewed 228 times

1

I’m a beginner in webservices and I’m studying on top of Slim (PHP) but I’m having a little problem saving something in the database. My job to try to save a new category in the bank is as follows:

function addCategoria(){
    global $app;
    $req = $app->request();
    $paramName = $req->params('name');

    $sql = "INSERT INTO category ('nameCategory') VALUES ($paramName);";
        try {
            $db = getDB();
            $stmt = $db->query($sql); 
            $stmt->bindParam("nameCategory",$paramName);
            $categorias = $stmt->fetchAll(PDO::FETCH_OBJ);
            $db = null;
            echo '{"categorias": ' . json_encode($categorias) . '}';
        } catch(PDOException $e) {
            echo '{"error":{"text":'. $e->getMessage() .'}}';
        }       
}   

I already tested and know that the category name is coming in Ws but when I run a test I get this:

[ 42,000 ]

And nothing is saved in the bank. So far I’ve managed to use the other methods but unfortunately I’m having work with PUT. If anyone can help, I’d appreciate it.

  • 42000 would sql state(error code normally) be returned? which database is using? vc does an Insert and then does a search( fetchAll()) pq?

  • The error returned is this: {"error":{"text":SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tests' in 'field list'}} Using Mysql. And with regard to fetchAll I ended up using for being in the tutorial I tried to use, as I had said I’m really inciante when it comes to Ws.

  • This error code has nothing to do with the question code, how strange. you are building a Ws and this method is in trouble? or you are consuming?

  • The error changed after I changed the quote type. Before it was 'nameCategory' and I changed to nameCategory. And the answer below solved the question.

1 answer

5


The query is incorrect. The correct structure would be:

$sql = "INSERT INTO category (nameCategory) VALUES ('$paramName');";
  • That’s what it really was (a really stupid mistake on my part). .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.