表单到Ajax到PHP以插入SQL

表单到Ajax到PHP以插入SQL,php,sql,arrays,ajax,forms,Php,Sql,Arrays,Ajax,Forms,我已经非常努力地得到了一个简单的工作模型。我的实际站点更大,但我简化了脚本编写,使事情变得简单(并进行故障排除) 当我点击发送表单时,我不断收到“500”个错误,我一直无法找出我做错了什么。 (我已经建立了一个简单的数据库来捕获这一项) (PHP文件名为“sample2.PHP”,与html位于同一目录中。) 我的数据库的屏幕截图: 我的HTML文件: <html> <head> <meta charset="utf-8">

我已经非常努力地得到了一个简单的工作模型。我的实际站点更大,但我简化了脚本编写,使事情变得简单(并进行故障排除)

当我点击发送表单时,我不断收到“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>

样本1:
$(文档).ready(函数(){ $(“#sendit”)。在(“单击”,函数()上){ var字段值=[]; $('#maindiv input')。每个(函数(){ fieldvalue.push([this.id,$(this.val()]); }); console.log(fieldvalue); $.ajax({ url:“sample2.php”, 类型:“POST”, 数据类型:“json”, 数据:JSON.stringify(fieldvalue), 成功:函数(){ 警惕(“已工作”); } }); }); });
和我的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();
    }
?>

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

为什么你要一个接一个地做两个准备?而且
$query->bindValue
在你做
$query=$pdo->prepare($sql)之前就来了prepare($sql);”行之后放一行“$query->bindValue”?是的,但您还没有回答为什么有两个prepare使用相同的sql语句。您只需要一个。对于多个字段/值,我是将“isset”嵌套在if语句中,还是将它们与逗号放在同一个isset()中?或者我可以简单地传递一个数组吗?我将在实时版本中有12个字段。非常感谢你!我要试一试。您只需检查一个参数,但您可以将完整的值数组传递到我为ajax做了更新的
execute()。你可能想试试。如果您这样做了,那么在php上只需
print\r($\u POST)
以确保页面得到您期望的内容。这将显示在
控制台中。日志来自ajax成功。我已经能够得到一个警报!谢谢,好险啊!它确实说“空字符串”,但是。。。你能帮我把它传过来吗?图片:下面是我执行此操作时的SQL数据库,它是“空的”:您是否按照我的建议更改了ajax?你还添加了
print\r($\u POST)
$(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");
            }
        });
    });     
});