Jump to content

php and creating databses


wrathican

Recommended Posts

hi.
im extremely new to coding php.
i have a problem with coding my databse creation form, i want to use the variables from a form to send to a database creation script.
[b]heres the code for my html form:[/b]

<form method="post" action="createdb.php">
user name: <input type="text" name="user" />
password: <input type="password" name="pass" />
database name: <input type="text" name="dbname" />
<input type="submit" />
</form>

[b]and heres the code from my php script:[/b]

<?php
$con = mysql_connect("mysqlhostserver",$_POST["user"],$_POST["pass"]);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE $_POST["dbname"]",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>

now i got the code from w3 and instead of the $_POST variables it had the username, password and the database name already written into the script, now i dont want this. i want to be able to enter the username, password and databse name into a form and do it from there.
please help?
thanks in advanced
Link to comment
https://forums.phpfreaks.com/topic/27325-php-and-creating-databses/
Share on other sites

Try this:

[code]<?php
$con = mysql_connect("localhost",$_POST["user"],$_POST["pass"]);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE".$_POST["dbname"],$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>[/code]

You will have to have already given the username permission to create new databases.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.