Jump to content

PHP Insert code not working


Rybat

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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>";
    
   
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.