S A N T A Posted April 30, 2008 Share Posted April 30, 2008 ok so i would like it so when someone registers it creates a database for them. example: if($_POST['submit']) { //some code to create a database with the variable $databasename } else { <form action="<?php echo $SCRIPT_NAME ?>" method="POST"> <table> <tr> <td>Database name</td> <td><input type="text" name="dbname"></td> </tr> <tr> <td>Username</td> <td><input type="text" name="username"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"></td> </tr> <tr> <td>Re-type Password</td> <td><input type="password" name="password2"></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname"></td> </tr> <tr> <td></td> <td><input type="submit" value="Register!"></td> </tr> </table> </form> } like that so that it creates your own database with the name like "fred" so it would create a database called "fred" thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/ Share on other sites More sharing options...
Fadion Posted April 30, 2008 Share Posted April 30, 2008 U want a database for each user? Never heard of such approach, and guess ill never will. Even one table for one user in the same database, would be no-sense. Probably u need this for a specific requirement, so explain better. The code anyway is: <?php $dbname = $_POST['dbname']; mysql_query("CREATE DATABASE $dbname"); ?> As for the username and password for each db, normally u connect to one database server and have different databases with one username, password. I guess this goes to creating users and assigning permissions, so it is beyond my php knowledge (if it can be done with php anyway!!). Ud want to create tables also for them and the syntax is "CREATE TABLE user(name TINYTEXT, .....". Search in google and u'll be suprised on how much information ull get. Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-529999 Share on other sites More sharing options...
S A N T A Posted April 30, 2008 Author Share Posted April 30, 2008 i tried this: <?php session_start(); require("config.php"); $db = mysql_connect($dbhost, $dbuser); mysql_select_db($dbdatabase, $db); if($_POST['submit']) { $dbname = $_POST['dbname']; mysql_query("CREATE DATABASE $dbname"); //some code to create a database with the variable $databasename } else { ?> <form action="<?php echo $SCRIPT_NAME ?>" method="POST"> <table> <tr> <td>Database name</td> <td><input type="text" name="dbname"></td> </tr> <tr> <td>Username</td> <td><input type="text" name="username"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"></td> </tr> <tr> <td>Re-type Password</td> <td><input type="password" name="password2"></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname"></td> </tr> <tr> <td></td> <td><input type="submit" value="Register!"></td> </tr> </table> </form> <?php } ?> didnt create the database lol I'm doing something wrong Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530001 Share on other sites More sharing options...
redarrow Posted April 30, 2008 Share Posted April 30, 2008 quick example for u.... <?php include 'config.php'; include 'opendb.php'; $query = 'CREATE DATABASE phpcake'; $result = mysql_query($query); mysql_select_db('phpcake') or die('Cannot select database'); $query = 'CREATE TABLE contact( '. 'cid INT NOT NULL AUTO_INCREMENT, '. 'cname VARCHAR(20) NOT NULL, '. 'cemail VARCHAR(50) NOT NULL, '. 'csubject VARCHAR(30) NOT NULL, '. 'cmessage TEXT NOT NULL, '. 'PRIMARY KEY(cid))'; $result = mysql_query($query); include 'closedb.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530006 Share on other sites More sharing options...
Fadion Posted April 30, 2008 Share Posted April 30, 2008 mysql_select_db() doesnt have to be called, just the mysql_connect(). Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530008 Share on other sites More sharing options...
teng84 Posted April 30, 2008 Share Posted April 30, 2008 mysql_select_db() doesnt have to be called, just the mysql_connect(). ??? Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530010 Share on other sites More sharing options...
S A N T A Posted April 30, 2008 Author Share Posted April 30, 2008 so your saying this would work? <?php require("config.php"); $db = mysql_connect($dbhost, $dbuser); if($_POST['submit']) { $dbname = $_POST['dbname']; mysql_query("CREATE DATABASE $dbname"); //some code to create a database with the variable $databasename } else { ?> <form action="<?php echo $SCRIPT_NAME ?>" method="POST"> <table> <tr> <td>Database name</td> <td><input type="text" name="dbname"></td> </tr> <tr> <td>Username</td> <td><input type="text" name="username"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"></td> </tr> <tr> <td>Re-type Password</td> <td><input type="password" name="password2"></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="lastname"></td> </tr> <tr> <td></td> <td><input type="submit" value="Register!"></td> </tr> </table> </form> <?php } ?> well it doesn't Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530013 Share on other sites More sharing options...
priti Posted April 30, 2008 Share Posted April 30, 2008 kindly change this line and your piece of code should work <td><input type="submit" value="Register!"></td> to <td><input type="submit" value="Register!" name="submit"></td> your if condition is not getting executed . Hope it works!! Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530015 Share on other sites More sharing options...
The Little Guy Posted April 30, 2008 Share Posted April 30, 2008 Does your host allow you to make databases from php? and does the user have the permissions to create a database? Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530018 Share on other sites More sharing options...
S A N T A Posted April 30, 2008 Author Share Posted April 30, 2008 kindly change this line and your piece of code should work <td><input type="submit" value="Register!"></td> to <td><input type="submit" value="Register!" name="submit"></td> your if condition is not getting executed . Hope it works!! Thanks I'm stupid when it comes to errors Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530020 Share on other sites More sharing options...
Fadion Posted April 30, 2008 Share Posted April 30, 2008 mysql_select_db() doesnt have to be called, just the mysql_connect(). ??? Whats so strange? U cant create a db when an existing one is bound to your connection. Quote Link to comment https://forums.phpfreaks.com/topic/103509-solved-creating-database-with-php/#findComment-530023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.