表单到Ajax到PHP以插入SQL

时间:2016-08-28 03:23:59

标签: php sql arrays ajax forms

我已经非常努力地想要一个简单的工作模型来运行。我的实际网站较大,但我已经愚弄了脚本以简化操作(并排除故障)。

我一直在" 500"当我点击发送表格时出错,而且我无法弄清楚我做错了什么。 (我已经设置了一个简单的数据库来捕获这一项)。

(PHP文件命名为" sample2.php"与html在同一目录中。)

我的数据库的屏幕截图:

database screenshot

我的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();
    }
?>

1 个答案:

答案 0 :(得分:0)

您的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']));
}

这是基本的。如果你可以让它工作,那么你只需要建立起来。如果您想解决任何无法预料的SQL错误,可能需要try / catch PDOException

对于测试目的,我很想不发送json,这样你可以更容易地从console.log()解决你的php:

$(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");
            }
        });
    });     
});