2017-04-16 29 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