Welcome Guest ( Log In | Register )

Bump Topic Topic Closed RSS Feed

Outline · [ Standard ] · Linear+

 Help needed, why this code is not working.

views
     
TSGOPI56
post Oct 19 2016, 11:07 AM, updated 8y ago

Regular
******
Senior Member
1,396 posts

Joined: Dec 2012
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
post Oct 19 2016, 11:15 AM

Regular
******
Senior Member
1,249 posts

Joined: Feb 2008
try echo out the $encpassword to check

it may contain some character that is not accepted by mysql
TSGOPI56
post Oct 19 2016, 11:32 AM

Regular
******
Senior Member
1,396 posts

Joined: Dec 2012
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
post Oct 19 2016, 12:32 PM

~ Just a noob programmer ~
****
Senior Member
541 posts

Joined: Mar 2011
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
post Oct 19 2016, 02:15 PM

Ouh Emm Gee I Can Edit This Nao
******
Senior Member
1,020 posts

Joined: Jul 2006
From: Bastion Keep


check connection to db..
ensure the username, password used is correct
FourZeroFour
post Oct 19 2016, 02:45 PM

Getting Started
**
Junior Member
175 posts

Joined: Apr 2008
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/3398408

This post has been edited by FourZeroFour: Oct 19 2016, 02:49 PM
chriss2010
post Oct 19 2016, 03:52 PM

Getting Started
**
Junior Member
71 posts

Joined: Sep 2009
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)
Attached Image
TSGOPI56
post Oct 19 2016, 09:48 PM

Regular
******
Senior Member
1,396 posts

Joined: Dec 2012
I tried eyerything but still not working. What is wrong ? Here is my table columns:

user posted image



otakotak
post Oct 19 2016, 09:55 PM

Getting Started
**
Junior Member
278 posts

Joined: Nov 2010


printing mysql_error() when $result == false will give u the answer
TSGOPI56
post Oct 20 2016, 09:04 AM

Regular
******
Senior Member
1,396 posts

Joined: Dec 2012
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
post Oct 20 2016, 09:47 AM

New Member
*
Junior Member
35 posts

Joined: May 2013
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
post Oct 20 2016, 10:09 AM

Regular
******
Senior Member
1,249 posts

Joined: Feb 2008
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
post Oct 20 2016, 11:32 AM

New Member
*
Junior Member
14 posts

Joined: May 2015
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
post Oct 20 2016, 12:38 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
[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
post Oct 20 2016, 12:53 PM

Regular
******
Senior Member
1,396 posts

Joined: Dec 2012
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
post Oct 20 2016, 01:39 PM

On my way
****
Junior Member
557 posts

Joined: Jul 2011


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
post Oct 20 2016, 01:44 PM

New Member
*
Junior Member
14 posts

Joined: May 2015
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
post Oct 20 2016, 02:31 PM

Look at all my stars!!
*******
Senior Member
3,740 posts

Joined: Mar 2009
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.php
QUOTE
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.


stevenlai84
post Oct 20 2016, 09:48 PM

New Member
*
Junior Member
11 posts

Joined: May 2012


simply change this line=> $result = mysql_query($connection, $query);
to this => $result = mysql_query($query,$connection);
TSGOPI56
post Oct 20 2016, 11:04 PM

Regular
******
Senior Member
1,396 posts

Joined: Dec 2012
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>


user posted image

2 Pages  1 2 >Top
Topic ClosedOptions
 

Change to:
| Lo-Fi Version
0.0225sec    0.46    6 queries    GZIP Disabled
Time is now: 29th March 2024 - 03:38 PM