2015-12-30 69 views
1

我正在學習php mysql,但我正面臨着這個問題。我無法將登錄信息插入到我的數據庫表中。我的數據庫名稱是loginapp,表名是用戶。請仔細閱讀這些我創建了----插入登錄信息到mysql數據庫

<?php 
if(isset($_POST['submit'])){ 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $connection = mysqli_connect('localhost', 'root', '','loginapp'); 
    if(!$connection){ 
    die("Database connection failed"); 
    } 
    $query = "INSERT INTO users(username,password)"; 
    $query .= "VALUES ('$username', '$password')"; 
    $result = mysqli_query($connection, $query); 
    if(!$result){ 
    die('query is faild' . mysqli_connect_error()); 
    } 
} 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Login </title> 
    <!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 

</head> 
<body> 

    <div class="container"> 
     <div class="col-xs-6"> 
      <form action="login.php" method="post"> 
       <div class="form-group"> 
        <label for="username">Username</label> 
        <input type="text" name="username"class="form-control"> 
       </div> 
        <div class="form-group"> 
        <label for="password">Password</label> 
        <input type="password" name="password" class="form-control"> 
       </div> 
       <input class="btn btn-primary" type="submit" name="submit" value="Submit"> 
      </form> 
     </div> 
    </div> 
</body> 
</html> 

回答

2

試試這個

$username = mysqli_real_escape_string($connection, $_POST['username']); 
$password = mysqli_real_escape_string($connection, $_POST['password']); 

$query = "INSERT INTO users(username,password)"; 
$query .= " VALUES ('$username', '$password')"; 

此外,檢出你的表和列名的拼寫是否正確。

+0

感謝您的努力。 –

5

你自己的價值觀之前,需要空間

$query = "INSERT INTO users(username,password)"; 

$query .= " VALUES ('$username', '$password')"; 
      ^

你的代碼是開放的SQL注入代碼,查看下面的鏈接,以防止它

How can I prevent SQL injection in PHP?

要查詢您的查詢中的錯誤使用

printf("Errormessage: %s\n", $mysqli->error); 

http://php.net/manual/en/mysqli.error.php

在你與數據庫連接使用校驗錯誤

if ($mysqli->connect_errno) { 
    printf("Connect failed: %s\n", $mysqli->connect_error); 
    exit(); 
} 

http://php.net/manual/en/mysqli.error.php

不要存儲明文口令到數據庫的使用

http://php.net/manual/en/faq.passwords.php

http://php.net/manual/en/function.password-hash.php

+0

嘿,非常感謝。事實就是空間。現在它的工作。 –