通过ajax调用的PHP查询将无法执行

时间:2019-05-28 18:04:57

标签: php ajax

可以通过AJAX调用访问此文件,我认为我无法检查错误。 使用该代码并考虑到$article = 1$username位于数据库中,并且在mysql中手动测试查询后,查询将不会插入upvote表中。

有什么想法吗?

    //Includes
    require 'includes/dbInclude.php';
    require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/SMTP.php";
require_once "PHPMailer/Exception.php";

    //Username
    $username = $_GET['username'];
    $article = $_GET['articleId'];

    $userSql = "SELECT * FROM users WHERE username=?";
    $statement0 = mysqli_stmt_init($connection);
    if (!mysqli_stmt_prepare($statement0, $userSql)){
        mysqli_stmt_close($statement0);
        mysqli_close($connection);
        exit();
    }
    else{
        mysqli_stmt_bind_param($statement0, "s", $username);
        mysqli_stmt_execute($statement0);
        $result = mysqli_stmt_get_result($statement0);
        if($row = mysqli_fetch_assoc($result)){
            $userId = $row['id'];
            $articleSql = "SELECT * FROM upvotes WHERE idUsername=? AND idArticle=?";
            $statement = mysqli_stmt_init($connection);
            if (!mysqli_stmt_prepare($statement, $articleSql)){
                mysqli_stmt_close($statement);
                mysqli_close($connection);
                exit();
            }
            else{
                mysqli_stmt_bind_param($statement, "ii", $userId, $article);
                mysqli_stmt_execute($statement);
                $result = mysqli_stmt_get_result($statement);
                if($row = mysqli_fetch_assoc($result)){
                    if($row['upvotes'] >= 15){
                        mysqli_stmt_close($statement);
                        mysqli_close($connection);
                        exit(); 
                    }
                    else{
                        $upvoteSql = "UPDATE articles SET likes=likes+1 WHERE idArticle = ?";
                        $statement2 = mysqli_stmt_init($connection);
                        if (!mysqli_stmt_prepare($statement2, $upvoteSql)){
                            mysqli_stmt_close($statement2);
                            mysqli_close($connection);
                            exit();
                        }
                        else{
                            mysqli_stmt_bind_param($statement2, "i", $article);
                            mysqli_stmt_execute($statement2);
                        }   

                        $upvoteUserSql = "UPDATE upvotes SET upvotes=upvotes+1 WHERE idUsername=? AND idArticle=?";
                        $statement3 = mysqli_stmt_init($connection);
                        if (!mysqli_stmt_prepare($statement3, $upvoteUserSql)){
                            mysqli_stmt_close($statement3);
                            mysqli_close($connection);
                            exit();
                        }
                        else{
                            mysqli_stmt_bind_param($statement3, "ii", $userId, $article);
                            mysqli_stmt_execute($statement3);
                        }   
                    }
                }
                else{
                    $upvoteUserAddSql = "INSERT INTO upvotes(idUsername, idArticle, upvotes) VALUES(idUsername=?, idArticle=?, upvotes=?)";
                    $statement4 = mysqli_stmt_init($connection);
                    if (!mysqli_stmt_prepare($statement4, $upvoteUserAddSql)){
                        mysqli_stmt_close($statement4);
                        mysqli_close($connection);
                        exit();
                    }
                    else{
                        $upvote = 1;
                        mysqli_stmt_bind_param($statement4, "iii", $userId, $article, $upvote);
                        mysqli_stmt_execute($statement4);
                    }       
                }
            }
        }
    }

0 个答案:

没有答案