PHP MySQLi 准备好的语句不工作

标签 php mysql mysqli

<分区>

EDIT START

所以我发现我的代码有问题。是下面这行

if(isset($_REQUEST['faq_submit'])) {

我不确定这条线有什么问题?为什么不能正常工作?

EDIT END

出于某种原因,我准备好的声明不起作用。此外,它似乎没有在错误日志中显示任何错误。现在我可能只是在那里做错了什么,但我相信这里有人能发现我做的错误哈哈……我尝试用几种不同的方式修复它,但没有成功,所以在这里寻求帮助总是一个不错的选择!

这是我添加问题的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();
}

最佳答案

您是否尝试检查是否抛出任何语句错误? http://www.php.net/manual/en/mysqli-stmt.error.php

关于PHP MySQLi 准备好的语句不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964878/

相关文章:

php - 如何在 yii2 中为 rollup 编写查询

mysql - 独特作者最近发布的 X 篇文章

php - 删除mysql上所有重复的行并保留一个与查询

php - 在 mysqli 函数中使用公共(public)静态函数

mysql - SQL Buddy 中的“是/否”条目使用什么数据库字段类型?

php - 准备好的 SQL LIKE 语句不返回所有结果

javascript - 检索新页面时jquery mobile与函数冲突

php - WMD 编辑器回显 wmd-preview div 的值以及关于将该值存储到数据库中的查询

php - PHP 缓存 vs 编译的问题

mysql - 如何从一个表中删除计数小于?