PHP Insert code not working PDO

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

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 code with html

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

This doesn’t look right.

Why??? cause that was my mysql connection and no error was on it, and then i proceeded to the next step of inserting data. which no data inserted and a blank page on hitting signup button.

Why? I’m guessing because it isn’t like the example.

http://php.net/manual/en/pdo.construct.php

<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

I’m surprised your not getting errors or exceptions. Especially with the INSERT using a keyword (i.e. “password”)

https://dev.mysql.com/doc/refman/8.0/en/keywords.html

Possible solution?? cause I am new to PHP.

Is it my database connection which is has an issue??? or is it my insert code.

when trying to connect to database it connected successfully upon putting the insert code, no changes were seen only the blank page and no errors.

changed my database connection to

$dsn = 'mysql:dbname=user_login; host=127.0.0.1';
$username = "root";
$password = "";

try {
	//creating connection to mysql
    $dbh = new PDO($dsn, $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;
    }
?>

still no difference still the same thing.

No no no. That’s not how you’re supposed to create the connection. You had it almost right the first time. This second time, it’s not even correct.

It’s actually possible. I’ve used them for my test databases before.

Without enclosing them so they would be interpreted as strings instead of keywords?

@Rybat try this

  	if ($statement ->rowCount() == 1){
 		$result = "<p style='color:green;'>Registration Successful</p>";
 	} else { 
        $result = "<p style='color:green;'>" . $statement->rowCount() . "</p>";
    }
	catch(PDOException $e)
    {
    $result = "<p style='color:green;'>An error occured: " . $e->getMessage() . "</p>";
    exit;
    } 

Yup. Without enclosing them.

so is this the right way???

$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;
    }
?>```

Tried it an error occurred

You most likely missed a curly brace when you inserted that else and line into your code.

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

 	} else { 

        $result = "<p style='color:green;'>" . $statement->rowCount() . "</p>";
    }
	catch(PDOException $e)
    {

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


?>```

No. This right here. PHP is exact. If you don’t do it the way you’re supposed to write it, then unexpected behaviors may occur.

No. The unexpected error is referring to you randomly using the catch block right after you use an else statement. catch blocks has to come immediately right after a try block.

2 Likes

You get a blank screen when you hit ‘submit’ because, after you’ve tried to insert the new record, although you set the $result variable, you also call exit() before you get as far as displaying it. So if your catch clause traps an error, it won’t show it.

2 Likes

The catch block will also complain because the curly brace is being wrapped around the wrong line. Not only that, there really is no output function being used for $result so either way, nothing will be displayed.

What should i do then…

Should i remove remove all the try/catch logic I 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.

Please I need help here guys i think I still don get it???

Even with all the help you are getting on the other forum?
https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/

Perhaps you should set your code aside and spend some time with this PDO tutorial and learn the basics. https://phpdelusions.net/pdo