简体   繁体   English

无法使用 ajax 将 php 文件插入数据库

[英]Can't get php file to insert into database using ajax

I'm quite new to ajax and php.我对 ajax 和 php 很陌生。 I've been trying to insert the text found in my dynamically generated js table into the database, but I just can't get it to work.我一直在尝试将在动态生成的 js 表中找到的文本插入到数据库中,但我就是无法让它工作。

My main issue is that I have got it to work on a different table in the database, from a different page of the site.我的主要问题是我让它在数据库中的不同表上工作,从站点的不同页面。

<button id="saveScorecard">Save Scorecard</button>

The above triggers the below js event.上面触发了下面的js事件。

$(document).ready(function() {
console.log("document loaded");
    $('#saveScorecard').click(function() {
        //Get value of item name
        var place_1 = $("#scorecard table tbody #tr0 td:first-child").html();
        console.log($("#scorecard table tbody #tr0 td:first-child").html());
        var place_2 = $("#scorecard table tbody #tr1 td:first-child").html();
        var place_3 = $("#scorecard table tbody #tr2 td:first-child").html();;
        var user_id = 1;
        
        $('#saveScorecard').prop("disabled", true);

        request = $.ajax({
            url: "ajax/upload.php",
            type: "post",
            data: {place1 : place_1, 
                   place2 : place_2, 
                   place3 : place_3, 
                   userid : 1}
            
        });
        console.log("sent data to php file");

        request.always(function () {
            $('#saveScorecard').prop("disabled", false);
        });

    });
    
});

upload.php:上传.php:

<?php

require_once('../includes/db.php');


if($_POST['place_1_name'] && $_POST['place_2_name'] && $_POST['place_3_name']){


   $query = "INSERT INTO scorecards (place_1_name, place_2_name, place_3_name, user_id) 
   
   VALUES 
   (:place_1, :place_2, :place_3, :user_id)";
   
   $result = $DBH->prepare($query);
   $result->bindParam(':place_1', $_POST['place1']);
   $result->bindParam(':place_2', $_POST['place2']);
   $result->bindParam(':place_3', $_POST['place3']);
   $result->bindParam(':user_id', $_POST['userid']);

   $result->execute();
   echo $DBH->lastInsertId();
}

?> 

The site is hosted through plesk if that helps.如果有帮助,该站点将通过 plesk 托管。

Please ignore the clutter and indivdual definition of all of the table cells, I've been focused on trying to get it to work before tidying it up.请忽略所有表格单元格的混乱和单独定义,我一直专注于在整理之前尝试让它工作。

Also any tips on php debugging would be great, as sometimes the errors arent logged in the console or error logs.此外,有关 php 调试的任何提示都会很棒,因为有时错误不会记录在控制台或错误日志中。

Thanks for any help!谢谢你的帮助!

TYPOGRAPHICAL ERROR印刷错误

You are condition is checking the data which you have never send it.您的条件是检查您从未发送过的数据。

In your upload.php file you are checking the condition...在您的 upload.php 文件中,您正在检查条件...

if($_POST['place_1_name'] && $_POST['place_2_name'] && $_POST['place_3_name'])
{
    //your codes
}

but you are sending the data using ajax is...但是您使用 ajax 发送数据是...

data: { place1 : place_1, 
        place2 : place_2, 
        place3 : place_3, 
        userid : 1}

Either you have to change the key from JQuery or PHP to solve the problem.您必须从 JQuery 或 PHP 更改密钥才能解决问题。

Once you changed the keys to what you are sending, Your problem will be solved将密钥更改为要发送的内容后,您的问题将得到解决

for example:例如:

if($_POST['place1'] && $_POST['place2'] && $_POST['place3'])
{
    //your codes
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM