-3
<?php
    if(isset($_POST['btn-signup'])) {
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "test";
            $conn = new mysqli($servername, $username, $password, $dbname);
            if($conn->connect_error)
                {
                    die("Connection failed :" . $conn->connect_error);
                }   
        $uname = trim($_POST['uname']);
        $email = trim($_POST['email']);
        $upass = trim($_POST['pass']);
        $mobile = trim($_POST['mobile']);
        $fee = trim($_POST['fee']);

        $uname = strip_tags($uname);
        $email = strip_tags($email);
        $upass = strip_tags($upass);
        $mobile = strip_tags($mobile);
        $fee = strip_tags($fee);
        $role = "user";


// check email exist or not
        $query = "SELECT email FROM users WHERE email='$email'";
        $result = mysqli_query($query);

        $count = mysqli_num_rows($result); // if email not found then proceed

if ($count==0) {

        $query = "INSERT INTO users(username,email,password,mobile,fee,role) VALUES('$uname','$email','$upass','$mobile','$fee','$role')";
        $res = mysqli_query($query);

    if ($res) {
        $errTyp = "success";
        $errMSG = "successfully registered, you may login now";
    } else {
        $errTyp = "danger";
        $errMSG = "Something went wrong, try again later..." .mysql_error();    
    }   

} else {
    $errTyp = "warning";
    $errMSG = "Sorry Email already in use ...";
}
mysqli_close($conn);
}

?>

whenever i click on submit button it always give me "something went wrong error" even i check for the error that has to be "sorry email already in use" but it always showing "something went wrong". i use session also. i am newbie in php and creating a session based login system for different role such as admin and student. please give me solution as soon as possible thank you :)

Amit Chauhan
  • 1,810
  • 3
  • 17
  • 25
  • Why don't you check for errors in your code? If you did you'd know what was wrong: [`mysql_error()`](http://php.net/manual/en/function.mysql-error.php) – John Conde Aug 19 '16 at 13:14
  • bro it shwoing error " Something went wrong, try again later...No database selected" when i use mysql_error() but i olready mention the dbname . now what to do ? – Amit Chauhan Aug 19 '16 at 13:19
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Epodax Aug 19 '16 at 13:25
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). `strip_tags` is **not** a suitable escaping method. – tadman Aug 19 '16 at 15:37

2 Answers2

2

Use only "mysqli_" or "mysql_", recommended is "mysqli_"

You are connecting database with "mysqli_" and fetching the result set using "mysql_"

Learn the Difference MYSQL vs MYSQLi vs PDO

Community
  • 1
  • 1
PravinS
  • 2,640
  • 3
  • 21
  • 25
0

I have seen your code.

Please check the syntax of Insert query once , print the query and run in mysql db and see if it run fine or not.

I think this will resolve your issue.

Shallini
  • 1
  • 1