PHP MySQLi 准备语句无法工作

3
我找到了代码问题所在,是以下这行代码:
if(isset($_REQUEST['faq_submit'])) {

我不确定这行代码有什么问题?为什么它不能正常工作呢?

编辑结束

由于某种原因,我的预处理语句(prepared statement)没有起作用。此外,错误日志中也没有显示任何错误信息。我可能在做一些错误的事情,但我相信这里肯定有人能够发现我做错了什么(笑)。我尝试以不同的方式进行修复,但都没有成功,所以向这里寻求帮助总是一个好的选择!

以下是我用于添加问题的 PHP 代码:

// Required Configuration
include_once('required.php');

// double check page was accessed via submit button
if(isset($_REQUEST['faq_submit'])) {
    // get data that sent from form
    $topic=trim($_REQUEST['faq_topic']);
    $detail=trim($_REQUEST['faq_detail']);
    $name=trim($_REQUEST['faq_name']);
    $email=trim($_REQUEST['faq_email']);

    // check if all forms are filled out
    if(!empty($topic) && !empty($detail) && !empty($name) && !empty($email)) {
        // Validate Email
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            // Prepared Insert Statement
            $stmt = $mysqli->prepare('INSERT INTO forum_question (`topic`, `detail`, `name`, `email`, `datetime`) VALUES (?, ?, ?, ?, ?)');

            // Bind Variables To Values
            $stmt->bind_param('sssss', $topic, $detail, $name, $email, $datetime);

            // Execute Prepared Statement
            $stmt->execute();

            // Print Results
            $html = '<div class="alert alert-dismissable alert-success"><button type="button" class="close" data-dismiss="alert">×</button>You <strong>successfully</strong> submited a question to the FAQ bored.</div>';
            print($html);

            // Close Connections and Statement
            $stmt->close(); // Statement
            $mysqli->close(); // MySQLi
        } else {
            // Email Validation Failed
            $html = '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Your <strong>email</strong> was invalid.</div>';
            print($html);
        }
    } else {
        // If the required items were not filled out print the following
        $html = '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert">×</button><strong>All</strong> forms are required.</div>';
        print($html);
    }
}

这里是需要的required.php文件,尽管我已经删除了一些登录信息。

$datetime=date("m/d/y h:i"); // Format Date And Time

// Connect
$mysqli = new mysqli('host', 'user', 'pass', 'db');

// Check Connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

2
启用错误处理。在页面顶部添加 ini_set('display_errors',1); error_reporting(E_ALL); - David Corbin
1
好的,我测试了你的代码并且成功了。请检查你选择的列类型、数据库、表等是否正确,你的表单元素是否命名正确且没有拼写错误。faq_emailFaq_Email不是同一个东西,提交按钮也没问题。此外,如果你使用了任何JS/Ajax,请也检查一下。@kmgilbert100 - Funk Forty Niner
@Fred-ii- 嘿,我把它搞定了,我不得不删除if(isset($_REQUEST['faq_submit'])) {语句,但我不知道那段代码有什么问题? - kevingilbert100
很可能是你的表单元素没有命名为 name="faq_submit",或者不是一个正确的提交按钮,或者你的表单正在使用 GET 方法,或者你需要将它改为 if(isset($_POST['faq_submit'])),没有看到你正在使用的表单,很难说。这可能是许多问题中的一种或混合体。最好发布完整的代码,这样就可以消除所有猜测;-)给我看看表格,我会立即告诉你。 - Funk Forty Niner
好吧,如果你不想或者不能发布你的表格,我就无法帮助你找出原因。 - Funk Forty Niner
显示剩余3条评论
1个回答

2

没有错误被显示。 - kevingilbert100
尝试将您的prepare()包装在if语句中以缩小范围。例如:$stmt = $mysqli->stmt_init(); if ($stmt->prepare($sql)) { ... } - philwc
请查看http://www.php.net/manual/en/mysqli-stmt.prepare.php上的示例#1。 - philwc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接