can't insert in database using web service

ajax, jquery, mysql, php, web-services

Solution

When you get to the success function that only means that the php script has run successfully / without any fatal errors.

You should add error handling to your database calls and add that information to the json object that your return from your script.

Then you can do a `console.log(data);` and see what happened exactly. You can of course also check what is returned by the script in the developer tools of the browser.

Enabling error display at the top of your script `ini_set('display_errors',1);` will probably invalidate your json (assuming there are warnings) but it will tell you exactly what went wrong, especially if you have PDO set up to throw exceptions.

Problem

I have a problem when I try to insert something to my database using my web service. I have this: index.php ``` <div class="modal-body"> <form id="wineForm"> <label>Id Selecao:</label> <input type="text" id="id_selecao" name="id_selecao"/> <label>Nome:</label> <input type="text" id="nome" name="nome"/> <label>Idade:</label> <input type="text" id="idade" name="idade"/> <label>Clube:</label> <input type="text" id="clube" name="clube"/> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button id="btnAdd" type="button" class="btn btn-primary">Add</button> </div> ... <script type="text/javascript"> var rootURLPessoas = "http://localhost/aw014/webservice/services/pessoas"; function addPessoa() { console.log('addPessoa aquiiiiiiii'); $.ajax({ type: 'POST', contentType: 'application/json', url: rootURLPessoas + '/add', dataType: "json", data: formToJSON(), success: function(data, textStatus, jqXHR){ alert('Person created successfully'); console.log("dsdkasdajhdjkas", data.id_selecao); //$('#wineId').val(data.id); }, error: function(jqXHR, textStatus, errorThrown){ alert('addWine error: ' + textStatus); } }); } function formToJSON() { return JSON.stringify({ "id_selecao": $('#id_selecao').val(), "nome": $('#nome').val(), "idade": $('#idade').val(), "clube": $('#clube').val(), "posicao": $('#posicao').val() }); } jQuery(document).ready(function ($) { $('#tabs').tab(); $('#btnAdd').click(function() { addPessoa(); }); }); </script> ``` service.php ``` function addPessoa(){ $request = \Slim\Slim::getInstance()->request(); $pessoa = json_decode($request->getBody()); $sql = "INSERT INTO pessoa (id_selecao, nome, idade,clube, posicao) values (:id_selecao, :nome,:idade,:clube,:posicao) "; $conn = getConn(); $stmt = $conn->prepare($sql); $stmt->bindParam("id_selecao",$pessoa->id_selecao); $stmt->bindParam("nome",$pessoa->nome); $stmt->bindParam("idade",$pessoa->idade); $stmt->bindParam("clube",$pessoa->clube); $stmt->bindParam("posicao",$pessoa->posicao); $stmt->execute(); $pessoa->id = $conn->lastInsertId(); $conn= null; echo json_encode($pessoa); } ``` So when I fill in all fields, and click in button Add, give me the alert `Person created successfully`. But when I check the database nothing was inserted, and don't know why.

Original source