Jump to content

Recommended Posts

I am making a login system but upon inserting data no response and no errors. Please help!

my database connection code

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "user_login";

try {
    //creating connection to mysql
    $dbh = new PDO("mysql: host=$servername;dbname=user_login", $username, $password);
    // set the PDO error mode to exception
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    exit;
    }
?>

 

my signup.php

<?php
include_once 'resource/database.php';

if(isset($_POST['email'])){
     $email = $_POST['email'];
     $username = $_POST['username'];
     $password = $_POST['password'];

     try {
    $sqlInsert = "INSERT INTO user_login (username, email, password, join_date)
                   VALUES (:username, :email, :password, now())";

     $statement = $dbh->prepare($sqlInsert);
     $statement->execute(array(':username' => $username, ':email' => $email, ':password' => $password,  ));

     if ($statement ->rowCount() == 1){

         $result = "<p style='color:green;'>Registration Successful</p>";

     }
   
    }
    catch(PDOException $e)
    {

    $result = "<p style='color:green;'>An error occured: " . $e->getMessage() . "</p>";

    exit;
    }

     

}

?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Register Page</title>
  </head>

<body>

<h2> User Authenticatication System</h2>

<h3>Registration Form</h3>

<?php if(isset($result)) echo $result; ?>

<form action="" method="POST">
        <table>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email" value=""></td>
            </tr>
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" value=""></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" value=""></td>
            </tr>

        </table>
        <input type="submit" name="submit" value="Signup">
            
        </form>


<p><a href="http://localhost/auth/index.php"> Back to Homepage </a></p>

</body>
</html>

 

what i am missing because there no errors and data is not inserted it give me a blank page upon clicking signup

 

Edited by Barand
Code tags added
Link to comment
https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/
Share on other sites

Have you got PHP's error reporting turned on?

error_reporting should be E_ALL

display_errors should be ON for development, log_errors should be on for production.

Best place to set these is your php.ini file

Edited by Barand

You won't get a response because you don't output one. You set $result but it is not echoed.

Quote

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

The purpose of doing that it so you don't have to use try...catch for every pdo statement.

You need to echo out your result to see the message.

Do you in fact have error checking turned on or have you looked at the error log to see if you have a problem?

Are you REALLY storing your password in plain text?  Silly, silly, silly.

Are you checking your inputs to be sure you have some?

Why are you not checking the result of your prepare call or your execute call?  If you RTFM you would see that they both return values that will tell you if they succeeded?

24 minutes ago, ginerjm said:

Why are you not checking the result of your prepare call or your execute call?  If you RTFM you would see that they both return values that will tell you if they succeeded?

S/he is:

3 hours ago, Rybat said:

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

I agree with you about the passwords though. Should be using password_hash() and password_verify().

I agree with you all about not using password_hash() and password_verify() because this is just a demo, still new to PHP though before I embark on it I wanted to taste what i have first done.

I did echo it <?php if(isset($result)) echo $result; ?> @ginerjmand @Barand

I did this still no difference

PS - If you're posting here you should be using:

        error_reporting(E_ALL);

        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

Why are you not checking the result of your prepare call or your execute call?  If you RTFM you would see that they both return values that will tell you if they succeeded? how do you go about this....

8 minutes ago, Rybat said:

I did echo it <?php if(isset($result)) echo $result; ?>

not for the case of a query error. you have an exit; statement in your code and the rest of the code on the page, where you are echoing it, is never executed.

you need to remove all the try/catch logic you currently have and let php catch the exceptions, where it will use its error_reporting, display_errors, and log_errors settings to control what happens with the actual error information. when learning, developing, and debugging, you should display all php errors. when on a live/public server, you should log all php errors.

the only useful time you should catch and handle a pdo exception in your code is if you are detecting duplicated data being inserted/updated.

 

  • Great Answer 1
9 hours ago, ginerjm said:

What part of the manual did you not understand when I suggested you read up on prepare/execute usage?  (hint, hint....)

I did read it but I was not getting,  it could you send me a link so that I could understand better....Thanks!!

Edited by Rybat
14 hours ago, mac_gyver said:

not for the case of a query error. you have an exit; statement in your code and the rest of the code on the page, where you are echoing it, is never executed.

you need to remove all the try/catch logic you currently have and let php catch the exceptions, where it will use its error_reporting, display_errors, and log_errors settings to control what happens with the actual error information. when learning, developing, and debugging, you should display all php errors. when on a live/public server, you should log all php errors.

the only useful time you should catch and handle a pdo exception in your code is if you are detecting duplicated data being inserted/updated.

I did remove the try/catch logic and it worked. The $result was supposed to be  $result = "<p style='color:green;'>Registration Successful</p>"; but it gave me $result = "<p style='color:green;'>An error occured: </p>";


    Here is the code after removing try /catch

include_once 'resource/database.php';

if(isset($_POST['email'])){

     $email = $_POST['email'];
     $username = $_POST['username'];
     $password = $_POST['password'];

    $sqlInsert = "INSERT INTO users (username, email, password, join_date)
                   VALUES (:username, :email, :password, now())";

     $statement = $dbh->prepare($sqlInsert);
     $statement->execute(array(':username' => $username, ':email' => $email, ':password' => $password,  ));

     if ($statement ->rowCount() == 1){

         $result = "<p style='color:green;'>Registration Successful</p>";

     } else { 

        $result = "<p style='color:green;'>" . $statement->rowCount() . "</p>";
    }
    
    $result = "<p style='color:green;'>An error occured: </p>";
    
   
}

2 hours ago, Rybat said:

The $result was supposed to be  $result = "<p style='color:green;'>Registration Successful</p>"; but it gave me $result = "<p style='color:green;'>An error occured: </p>";

that's because you are unconditionally assigning that string to $result and it comes after the assignment of the success message. remove the An error occurred ... line of code. the exception error handling takes care of producing a query error message.

  • Thanks 1
19 hours ago, mac_gyver said:

that's because you are unconditionally assigning that string to $result and it comes after the assignment of the success message. remove the An error occurred ... line of code. the exception error handling takes care of producing a query error message.

Much thanks for the help!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.