Error in connecting PHP to MYSQL -
good day! i'm trying connect code database localhost/phpmyadmin.
this code:
in register.php file:
<?php session_start(); require_once('dbconfig/config.php'); //phpinfo(); ?>
config.php file database name located
<!doctype html> <html> <head> <title>registration page</title> <link rel="stylesheet" href="css/style.css"> </head> <body style="background-color:#bdc3c7"> <div id="main-wrapper"> <center><h2>registration form</h2></center> <form action="register.php" method="post"> <div class="imgcontainer"> <img src="imgs/avatar.png" alt="avatar" class="avatar"> </div> <div class="inner_container"> <label><b>username</b></label> <input type="text" placeholder="enter username" name="username" required> <label><b>password</b></label> <input type="password" placeholder="enter password" name="password" required> <label><b>confirm password</b></label> <input type="password" placeholder="enter password" name="cpassword" required> <button name="register" class="sign_up_btn" type="submit">sign up</button> <a href="index.php"><button type="button" class="back_btn"><< login</button></a> </div> </form> <?php if(isset($_post['register'])) { @$username=$_post['username']; @$password=$_post['password']; @$cpassword=$_post['cpassword']; if($password==$cpassword) { $query = "select * userinfotbl username='$username'"; //echo $query; $query_run = mysqli_query($con,$query); //echo mysql_num_rows($query_run); if($query_run) { if(mysqli_num_rows($query_run)>0) { echo '<script type="text/javascript">alert("this username exists.. please try username!")</script>'; } else { $query = "insert userinfotbl values('$username','$password')"; $query_run = mysqli_query($con,$query); if($query_run) { echo '<script type="text/javascript">alert("user registered.. welcome")</script>'; $_session['username'] = $username; $_session['password'] = $password; header( "location: homepage.php"); } else { echo '<p class="bg-danger msg-block">registration unsuccessful due server error. please try later</p>'; } } } else { echo '<script type="text/javascript">alert("db error")</script>'; } }
this line error occurs. don't know happened.
else { echo '<script type="text/javascript">alert("password , confirm password not match")</script>'; } } else { } ?> </div> </body> </html>
however in localhost page got error saying "localhost says: db error" don't know what's wron it.
it means "select * userinfotbl username='$username'"
query not found entities match condition or got error.
returns false on failure. successful select, show, describe or explain queries mysqli_query() return mysqli_result object. other successful queries mysqli_query() return true.
http://php.net/manual/en/mysqli.query.php
use mysqli_error see error got.
example:
if($password==$cpassword) { $query = "select * userinfotbl username='$username'"; //echo $query; $query_run = mysqli_query($con,$query); //echo mysql_num_rows($query_run); if(!empty($query_run)) { ... } elseif ($query_run === false) echo mysqli_error($con); else echo '<script type="text/javascript">alert("db error")</script>'; }
Comments
Post a Comment