2017-04-16 32 views
-1

我想使用php和mysql進行註冊表單。
我使用xampp和phpmyadmin從這個和我的sql查詢表'用戶'工作正常。
但是當我嘗試插入使用PHP代碼時,它顯示「用戶註冊失敗」。下面
是我的代碼

register.php
我在做什麼這個register.php php代碼錯了?

<?php 
    require('includes/connect.php'); 
    // If the values are posted, insert them into the database. 
    if (isset($_POST['username']) && isset($_POST['psw'])){ 
     $username = $_POST['username']; 
     $email = $_POST['email']; 
     $password = $_POST['psw']; 
    $query = "INSERT INTO `user` (username, password, email) VALUES('$username', '$password', '$email')"; 
     $result = mysqli_query($connection, $query); 
     if($result){ 
      $smsg = "User Created Successfully."; 
     }else{ 
      $fmsg ="User Registration Failed"; 
     } 
     } 
     ?> 

<html> 
<head> 
    <title>BondOnNet | Register</title> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 

<!-- JS --> 
<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script> 
<script src="js/jquery.jcarousel.pack.js" type="text/javascript"></script> 
<script src="js/jquery-func.js" type="text/javascript"></script>  
<!-- End JS --> 

<link rel="stylesheet" type="text/css" href="css/register.css"> 
</head> 
<body> 
<div id="Register_header"> 
    <h1 id="logo"><a href="index.html">BondOnNet</a></h1> 
</div> 
<div id="register_container"> 
    <form method="POST" style="border:1px solid #ccc"> 
     <?php if(isset($smsg)){ ?><div class="alert alert-success" role="alert"> <?php echo $smsg; ?> </div><?php } ?> 
     <?php if(isset($fmsg)){ ?><div class="alert alert-danger" role="alert"> <?php echo $fmsg; ?> </div><?php } ?> 
     <div class="imgcontainer"> 
     <img src="images/avatar.png" alt="Avatar" class="avatar"> 
     </div> 
     <div class="container"> 
     <label><b>Username</b></label> 
     <input type="text" placeholder="Enter Username" name="username" required> 

     <label><b>Email</b></label> 
     <input type="text" placeholder="Enter Email" name="email" required> 

     <label><b>Password</b></label> 
     <input type="password" placeholder="Enter Password" name="psw" required> 

     <label><b>Repeat Password</b></label> 
     <input type="password" placeholder="Repeat Password" name="psw-repeat" required> 
     <input type="checkbox" checked="checked"> Remember me 
     <p>By creating an account you agree to our <a href="terms.html" style="color: #4CAF50;">Terms & Privacy</a></p> 

     <div class="clearfix"> 
      <button type="submit" class="signupbtn">Sign Up</button> 
     </div> 
     </div> 
    </form> 
</div> 
</body> 
</html> 


connect.php

<?php 
$connection = mysqli_connect('localhost', 'root', ''); 
if (!$connection){ 
    die("Database Connection Failed" . mysqli_error($connection)); 
} 
$select_db = mysqli_select_db($connection, 'test'); 
if (!$select_db){ 
    die("Database Selection Failed" . mysqli_error($connection)); 
} 
?> 

我試圖代碼,所以很多小的變化,但沒有它的工作!
PLZ幫我理清什麼即時做錯了

+1

'echo'你'$ query'變量,複製和直接在phpmyadmin中運行打印的查詢。您會對查詢失敗的原因有所瞭解。
此外,使用'mysqli_error($連接)'在執行後打印查詢的錯誤 – gaganshera

+0

你的查詢變量$ result的var_dump()是什麼?將它放在if(){}語句中,以確保它在那裏傳遞邏輯。 您是否嘗試將原始查詢注入SQL phpmyadmin(如果可以查看查詢在SQL環境中是否正常運行? –

+2

嘗試使用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)而不是引用字符串。這將有助於防止SQL注入並逃脫任何奇怪的字符。 – ethrbunny

回答

0

使用此檢測錯誤:

mysqli_connect_errno() - Returns the error code from last connect call 
mysqli_connect_error() - Returns a string description of the last connect error 
mysqli_errno() - Returns the error code for the most recent function call 
mysqli_sqlstate() - Returns the SQLSTATE error from previous MySQL operation 

欲瞭解更多信息和exemples:http://php.net/manual/en/mysqli.error.php