2

我很难追踪 $.ajax/PHP/MySQL 数据库插入错误的原因。

这是jQuery方面:

function SaveNewBranch(newBranchName, dataString) {
    console.log('In function SaveNewBranch(newBranchName, dataString). Value of
        newBranchName: ' + newBranchName + '. Value of dataString: ' + 
        dataString + '.');

    $.ajax({
        type: 'POST',
        url: '../scripts/branchAdmin.php',
        data: dataString,
        dataType: 'json',
        success: function(newBranchID) {

            console.log('from server: ' . newBranchID);

            // Clear the page
            ClearPageForNewSelection();

            PopulateBranchDropdown(newBranchID);
        },
        error: function(xhr, status, error) {
            console.log('datastring: ' + dataString);
            alert('An error occurred while attempting to save the branch to the 
                database. jqXHR object: ' + xhr + '. Status: ' + status + '. Error
                message: ' + error + '. An error log with more details has been
                created on the server. If the error persists, contact your site
                administrator.');
        }
    });
} // End Save New Branch

这是PHP代码:

if(isset($_POST['newBranchName']) &&
    isset($_POST['newBranchAddr1']) &&
    isset($_POST['newBranchAddr2']) &&
    isset($_POST['newBranchCity']) &&
    isset($_POST['newBranchState']) &&
    isset($_POST['newBranchZip']) &&
    isset($_POST['newBranchPhone']) &&
    isset($_POST['newBranchFax']) &&
    isset($_POST['newBranchUrl'])) {

    require_once('dbConnect.php');

    $log->lwrite('name: ' . mysqli_real_escape_string($dbc,$_POST['newBranchName']) .
        ', addr1: ' . mysqli_real_escape_string($dbc,$_POST['newBranchAddr1']) . ',
        addr2: ' .  mysqli_real_escape_string($dbc,$_POST['newBranchAddr2']) . ',
        city: ' . mysqli_real_escape_string($dbc,$_POST['newBranchCity']) . ',
        state: ' .$_POST['newBranchState'] . ', zip: ' .
        mysqli_real_escape_string($dbc,$_POST['newBranchZip']) . ', phone: ' .
        mysqli_real_escape_string($dbc,$_POST['newBranchPhone']) . ', fax: ' . 
        mysqli_real_escape_string($dbc,$_POST['newBranchFax']) . ', url: ' . 
        mysqli_real_escape_string($dbc,$_POST['newBranchUrl']));

    $_POST['newBranchState'] == '0' ? $newBranchState = '' : $newBranchState = 
        strtoupper(mysqli_real_escape_string($dbc,$_POST['newBranchState']));

    $queryInsertNewBranch = "INSERT INTO branches (name, address1, address2, city, 
        state, zipCode, phone, fax, url) VALUES ('" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchName']) . "', '" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchAddr1']) . "', '" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchAddr2']) . "', '" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchCity']) . "', '" .
        newBranchState . "', '" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchZip']) . 
        "', '" . mysqli_real_escape_string($dbc,$_POST['newBranchPhone']) . "', '" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchFax']) . "', '" . 
        mysqli_real_escape_string($dbc,$_POST['newBranchUrl']) . "')";

    $log->lwrite('new branch insert: ' . $queryInsertNewBranch);

    $resultInsertNewBranch = @mysqli_query($dbc, $queryInsertNewBranch);
    ...

我已经确认好的数据正在通过“dataString”的控制台输出传递给 php 脚本。

输出到控制台以进行测试插入:

newBranchName: A Test Branch, datastring: newBranchName=A Test branch&
    newBranchAddr1=123 StateSt.&newBranchAddr2=#123&
    newBranchCity=Anywhere&newBranchState=MN&newBranchZip=12343&
    newBranchPhone=555-555-1212&newBranchFax=555-555-2121&newBranchUrl=minnesota

我还通过检查 php 脚本中的第一行 $log->lwrite 来确认 php 脚本正在获取良好的数据。

输出到日志以进行相同的插入测试:

new branch insert: INSERT INTO branches (name, address1, address2, city, state,
    zipCode, phone, fax, url) VALUES ('A Test Branch', '123 State St.', '#123',
    'Anywhere', 'MN', '12343', '555-555-1212', '555-555-2121', 'minnesota')

最后一个 $log->lwrite 条目(新分支插入)永远不会被写入日志文件。

同样在 jQuery 端,错误函数被触发,显示警告框。这三个对象的值是:

jqXHR object: [object Object]
Status: parsererror
Message: Unexpected token e

无论我在获取数据的表单中输入什么内容,我总是得到相同的结果:INSERT 永远不会运行,并且错误函数中的警告框会显示 - 每次都显示相同的消息。

我一遍又一遍地查看代码的每一个细节,寻找错误的空格或单引号应该在双引号的位置,等等。我一辈子都找不到问题所在!

如果 jqXHR 对象可能有线索,我不知道如何获取其中的元素。你是怎样做的?

我(和我的客户)将非常感谢我在解决这个问题时能得到的任何帮助!

谢谢你的帮助...

4

1 回答 1

0

我真的不知道为什么,但这就是导致问题的原因(也许有人可以告诉我为什么这会破坏插入):

将代码添加到我的问题时,我删除了一条我认为与问题无关的行。

这条线是这样的:

echo 'new branch state: ' . $newBranchState;

它是在三元运算符行设置 $newBranchState 变量(确实有效)之后立即出现的。

评论 echo line out 解决了这个问题。

现在,有人可以告诉我为什么这会导致脚本的其余部分中断并给我那个错误吗?我现在看到“意外的标记 e”来自哪里,但是在该回声之前的行末尾有一个分号,所以我不明白为什么这会导致问题......

于 2013-02-28T16:11:59.647 回答