0

I'm getting error about Undefined Index on line 2 in this case:

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

I know I can fix it with isset, but I don't know how to do it inside another if statement.

Thank you

 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());
     }
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
  • 2
    would `if (isset($_POST['go']) && $_POST['go'] == 'create') {` be an option? – Federico klez Culloca Jun 21 '18 at 10:44
  • Solutions should be added as an Answer, *not* as an edit to the question. Continuity of questions keeps the site useful for all visitors, as does the ability to vote on answers. I am rolling back the edit to remove the supposed "answer". – SherylHohman Jun 24 '18 at 19:46
  • @artmasterpl, it is recommended to add a check-mark to the answer which helped you best. You can also add, and accept, an answer yourself, if none of the proposed answers helped you. Please also upvote any that you found useful. These checkmarks and upvotes are what give SO volunteers incentive to continue sharing their knowledge, and help answer your questions. Thx, and Welcome to SO. Glad you found a solution. – SherylHohman Jun 24 '18 at 19:51

4 Answers4

0

use is set as 'isSet' like this

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

    }

}
0

This should work

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());
        }
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Paul
  • 263
  • 4
  • 21
0

Use isset and !empty both.

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());
     }
Kuldeep
  • 83
  • 1
  • 9
-1
if (isset($_GET['createDB'])) {
  if ((isset($_POST['go'])) &&  ($_POST['go']  == 'create')) {
      // Your Code
 }
}

Same isset will work

Roshni hegde
  • 423
  • 3
  • 14