2016-11-29 47 views
2

我已經創建了簡單的代碼,它必須通過PHP在mysql上添加用戶,但事實並非如此。這是代碼。如何通過php代碼在mysql服務器上添加用戶?

<form action='' method='post'> 
Login: <input type='text' name='login' /> <br/> 
Pass: <input type='pass' name='pass' /> <br/> 
<input type='submit'> 
</form> 

<?php 
$c=mysql_connect('localhost','test','test'); 

$login=$_POST['login']; 
$pass=$_POST['pass']; 

$t="CREATE USER '".$login."'@'localhost' IDENTIFIED BY PASSWORD '".$pass."';"; 

if($login!=NULL){ 
    if($q=mysql_query($t,$c)) 
    { 
    echo "CREATED!"; 
    }else{ 
    die('ERROR: ' . mysql_error()); 
    } 
} 
?> 

Error: Password hash should be a 41-digit hexadecimal number

哪些錯誤呢?

+1

取出字'PASSWORD' 。通過使用該關鍵字,它表明你提供了散列。 https://dev.mysql.com/doc/refman/5.5/en/create-user.html – aynber

+0

謝謝,它的工作 – krispi

回答

0

取出PASSWORD這個詞。通過使用該關鍵字,它表明你提供了散列。 dev.mysql.com/doc/refman/5.5/en/create-user.html

你想這樣的:

$t="CREATE USER '".$login."'@'localhost' IDENTIFIED BY '".$pass."';"; 
0

試試這個:

<?php 
$host="localhost"; 
$user="root"; 
$password=""; 
$con = mysqli_connect("localhost","root","","mydb"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
?> 

<!DOCTYPE html> 
    <head> 
     <title>MyExample</title> 
    <meta charset="UTF-8"/> 
</head> 
<body> 
    <h1>Create user in MySQL by FORM</h1> 
    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST"> 
     <label for="username">Username</label> 
     <input type="text" id="username" name="username" /> 
     <label for="password">Password</label> 
     <input type="password" id="password" name="password"/> 
     <button type="submit" name="send">Send</button> 
    </form> 
</body> 
</html> 
<?php 
    if (isset($_POST['send'])) { 
     $username = $_POST['username']; 
     $password = $_POST['password']; 
     mysqli_query($con,"CREATE USER '".$username."'@'".$host."' IDENTIFIED BY '".$password."'"); 
     mysqli_close($con); 
    } 
    ?> 
+0

順便說一句,mysql_connect已棄用 –

相關問題