Rybat Posted August 26, 2018 Share Posted August 26, 2018 (edited) 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 August 26, 2018 by Barand Code tags added Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/ Share on other sites More sharing options...
Barand Posted August 26, 2018 Share Posted August 26, 2018 (edited) 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 August 26, 2018 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560530 Share on other sites More sharing options...
Rybat Posted August 26, 2018 Author Share Posted August 26, 2018 My display_errors are ON Any other possible way it is not inserting data and also not displaying errors. Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560531 Share on other sites More sharing options...
Barand Posted August 26, 2018 Share Posted August 26, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560532 Share on other sites More sharing options...
ginerjm Posted August 26, 2018 Share Posted August 26, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560534 Share on other sites More sharing options...
Barand Posted August 26, 2018 Share Posted August 26, 2018 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(). Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560535 Share on other sites More sharing options...
Rybat Posted August 27, 2018 Author Share Posted August 27, 2018 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.... Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560541 Share on other sites More sharing options...
mac_gyver Posted August 27, 2018 Share Posted August 27, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560542 Share on other sites More sharing options...
ginerjm Posted August 27, 2018 Share Posted August 27, 2018 What part of the manual did you not understand when I suggested you read up on prepare/execute usage? (hint, hint....) Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560545 Share on other sites More sharing options...
Rybat Posted August 27, 2018 Author Share Posted August 27, 2018 (edited) 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 August 27, 2018 by Rybat Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560558 Share on other sites More sharing options...
Rybat Posted August 27, 2018 Author Share Posted August 27, 2018 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560559 Share on other sites More sharing options...
ginerjm Posted August 27, 2018 Share Posted August 27, 2018 If you can't read the manual and understand it, I think you should stop. Or try reading and applying some thought to it. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560560 Share on other sites More sharing options...
mac_gyver Posted August 28, 2018 Share Posted August 28, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560561 Share on other sites More sharing options...
Rybat Posted August 28, 2018 Author Share Posted August 28, 2018 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! Quote Link to comment https://forums.phpfreaks.com/topic/307644-php-insert-code-not-working/#findComment-1560572 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.