为什么我一直收到NULL返回?

时间:2018-03-19 15:38:42

标签: javascript php html mysql slim

我正在为一个大学项目创建一个预订系统,我正在尝试将一位作者添加到桌面作者'出于某种原因,当我添加一个字段时,它在我的数据库中返回NULL并在我的html页面上未定义?任何人都可以帮我这个我已经在下面显示我的HTML,Javascript和PHP代码。 任何人都可以帮助我,并指导我正确的方向。我觉得这与我的名字有关,例如。作者或作者 提前致谢。

HTML

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="w3.js"></script>
    <script src="https://scm.ulster.ac.uk/zhiwei.lin/js/jquery.tmpl.min.js"></script>
</head>
<body>

    <div id="authors">
        <ul id="authors_list"></ul>
    </div>

    <div class="mainArea">
        <label>Author:</label>
        <input type="text" id="author" name="name" required>


        <button id="btnSave">Save</button>
        <button id="btnUpdate">Update</button>
    </div>

    <p>Click Here to Delete Last Author
        <button id="btnDelete">Delete</button></p>
</body>
</html>

的js

$(document).ready(function(){

   $.ajax({
        type: 'GET',
        dataType: "json",
        url: "api.php/authors",
        success: showAllAuthors,
        error: showError
    }); 

});

function showAllAuthors(responseData) {
    $.each(responseData.authors,function(index,authors){
        $("#authors_list").append("<li type='square'> author:"+authors.author+"");
        $("#authors_list").append("</li>");
    });
}
function showError(){
    alert("Sorry, there was a problem!");
}

$(document).ready(function(){
    $("#btnSave").click(function(){


            $.ajax({
            type: 'POST',
            dataType: "json",
            url: "api.php/authors",
            data:{author: $("#author").val()},                
            data:JSON.stringify(authors),
            success: showResponse,
            error: showError
        });
    });
});

function authors(Author){
    this.author=Author;

}

function showResponse(responseData) {
    console.log(responseData);
}

function showError() {
    alert("Sorry, there was a problem!");
}

$(document).ready(function(){

   $.ajax({
        type: 'GET',
        dataType: "json",
        url: "api.php/authors/12",
        success: showResponse,
        error: showError
    }); 

});

$(document).ready(function(){
    $("#btnUpdate").click(function(){

        $.ajax({
            type: 'PUT',
            dataType: "json",
            url: "api.php/authors/12",
            data:{author: $("#author").val()},                
            data:JSON.stringify(authors),
            success: alert("Updated!")
        });
    });
});

$(document).ready(function(){
    $("#btnDelete").click(function(){
        $.ajax({
            type: 'DELETE',
            dataType: "json",
            url: "api.php/authors/13",
            data:{author: $("#author").val()},                
            data:JSON.stringify(authors),
            success: alert("Deleted!")
        });
    });
});

PHP

<?php

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
use Slim\Slim;
$app=new Slim();
$app->get('/authors','getAuthors');
$app->post('/authors','addAuthor');
$app->get('/authors/:id','getAuthor');
$app->put('/authors/:id','updateAuthor');
$app->delete('/authors/:id', 'deleteAuthor');
$app->run();

function deleteAuthor($id) {
    $sql = "DELETE FROM authors WHERE id=:id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $db = null;
        responseJson("Deleted",200);

    }catch(PDOException $e) {
        responseJson('{"error":{"text":'.$e->getMessage().'}}',500);
    }
}

function updateAuthor($id) {
    $request = Slim::getInstance()->request();
    $body = $request->getBody();
    $authors = json_decode($body);
    $sql = "UPDATE authors SET author=:author WHERE id=:id";
    try { 
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("author", $authors->author);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $db = null;
        responseJson("Updated",200);
    } catch(PDOException $e) { 
        responseJson('{"error":{"text":'.$e->getMessage().'}}',500);
    }
}

function getAuthor($id) {
    $sql = "SELECT * FROM authors WHERE id=:id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $authors = $stmt->fetchObject();
        $db = null;
        responseJson(json_encode($authors),200);
    } catch(PDOException $e) {
            responseJson('{"error":{"text":'.$e->getMessage().'}}',500);
    }
}

function getAuthors(){

    $sql = "select * FROM authors ORDER BY id";

    try {
        $db = getConnection();
        $stmt = $db->query($sql);
        $authors = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        responseJson('{"authors":'.json_encode($authors).'}',200);
    }catch(PDOException $e){
        responseJson('{"error":{"text":'.$e->getMessage().'}}',500);
    }            
}

function addAuthor(){

    $request = Slim::getInstance()->request();
    $authors=json_decode($request->getBody());
    $sql= "INSERT INTO authors (author) 
    VALUES (:author)";

    try {

        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("author", $authors->author);
        $stmt->execute();
        $authors->id=$db->lastInsertId();
        $db = null;
        responseJson(json_encode($authors),201);
    }catch(PDOException $e) {

        responseJson('{"error":{"text":'.$e->getMessage().'}}',500);
    }
}


function getConnection(){
    $dbhost="localhost";
    $dbuser="B00657229";
    $dbpass="9wz7Fr9J";
    $dbname="B00657229";
    $dbh= new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser,$dbpass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    return $dbh;    
}

function responseJson($responseBody,$statusCode){
    $app = Slim::getInstance();
    $response = $app->response();
    $response['Content-Type']='application/json';
    $response->status($statusCode);
    $response->body($responseBody);

}


?>

1 个答案:

答案 0 :(得分:-1)

你的js代码中似乎有一个错误:

$(document).ready(function(){
    $("#btnSave").click(function(){


        $.ajax({
        type: 'POST',
        dataType: "json",
        url: "api.php/authors",
        data:{author: $("#author").val()},              
        data:JSON.stringify(authors),
        success: showResponse,
        error: showError
    });
});

你设置数据两次,一次用 {author: $("#author").val()}JSON.stringify(authors)的其他时间我不认为你需要第二个,但我没有测试过。