Rybat
August 26, 2018, 8:18pm
1
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>
Rybat:
mysql: h
This doesnât look right.
Rybat
August 26, 2018, 8:48pm
3
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
Rybat
August 26, 2018, 9:15pm
5
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.
Rybat
August 26, 2018, 9:22pm
6
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.
Rybat
August 27, 2018, 7:00am
10
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;
}
?>```
Rybat
August 27, 2018, 7:04am
11
Tried it an error occurred
You most likely missed a curly brace when you inserted that else and line into your code.
Rybat
August 27, 2018, 7:42am
13
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;
}
}
}
?>```
Rybat:
mysql: h
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.
Rybat
August 27, 2018, 9:41pm
19
spaceshiptrooper:
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.
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.
Rybat
August 27, 2018, 9:43pm
20
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