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 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗