2014-01-09 38 views
-1

我想知道如何爲用戶輸入一個文本框,然後創建一個以該輸入命名的數據庫。如何在用戶輸入後命名mysql數據庫

<?php 
/* 
code to connect and make a mysql database named after the user input 
*/ 

?> 
<!DOCTYPE HTML>  
<html> 
<head> 

</head> 
<body> 
<form action=<?php $_SERVER['PHP_SELF']; ?>> 
<input type="text" name="databasename"> 
<input type="submit" name="submit"> 
</form> 
</body> 
</html> 
+3

你確定你不想創建一個TABLE,或者也許只是一行有關該用戶的相關數據?爲每個用戶創建一個完整的數據庫是不尋常的。 – gibberish

+1

想象一下,1000+用戶,即使是100-500。我希望你有一個自動化的方法來創建將與表,維護等等一起的代碼等。---創建關係表,而不是數據庫。你會在工作中接受你的腋窩。我當然希望你知道你在做什麼。 –

回答

4

在技術上取決於您使用的數據庫管理系統,但只是做一個SQL查詢(使用MySQL)的「CREATE DATABASE數據庫名稱」會做到這一點。

但是,除非你正在創建一個像PhpMyAdmin這樣的數據庫管理工具,否則不要這樣做。你只是要求用戶在你的系統中運行橫行。

而這只是基礎知識。我懇請您致電read the documentation on MySQL

0

由於您尚未爲您的表單聲明方法,因此它默認爲GET。

$db_name = $_GET['databasename']; 

$host="localhost"; 

$user="username"; 
$password="pa55word"; 

$con=mysqli_connect($host,$user,$password); 

// Create database 
$query="CREATE DATABASE `$db_name`"; 
if (mysqli_query($con,$query)) 
    { 
    echo "Database created"; 
    } 
else 
    { 
    echo "Error creating database..."; 
    } 

如果用戶不是root,那麼您需要確保您已授予用戶足夠的權限來創建數據庫。

1

您絕對必須確保用戶的輸入是可用的,而不是黑客攻擊或錯誤。所以,首先你檢查輸入,然後如果沒關係,你創建一個數據庫。

這是假設您在表單中添加action =「post」。我不喜歡把輸入放到「get」中,因爲那時你的變量是URL的一部分,有些人可能會嘗試在那裏設置書籤。

if(isset($_POST['databasename'])) { //this lets us know the form has posted 
    // 1. Check for acceptable name 
    $name = $_POST['databasename']; 
    $name = strtolower($name);  //all lowercase (makes things easier to deal with) 
    $name = preg_replace("/[^a-z]/", '', $name); //get rid of things that aren't letters 

    if($name != '') { 
     // 2. Create the database 
     $mysqli = new mysqli('localhost', 'my_user', 'my_password'); 

     if ($mysqli->connect_error) { 
      throw new Exception("Connect Error ($mysqli->connect_errno) $mysqli->connect_error"); 
     } 
     $query = "CREATE DATABASE `$name`"; 
     $mysqli->query($query); 
     if($mysqli->errno) { 
      throw new Exception("Error creating database: $mysqli->error"); 
      // I am pretty sure this is enough to catch the error of the database already existing 
     } 
     echo "Database $name created."; 
    } else { 
     throw new Exception("Invalid name"); 
    } 
} 

如果我是你,我會把它放在try-catch中來捕捉異常並處理它們。

+0

這不會佔用空間'$ name = preg_replace(「/ [^ az] /」,'',$ name);' –

+0

這個'$ string = preg_replace(「/ [^ \ w] + /」, 「」,$ string);'用空格替換所有非空格和非單詞的字符 –

+0

空格不是從一個到z,因此它排除了空格和其他所有內容。從技術上講,未加引號的MYSQL數據庫名稱只能包含字符'0-9a-zA-Z $ _ []'加上一些擴展的unicode(但是如果在數據庫名稱中允許$,則不能在PHP中使用雙引號)。出於這個原因,使用'[^ ​​\ w]'是不明智的,因爲它會允許不允許不加引號的大量字符。如果每次使用它時都會仔細引用數據庫名稱,那麼實際上,允許空格(除了最後一個字符)以及其他許多事物。 – miyasudokoro

0

您可以使用此方法來檢查是否存在具有相同名稱和通過其他錯誤創建和顯示創建successfuly

<?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"; 
    } 
    ?> 

爲HTML格式請參考下面的代碼數據庫 -

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