dongmi0760 2016-06-25 22:03
浏览 14
已采纳

mysqli_query没有添加用户

I'm trying to create a log in feature using PHP and MySQL.

I'm pretty new to back end coding so I'm having some issues. I've checked all other threads on here and nothing has worked.

I am using localhost and I've managed to connect the database I created.

I used this query to create the database and table:

CREATE DATABASE `dbtest` ;
CREATE TABLE `dbtest`.`users` (
`user_id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 35 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL ,
UNIQUE (`email`)
) ENGINE = MYISAM ;

Here is my file: dbconnect.php connecting to the database:

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "root";



// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(!mysqli_select_db($conn, "dbtest"))
{
    die('oops database selection problem ! --> '.mysqli_error());
}
?>

and my registration page:

<?php
session_start();
if(!empty($_SESSION['user']))
{
    header("Location: home.php");
}
include_once 'dbconnect.php';

if(isset($_POST['btn-signup']))
{
    $uname = mysqli_real_escape_string($conn, $_POST['uname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $upass = md5(mysqli_real_escape_string($conn, $_POST['pass']));
    
    $uname = trim($uname);
    $email = trim($email);
    $upass = trim($upass);
    
    // email exist or not
    $query = "SELECT user_email FROM users WHERE user_email='$email'";
    $result = mysqli_query($conn, $query);
    
    $count = mysqli_num_rows($result); // if email not found then register
    
    if($count == 0){
        
        if(mysqli_query($conn, "INSERT INTO users(user_name,user_email,user_pass) VALUES('$uname','$email','$upass')"))
        {
            ?>
            
            <?php
            echo ("<p style='color:green; text-align:center; font-size:30px'>Successful!: </p>");
        }
        else
        {
            ?>
            
            <?php
            echo ("<p style='color:red; text-align:center; font-size:30px'>Error While Registering you : </p>");
        }       
    }
    else{
            ?>
            <script>alert(' ...');</script>
            <?php
        echo ("<p style='color:red; text-align:center; font-size:30px'>This Email address is already taken: </p>");
    }
    
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration System</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />

</head>
<body>
<center>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="uname" placeholder="User Name" required /></td>
</tr>
<tr>
<td><input type="email" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="pass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
<tr>
<td><a href="index.php">Sign In Here</a></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>

When I try to register a new user, the SQL query gives me the error "Error while registering you" on line 39 of register.php.

Can anyone see why this is? I've been stuck for days now.

I've tried adding $conn, as the first parameter of the queries (similar to line 13 of dbconnect.php as I read somewhere that it should fix it. It didn't so I've removed them for now.

</div>
  • 写回答

1条回答 默认 最新

  • dpjtn48868 2016-06-25 23:17
    关注

    Another problem, database is created with:

    `username` VARCHAR( 25 ) NOT NULL ,
    `email` VARCHAR( 35 ) NOT NULL ,
    `password` VARCHAR( 50 ) NOT NULL ,
    

    But queries use user_email instead of email and user_name instead of username and user_pass instead of password:

    SELECT user_email FROM users WHERE user_email=
    INSERT INTO users(user_name,user_email,user_pass)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败