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?– rray
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.
– Rodrigo Oliveira
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?
– rray
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.– Rodrigo Oliveira