php未定义索引在另一个if中

qzlgjiam  于 2021-06-20  发布在  Mysql
关注(0)|答案(4)|浏览(193)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
两年前关门了。
在这种情况下,我在第2行得到了关于未定义索引的错误:

if ($_POST['go'] == 'create') {

我知道我可以用它来解决 isset ,但我不知道如何在另一个if语句中执行它。
谢谢您

if (isset($_GET['createDB'])) {
  if ($_POST['go'] == 'create') {
     $handle = fopen($databaseSchema, "r");
     $createTable_events = fread($handle, filesize($databaseSchema));

     $user=$_POST['user']; 
     $pass=$_POST['pass']; 
     $clientHost= (isset($_POST['host']) ? $_POST['host'] : 'localhost');
     try {
        $dbh = null;
        $dbh = new PDO("mysql:host=$DB_HOST", $user, $pass);

        // create database
        $dbh->exec("CREATE DATABASE `".$DATABASE."`;");

        // Create waf-fle user
        $dbh->exec("CREATE USER '".$DB_USER."'@'".$clientHost."' IDENTIFIED BY '".$DB_PASS."';
           GRANT SELECT, INSERT, UPDATE, DELETE, CREATE TEMPORARY TABLES ON `".$DATABASE."`.* TO '".$DB_USER."'@'".$clientHost."';                        
           FLUSH PRIVILEGES;");
     } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
     }
2skhul33

2skhul331#

这应该管用

if (isset($_GET['createDB'])) {
    if (isset($_POST['go']) && $_POST['go'] == 'create') {
        $handle = fopen($databaseSchema, "r");
        $createTable_events = fread($handle, filesize($databaseSchema));

        $user=$_POST['user']; 
        $pass=$_POST['pass']; 
        $clientHost= (isset($_POST['host']) ? $_POST['host'] : 'localhost');
        try {
            $dbh = null;
            $dbh = new PDO("mysql:host=$DB_HOST", $user, $pass);

            // create database
            $dbh->exec("CREATE DATABASE `".$DATABASE."`;");

            // Create waf-fle user
            $dbh->exec("CREATE USER '".$DB_USER."'@'".$clientHost."' IDENTIFIED BY '".$DB_PASS."';
                    GRANT SELECT, INSERT, UPDATE, DELETE, CREATE TEMPORARY TABLES ON `".$DATABASE."`.* TO '".$DB_USER."'@'".$clientHost."';                        
                    FLUSH PRIVILEGES;");
        } catch (PDOException $e) {
            die("DB ERROR: ". $e->getMessage());
        }
pengsaosao

pengsaosao2#

use设置为“isset”,如下所示

if(isSet($_POST['data']))
{
    if(isSet($_POST['data2']))
   {
    //code here

    }

}
gg58donl

gg58donl3#

使用isset和!两个都清空。

if (isset($_GET['createDB'])) {
  if (isset($_POST['go']) && $_POST['go'] == 'create') {
     $handle = fopen($databaseSchema, "r");
     $createTable_events = fread($handle, filesize($databaseSchema));

     $user=$_POST['user']; 
     $pass=$_POST['pass']; 
     $clientHost= (isset($_POST['host']) ? $_POST['host'] : 'localhost');
     try {
        $dbh = null;
        $dbh = new PDO("mysql:host=$DB_HOST", $user, $pass);

        // create database
        $dbh->exec("CREATE DATABASE `".$DATABASE."`;");

        // Create waf-fle user
        $dbh->exec("CREATE USER '".$DB_USER."'@'".$clientHost."' IDENTIFIED BY '".$DB_PASS."';
           GRANT SELECT, INSERT, UPDATE, DELETE, CREATE TEMPORARY TABLES ON `".$DATABASE."`.* TO '".$DB_USER."'@'".$clientHost."';                        
           FLUSH PRIVILEGES;");
     } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
     }
wmtdaxz3

wmtdaxz34#

if (isset($_GET['createDB'])) {
  if ((isset($_POST['go'])) &&  ($_POST['go']  == 'create')) {
      // Your Code
 }
}

同样的isset也可以

相关问题