Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Barrett
13,253 Pointserror_message variable not displaying the string value on form submit
Hi there,
My database validation is working; the 'User' table will not update if there is already a registered user. However, my $error_message variable will not display the the error message string. Here is my code:
HTML/PHP:
<?php
// Collect and validate user inputs
if($_SERVER["REQUEST_METHOD"] == "POST") {
session_start();
$forename = trim(filter_input(INPUT_POST,"user_forename",FILTER_SANITIZE_STRING));
$surname = trim(filter_input(INPUT_POST,"user_surname",FILTER_SANITIZE_STRING));
$gender = trim(filter_input(INPUT_POST,"user_gender",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"user_email",FILTER_SANITIZE_EMAIL));
$password = trim(filter_input(INPUT_POST,"user_password"));
$city = trim(filter_input(INPUT_POST,"user_city"));
$team = trim(filter_input(INPUT_POST,"user_team",FILTER_SANITIZE_STRING));
$bio = trim(filter_input(INPUT_POST,"user_bio",FILTER_SANITIZE_SPECIAL_CHARS));
$human = trim(filter_input(INPUT_POST,"user_human",FILTER_SANITIZE_STRING));
$userExist = $db->query("SELECT * FROM User WHERE U_Email='$email'");
if($forename == "" || $surname == "" || $email == "" || $password == ""
|| $city == "" || $team == "" || $bio == "" || $human == "") {
$error_message = "Please fill in all form fields";
}
if (!isset($error_message) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message = "$email is a not a valid email address";
}
if (!isset($error_message) && (mysql_num_rows($userExist) > 0)) {
$error_message = "$email is already taken!";
}
if(!isset($error_message)) {
$sql = $db->query("INSERT INTO User (U_Forename, U_Surname, U_Gender, U_Email, U_Password, U_City, U_Team, U_Biography)
VALUES('{$forename}', '{$surname}', '{$gender}', '{$email}', '{$password}', '{$city}', '{$team}', '{$bio}')");
// header('Location: index.php');
}
}
<div class="wrapper">
<h1>Register, it's free!</h1>
<div>
<?php
if (isset($error_message)) {
echo "<h2>".$error_message."</h2>";
}
?>
</div>
The last piece of code is the part not executing correctly. I cannot capture any error messages so I do not know to do.
Any suggestions would be great.
Thanks, James.
1 Answer
Thomas Dimnet
Python Development Techdegree Graduate 43,629 PointsDid you try to use the var_dump() function? I think you'll see your error more clearly (I do not try to run your code yet).