PHP INSERT准备语句不插入ajax

时间:2016-04-07 03:18:03

标签: javascript php jquery ajax pdo

我正在尝试使用ajax创建INSERT语句,并在预准备语句表单中创建查询。我之前从未使用过带有PDO的AJAX,所以请原谅任何无知。

这种方式,我得到alert(data);错误,但是警告弹出窗口只是说"错误| &#34 ;.这是指javascript不正确还是php文件?我相信它是javascript,因为我甚至没有将php文件显示在我的控制台网络选项卡中。

AJAX内有什么问题?

<form method="POST" id="pdo-add">
    <input name="first" id="pdo-add-first" placeholder="First Name">
    <input name="last" id="pdo-add-last" placeholder="Last Name">
    <input name="product" id="pdo-add-product" placeholder="Product">
    <input name="add" type="submit" value="Add">
</form>

AJAX

$(function() {

   $("#pdo-add").on("submit", function (event) {
    event.preventDefault();

    var add_first = $("#pdo-add-first").val();
    var add_last = $("#pdo-add-last").val();
    var add_product = $("#pdo-add-product").val();

    $.ajax({ 
        url: "pdoAddSend.php", 
        type: "POST",
        data: {
            "add_first": add_first,
            "add_last": add_last,
            "add_product": add_product
        },
        success: function (data) {
        //  console.log(data); // data object will return the response when status code is 200
            if (data == "Error!") {
                alert("Unable to insert product record!");
                alert(data);
            } else {
                //$("#newsletter-form")[0].reset();
                $('.announcement_success').html('Product Successfully Added!');
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus + " | " + errorThrown);
            //console.log("error"); //otherwise error if status code is other than 200.
        }
    });
});
});

PHP

ini_set('display_errors', 1);
error_reporting(E_ALL);

$add_first = $_POST['add_first'];
$add_last = $_POST['add_last'];
$add_product = $_POST['add_product'];
try {
    $host = 'localhost';
    $name = '';
    $user = '';
    $password = '';

    $dbc = new PDO("mysql:host=$host;dbname=$name", $user, $password);

}catch(PDOException $e) {
    echo $e->getMessage();

}

//if(isset($_POST['add'])) {
if(isset($add_first && $add_last && $add_product) {

    $stmt = $dbc->prepare("INSERT INTO users (first, last, product) VALUES (:first,:last,:product)");

    $stmt->bindParam(':first', $add_first);
    $stmt->bindParam(':last', $add_last);
    $stmt->bindParam(':product', $add_product);

    $stmt->execute();
}

2 个答案:

答案 0 :(得分:1)

  • 使用if (!empty($add_first) && !empty($add_last) && !empty($add_product)) {检查空值
  • 使用数据库中的dataType json to return数组
  • 在ajax
  • 成功动态插入输入

JS

$.ajax({
    url: "pdoAddSend.php",
    type: "POST",
    data: {
        "add_first": add_first,
        "add_last": add_last,
        "add_product": add_product
    },
    dataType: "json",
    success: function(data) {

        for (var i = 0; i < data.length; i++) {
            var tr = $('<tr/>');
            tr.append("<td><input name='id' value=" + data[i].id + " readonly=''></td><td><input name='first' value=" + data[i].first + "></td><td><input name='last' value=" + data[i].last + "></td><td><input name='product' value=" + data[i].product + "></td><td><input name='save' type='submit' value='Save'></td><td><input name='delete' type='submit' value='Delete'></td>");
            $("#tableid").append(tr);
        }
        console.log(data); // data object will return the response when status code is 200
        if (data == "Error!") {
            alert("Unable to insert product record!");
            alert(data);
        } else {
            //$("#newsletter-form")[0].reset();
            $('.announcement_success').html('Product Successfully Added!');
        }
    },
    error: function(xhr, textStatus, errorThrown) {
        alert(textStatus + " | " + errorThrown);
        //console.log("error"); //otherwise error if status code is other than 200.
    }
});

答案 1 :(得分:0)

您最好先测试这些变量,这样就没有&#34;未定义的索引&#34;发生了通知。

因为你没有名为add的POST变量,if(isset($ _ POST [&#39; add&#39;]))总是假的。

代码在这里:

if(isset($_POST['add_product']) && isset($_POST['add_last']) && isset($_POST['add_product'])) {
    $add_first = $_POST['add_first'];
    $add_last = $_POST['add_last'];
    $add_product = $_POST['add_product'];
//then your db execute code here
}