Outline ·
[ Standard ] ·
Linear+
Help needed, why this code is not working.
TSGOPI56
|
Oct 19 2016, 11:07 AM, updated 8y ago
|
|
Hi all, I a creating a login and registration for a web page. The code for the registration is not working,it shows User Registration Failed. I have no idea where i have gone wrong. CODE <?php session_start(); include("includes/config.php");
if (isset($_POST['username']) && isset($_POST['password'])){ $username = $_POST['username']; $email = $_POST['email']; $password = $_POST['password'];
$encpassword = md5(addslashes($_POST['password'])); $query = "INSERT INTO tbl_users (username, password, useremail) VALUES ('$username', '$encpassword', '$email')"; $result = mysql_query($connection, $query); if($result){ $smsg = "User Created Successfully."; }else{ $fmsg ="User Registration Failed"; } } ?> <html> <head> <title>User Registeration</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /> <link rel="stylesheet" href="css/signin.css" type="text/css" /> <link rel="stylesheet" href="css/styles.css" > <!-- Latest compiled and minified JavaScript --> <script src="css/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <form class="form-signin" method="POST"> <?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 } ?> <h2 class="form-signin-heading">Please Register</h2> <div class="input-group"> <input type="text" name="username" class="form-control" placeholder="Username" required>
<label for="inputEmail" class="sr-only">Email address</label> <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button> <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a> </form> </div>
</body> </html>
This post has been edited by GOPI56: Oct 19 2016, 01:31 PM
|
|
|
|
danieln
|
Oct 19 2016, 11:15 AM
|
|
try echo out the $encpassword to check
it may contain some character that is not accepted by mysql
|
|
|
|
TSGOPI56
|
Oct 19 2016, 11:32 AM
|
|
QUOTE(danieln @ Oct 19 2016, 12:15 PM) try echo out the $encpassword to check it may contain some character that is not accepted by mysql I tried echo out, it echo outs the password encrypted in md5. I think there is some problem at the database insert part.
|
|
|
|
Ryutaro
|
Oct 19 2016, 12:32 PM
|
|
QUOTE(GOPI56 @ Oct 19 2016, 11:32 AM) I tried echo out, it echo outs the password encrypted in md5. I think there is some problem at the database insert part. 1. Use the code template when u r posting codes, looking at it raw make me headache 2. No error code? 3. To help u with troubleshooting, I would suggest first u check whether the connection to database is valid
|
|
|
|
xsan
|
Oct 19 2016, 02:15 PM
|
|
check connection to db.. ensure the username, password used is correct
|
|
|
|
FourZeroFour
|
Oct 19 2016, 02:45 PM
|
Getting Started
|
CODE $query = "INSERT INTO tbl_users (username, password, useremail) VALUES ('$username', '$encpassword', '$email')"; Not a php coder here but, in php do variables still get expanded when being wrapped in single/strong quotes? By the way, what you're doing in the code is a bad practice when executing sql call. Consider the advices in the link https://forum.lowyat.net/topic/3398408This post has been edited by FourZeroFour: Oct 19 2016, 02:49 PM
|
|
|
|
chriss2010
|
Oct 19 2016, 03:52 PM
|
Getting Started
|
use real escape string before sql... check db field length especially for password anyway u can debug in chrome just open the form page and hit f12, submit the form once and then select network tab it'll show red filename for script errors, click on the file and it may show the exact line error This post has been edited by chriss2010: Oct 19 2016, 03:57 PM Attached thumbnail(s)
|
|
|
|
TSGOPI56
|
Oct 19 2016, 09:48 PM
|
|
I tried eyerything but still not working. What is wrong ? Here is my table columns:
|
|
|
|
otakotak
|
Oct 19 2016, 09:55 PM
|
Getting Started
|
printing mysql_error() when $result == false will give u the answer
|
|
|
|
TSGOPI56
|
Oct 20 2016, 09:04 AM
|
|
QUOTE(otakotak @ Oct 19 2016, 10:55 PM) printing mysql_error() when $result == false will give u the answer But the data is not entering into database.
|
|
|
|
wengwei
|
Oct 20 2016, 09:47 AM
|
New Member
|
QUOTE(chriss2010 @ Oct 19 2016, 03:52 PM) use real escape string before sql... check db field length especially for password anyway u can debug in chrome just open the form page and hit f12, submit the form once and then select network tab it'll show red filename for script errors, click on the file and it may show the exact line error I usually do it this way also.
|
|
|
|
danieln
|
Oct 20 2016, 10:09 AM
|
|
QUOTE(GOPI56 @ Oct 20 2016, 09:04 AM) But the data is not entering into database. that's why you should print out the error to see that is the problem la
|
|
|
|
cl.tech999
|
Oct 20 2016, 11:32 AM
|
New Member
|
QUOTE(GOPI56 @ Oct 20 2016, 09:04 AM) But the data is not entering into database. try change the variable ($username, $encpassword, $email) in this code to value, and testing it can insert to database or not CODE $query = "INSERT INTO tbl_users (username, password, useremail) VALUES ('$username', '$encpassword', '$email')"; or echo the $query to check the query statement This post has been edited by cl.tech999: Oct 20 2016, 11:34 AM
|
|
|
|
alien3d
|
Oct 20 2016, 12:38 PM
|
|
[quote=GOPI56,Oct 19 2016, 11:07 AM] Hi all, I a creating a login and registration for a web page. The code for the registration is not working,it shows User Registration Failed. I have no idea where i have gone wrong. changed more proper code.. these code not tested ya CODE
$username = filter_input(INPUT_GET,"username"); $password = filter_input(INPUT_GET,"password"); $useremail = filter_input(INPUT_POST,"useremail"); if(strlen($username)>0 && strlen($password)>0){ $query="INSERT INTP tbl_user(username,password,useremail) values(?,?,?); try { // use mysqli or pdo please.mysql function allready depreciated $stmt = $mysqli->prepare($query); $stmt->bind_param('sss',$username,md5($password),$useremail); mysqli_stmt_execute($stmt); if($result){ $messsage="Got Record inserted"; }else{ $message= mysqli_error($link); } }catch(Exception $ex){ $message = $ex->getMessage(); } } else{ $message = "Either username length is missing or password field "; } echo $message;
|
|
|
|
TSGOPI56
|
Oct 20 2016, 12:53 PM
|
|
QUOTE(cl.tech999 @ Oct 20 2016, 12:32 PM) try change the variable ($username, $encpassword, $email) in this code to value, and testing it can insert to database or not CODE $query = "INSERT INTO tbl_users (username, password, useremail) VALUES ('$username', '$encpassword', '$email')"; or echo the $query to check the query statement When i echo $query. The output is: INSERT INTO tbl_users (userid, username, useremail, password) VALUES ('','GOPI56', 'gopi@yahoo.com', '1234')
|
|
|
|
cubiclecarbonate
|
Oct 20 2016, 01:39 PM
|
|
QUOTE(GOPI56 @ Oct 20 2016, 12:53 PM) When i echo $query. The output is: INSERT INTO tbl_users (userid, username, useremail, password) VALUES ('','GOPI56', 'gopi@yahoo.com', '1234') copy the query then execute directly in the phpmyadmin see the output result
|
|
|
|
cl.tech999
|
Oct 20 2016, 01:44 PM
|
New Member
|
QUOTE(GOPI56 @ Oct 20 2016, 12:53 PM) When i echo $query. The output is: INSERT INTO tbl_users (userid, username, useremail, password) VALUES ('','GOPI56', 'gopi@yahoo.com', '1234') for autoincrement, no need put the userid in sqlquery
|
|
|
|
alien3d
|
Oct 20 2016, 02:31 PM
|
|
QUOTE(GOPI56 @ Oct 20 2016, 12:53 PM) When i echo $query. The output is: INSERT INTO tbl_users (userid, username, useremail, password) VALUES ('','GOPI56', 'gopi@yahoo.com', '1234') shouldn't be.. that output ? if md5 should bring out weird character instead of above ? 2. Please read user manual php http://php.net/manual/en/function.mysql-query.phpQUOTE For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
|
|
|
|
stevenlai84
|
Oct 20 2016, 09:48 PM
|
New Member
|
simply change this line=> $result = mysql_query($connection, $query); to this => $result = mysql_query($query,$connection);
|
|
|
|
TSGOPI56
|
Oct 20 2016, 11:04 PM
|
|
Hi All, i have changed the code quite a bit, data is being entered into the database but when i check in the database there is no data. CODE <?php session_start(); $servername = "localhost"; $username = "root"; $password = "4035"; $dbname = "users";
// Create connection $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } if ($_POST['username'] and $_POST['password']){ $username = mysqli_real_escape_string($_POST['username']); $email = mysqli_real_escape_string($_POST['email']); $pass = mysqli_real_escape_string($_POST['password']);
echo "$pass"; echo "$username"; echo "$email"; $sql = "INSERT INTO tbl_users (userid, username, password, useremail) VALUES ('','$username', '$email', '$pass')"; if ($conn->query($sql) === TRUE) { $smsg = "User Created Successfully."; } else { $fmsg ="User Registration Failed"; echo "Error: " . $sql . "<br>" . $conn->error; } } ?> <html> <head> <title>User Registeration</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /> <link rel="stylesheet" href="css/signin.css" type="text/css" /> <link rel="stylesheet" href="css/style.css" >
</head> <body> <div class="container"> <form class="form-signin" method="POST"> <?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 } ?> <h2 class="form-signin-heading">Please Register</h2> <div class="input-group"> <input type="text" name="username" class="form-control" placeholder="Username" required>
<label for="inputEmail" class="sr-only">Email address</label> <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> <label for="inputPassword" class="sr-only">Password</label> <input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required> <div class="checkbox"> <label> <input type="checkbox" value="remember-me"> Remember me </label> </div> <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button> <a class="btn btn-lg btn-primary btn-block" href="login.php">Login</a> </form> </div>
</body> </html>
|
|
|
|