PHP写入mysql不起作用虽然Firebug报告回连接OK一切似乎都没问题

时间:2014-08-06 13:57:51

标签: php mysql

这是Ajax调用处理程序,并且在连接,准备,绑定和执行时进行了检查,它们似乎都没问题(从本网站的其他地方检查),但没有添加任何记录。 Firebug刚回来时连接OK,所有检查后Everything似乎都没问题。我是这些东西的新手,完全不知所措。任何帮助感激不尽。提前谢谢。

<?php
ini_set('display_errors', 'On');
 error_reporting(E_ALL | E_STRICT);

if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
    && ($_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest')) {

    require_once '../../htconfig/dbconf.php';

    $sql =  new mysqli($db['hostname'],$db['username'],$db['password'],$db['database']);
    if (!$sql -> connect_errno) {
        echo 'Connected OK';
    }
    else {
        echo 'Somethings wrong';
    exit;
    };

    if (isset($_POST['f4'])) {
        $f1 =   $_POST['f1'];
        $f2 =   $_POST['f2'];
        $f3 =   $_POST['f3'];
        $f4 =   $_POST['f4'];
        $f5 =   $_POST['f5'];
        $f6 =   $_POST['f6'];
        $f7 =    $_POST['f7'];
        $f8 =  $_POST['f8'];
        $f9 =    $_POST['f9'];

        $stmt = $sql->prepare("INSERT INTO blatto (f1, f2, f3, f4, f5, f6, f7, f8, f9) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
        if ( false===$stmt ) {
          // and since all the following operations need a valid/ready statement object
          // it doesn't make sense to go on
          // you might want to use a more sophisticated mechanism than die()
          // but's it's only an example
          echo 'Prepare statemnt failure';
          die('\n prepare() failed: ' . htmlspecialchars($mysqli->error));

        $rc = $stmt->bind_param('ssssddiis', $f1, $f2, $f3, $f4, $f5, $f6, $f7, $f8, $f9 );
        // bind_param() can fail because the number of parameter doesn't match the placeholders in the statement
        // or there's a type conflict(?), or ....
        if ( false===$rc ) {
          // again execute() is useless if you can't bind the parameters. Bail out somehow.
          echo 'Param bind failure';
          die('bind_param() failed: ' . htmlspecialchars($stmt->error));
        }

        $rc = $stmt->execute();
        // execute() can fail for various reasons. And may it be as stupid as someone tripping over the network cable
        // 2006 "server gone away" is always an option
        if ( false===$rc ) {
          echo 'Execute failure';
          die('execute() failed: ' . htmlspecialchars($stmt->error));
        }

        $stmt->close();
        }
        else {
            echo ' Everything seems OK';
        }
    };
}
?>

我在Ajax调用中添加了以下内容(也是从此站点中添加了内容),并返回状态为200且状态为“OK”的成功消息。仍然没有写入数据库。

error: function(jqXHR, textStatus, errorThrown) {
   alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');

    $('#result').html('<p>status code: '+jqXHR.status+'</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
    console.log('jqXHR:');
    console.log(jqXHR);
    console.log('textStatus:');
    console.log(textStatus);
    console.log('errorThrown:');
    console.log(errorThrown);

    },

/**
 * A function to be called if the request succeeds.
 */
success: function(data, textStatus, jqXHR) {
   $('#result').html(data);
    alert('Load was performed. Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information! ');
    console.log('jqXHR:');
    console.log(jqXHR);
    console.log('textStatus:');
    console.log(textStatus);
    console.log('data:');
    console.log(data);

}

我创建了一个新页面,其中只包含一个按钮和带有固定数据的Ajax调用。它也报告成功但没有添加记录。我复制了SQL语句并将其粘贴到具有相同固定值的PHPMyadmin SQL框中并运行它并且一切都很好,添加了一条记录。自杀是唯一的选择吗? 我知道Ajax调用使它成为php脚本,因为它会发送回不同点的回显字符串。 f4检查是一个红色的鲱鱼,因为只有在设置并且格式正确的情况下才会进行Ajax调用,并且我已经获取了客户端jscript,将其放在一个裸页面中并为每个字段提供正确的数据并使其成为Ajax调用。它的行为方式完全相同,并返回相同的检查字符串,但仍然不写入数据库。创建了$ sql对象,因此db连接很好。准备,绑定和执行不会产生任何错误。我还添加了另一个回声,上面没有显示,它表明php脚本一直运行到最后而不会遇到错误。就像我说的那样,我对这些东西不熟悉,当我问及5xx和4xx错误是否可以发生并且不被php脚本中的任何检查捕获时,请原谅我的无知?我能看到的唯一4xx错误是如果包含dbconf.php的路径错误。我将确保路径错误并重新测试以查看错误是否被拾取。我不知道如何生成服务器端5xx错误。 谢谢大家的建议。

好的,我将include_once行中的文件名更改为假名,并生成了一个未找到致命错误的文件,并在此时退出。因此处理4xx错误,或至少处理404。

0 个答案:

没有答案