简体   繁体   English

SQL查询似乎不起作用

[英]SQL query doesn't seem to be working

I am making a PHP script at the moment and I am making an install page to go with it. 目前,我正在制作一个PHP脚本,并且正在制作一个安装页面。 Before running the install script the user has to edit the config file and enter their database credentials (they have to create the database first). 在运行安装脚本之前,用户必须编辑配置文件并输入其数据库凭证(他们必须首先创建数据库)。

The install script starts by checking they have met the requirements. 安装脚本首先检查它们是否满足要求。 If they have then display the "Import Tables" button. 如果已显示,则显示“导入表”按钮。 This is where I am having problems. 这就是我遇到的问题。 Here is my code: 这是我的代码:

<?php 

if (isset($_POST['step2'])) {
    if (isset($success_php, $success_mysqli, $step_1_complete)) {
        // Import SQL tables

        $sql = ("
            CREATE TABLE `banned_ips` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `ip` varchar(15) NOT NULL,
            `reason` text NOT NULL,
            PRIMARY KEY (`id`),
            UNIQUE KEY `ip` (`ip`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
            ");

        if ($db->execute($sql)) {
            $success_import = true;
            $step_2_complete = true;
        }

        $db->close();
    }
}

}

?>

I was thinking of loading it straight from the .sql file however I am trying to make the script as compatible as I can across different servers, so therefore, I chose to do the query like so. 我当时想直接从.sql文件中加载它,但是我试图使该脚本在不同服务器之间尽可能兼容,因此,我选择像这样进行查询。

What I am asking is whether there is an issue with my above query and if so, how can I resolve it. 我要问的是上面的查询是否存在问题,如果存在,我该如何解决。 At the moment when I click the button to test it, the table and its columns are not created. 当我单击按钮进行测试时,尚未创建表及其列。

(To close the question and marked as solved) (关闭问题并标记为已解决)

Your table creation code checks out. 您的表创建代码签出。 As I said in my commment(s) your code is dependant in regards to the conditional statements you've set. 正如我在评论中所说,您的代码取决于您设置的条件语句。

If you're using this with a form ( which seems obvious ), check that your variables are properly set/named. 如果您将它与某种形式一起使用( 看起来很明显 ),请检查变量是否已正确设置/命名。

I am questioning this also isset($success_php, $success_mysqli, $step_1_complete) so double-check everything. 我在质疑这也是isset($success_php, $success_mysqli, $step_1_complete)因此请仔细检查所有内容。

"@Fred-ii- Thanks, the error was because two of the variables weren't set before it tried to execute." “ @ Fred-ii-谢谢,错误是因为在尝试执行之前未设置两个变量。”

Add error reporting to the top of your file(s) when in production. 在生产中将错误报告添加到文件顶部。

error_reporting(E_ALL);
ini_set('display_errors', 1); 

Plus, I quote John Conde: 另外,我引用John Conde:

"You don't need a unique key if the column is the primary key. They're already unique." “如果列是主键,则不需要唯一键。它们已经是唯一的。”

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

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