-1

I'm simply trying to check if a database with the same name exists (if yes, throw an error) else create it and display it has been created successfully. But after entering the database name, no database is getting created. What could I be missing ? (Is db.php even getting called ?? I think there should be some way for index.html and db.php to communicate , right ?)

This is my index.html

<form method="post" action="">
    Enter database name: <input type="text" name="db" /><br/>
    <input type="submit" name="submit" value="submit"/>
</form>

And my db.php:

<?php
    if(isset($_POST['submit'])){  //check for the submit button pressed
    $dbname=$_POST['db'];     //store the database name in php variable
    $query= mysqli_connect('localhost','root','')or die("Error establishing connection");      //check for database connectivity
    $a="CREATE DATABASE IF NOT EXISTS ".$dbname;    //create a database if it doesn't already exist
    $q= mysqli_query($query, $a) or die("Database already exist.. please try   different name");
    echo "Your database ".$dbname." is successfully created";
    }
?>
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
vinita
  • 595
  • 1
  • 9
  • 24

1 Answers1

1

You are not using action in your <form> here:

<form method="post" action="">

You must need to use action to call db.php as:

<form method="post" action="db.php">

here, i am assuming, db.php and index.html both of them located at same directory.

Side Note:

Creating database by using user input form, without preventing SQL injection is not a good approach. use PDO / prepared statement to prevent with SQL injection.

One more suggestion for future visitors:

instead of die("Database already exist.. please try different name"); use mysqli_error() to get actual error if your query not executed.

devpro
  • 16,184
  • 3
  • 27
  • 38