形成了Ajax给PHP插入SQL(Form to Ajax to PHP for Insert in

2019-09-29 03:39发布

我曾尝试很难获得这种简单的工作模式来运作。 我的实际站点较大,但我已经简单化了脚本,使事情变得简单(和故障排除)。

我不断收到“500”错误,当我点击发送通过表单,我一直无法弄清楚什么我已经做错了。 (我已经建立了一个简单的数据库来捕捉仅这一个项目)。

(PHP的文件名为“sample2.php”为html是在同一个目录中。)

我的数据库的截图:

我的HTML文件:

<html>
    <head>
        <meta charset="utf-8">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    </head>
    <body>
        <div name="maindiv" id="maindiv">
            <span>sample1:</span> <input id="sample1" name="sample1" width="300px" type="text" value="sample1text" /><br />
        </div>

        <input type="button" name="sendit" value="Do it" id="sendit"/> 

        <script language="javascript" type="text/javascript">
        $(document).ready(function(){

            $("#sendit").on("click", function() {
                var fieldvalue = [];

                $('#maindiv input').each(function() { 
                    fieldvalue.push([this.id, $(this).val()]); 
                });

                console.log(fieldvalue);

                $.ajax({
                    url: "sample2.php",
                    type: "POST",
                    dataType: "json",
                    data: JSON.stringify(fieldvalue),
                    success: function() {
                        alert("worked");
                    }
                });
            });     
        });
        </script>
    </body>
</html>

我的PHP文件:

<?
    $pdo = new PDO("mysql:dbname=trialdb;host=extoleducation.ipagemysql.com","username","password");

    $id = $_POST['sample1'];

    $query->bindValue(':sample1', $sample1, PDO::PARAM_STR);
    $sql = "INSERT INTO sampletable (sampleline) VALUES (:sample1);";

    $query = $pdo->prepare($sql);

    if($statment = $pdo->prepare($sql)) {
        $statment->execute();
        $statment->closeCursor();
        exit();
    }
?>

Answer 1:

你的PHP似乎混淆。 为简单起见,尽量只这样做:

<?php
$pdo = new PDO("mysql:host=extoleducation.ipagemysql.com;dbname=trialdb","username","password");

if(isset($_POST['sample1'])) {
    $sql = "INSERT INTO `sampletable` (`sampleline`) VALUES (:sample1)";
    $query = $pdo->prepare($sql);
    # I find binding values much easier just doing the array into the execute
    # If you get it working like this and really want to go back and try
    # bindValue(), you can
    $query->execute(array(':sample1'=>$_POST['sample1']));
}

这是因为基本的,因为它得到。 如果你能得到这个工作,那么你只是一种建立过它。 你可能想try / catch PDOException ■如果要排除任何不可预见的SQL错误。

为了测试pursposes,我会受到诱惑,不发送JSON,这样你可以更轻松地解决从你的PHP console.log()

$(document).ready(function(){
    $("#sendit").on("click", function() {
        // If you are not serializing, I would do an object, not array
        var fieldvalue = {"action":"submit"};
        // Loop through and save names and values
        $('#maindiv input').each(function(k,v) { 
            fieldvalue[$(v).attr('name')] = $(v).val();
        });

        $.ajax({
            url: "sample2.php",
            type: "POST",
            // Try just sending object here instead of json string
            data: fieldvalue,
            // On the success, add the response so you can see
            // what you get back from the page
            success: function(response) {
                // Do a check to see if you get any errors back
                console.log(response);
                // This has minimal value because it is only telling you
                // that the ajax worked. It's not telling you anything from the
                // response of the page
                alert("worked");
            }
        });
    });     
});


文章来源: Form to Ajax to PHP for Insert into SQL