1

I have created a user register page, with a HTML form. I have just finished the PHP file to submit the data to a MySQL database.

I have checked that phpMyAdmin is connected to the database and working.

As far as I can see, the MySQL queries and the rest of the PHP code all seems fine, however I am getting an internal error on the page with the PHP code. I have used the include function to include the PHP file on the HTML file, and both the HTML register page and the PHP file return the following error on Chrome:

"The website encountered an error while retrieving [WEB ADDRESS]. It may be down for maintenance or configured incorrectly."

I should also note that it is only this page that is doing it, other pages on the website work fine, so it is defiantly this PHP file. I would paste the code but it's pretty long. I've never gotten a plain 500 error with a PHP file before, I have made similar sign up forms and codes before and never had this problem.

Any help would be much appreciated.

Code

<?php
error_reporting(E_ERROR);

//Errors
$password_error = "<div class='login-form-error'>Your passwords didn't match, be careful this time!</div>";
$field_error = "<div class='login-form-error'>You cannot leave any fields blank or use an invalid character.</div>";
$username_error = "<div class='login-form-error'>That username is already taken, make another one.</div>";
$characters_error = "<div class='login-form-error'>Your username cannot contain any special characters.</div>";
$email_error = "<div class='login-form-error'>Your emails didn't match, be careful next time!</div>";
$signedup = "<center><h1>Welcome to the club!</h1><span style='font-size: 24px;'>Your account is all ready, <a href='/client/sign-in' style='color: #212121; text-decoration: underline;'>Sign In</a> and set up your profile.</span></center>";
$max_length_error = "<div class='login-form-error'>Your first name or username cannot contain more than 25 characters.</div><br>";
$max_length_password_error = "<div class='login-form-error'>Your password must be between 8 and 30 characters long.</div>";

$reg =@$_POST['reg'];
//Declaring variables to prevent errors
$first_name = "";
$gender = "";
$age = "";
$username = "";
$password = "";
$password2 = "";
$email = "";
$email2 = "";
$date = "";
$u_check = "";

//registration form
$first_name =  mysql_real_escape_string(strip_tags(stripslashes(@$_POST['first_name'])));
$gender =  mysql_real_escape_string(strip_tags(stripslashes(@$_POST['gender'])));
$age =  mysql_real_escape_string(strip_tags(stripslashes(@$_POST['age'])));
$username = mysql_real_escape_string(strip_tags(stripslashes(@$_POST['username'])));
$password = mysql_real_escape_string(strip_tags(stripslashes(@$_POST['password'])));
$password2 = mysql_real_escape_string(strip_tags(stripslashes(@$_POST['password2'])));
$email = mysql_real_escape_string(strip_tags(stripslashes(@$_POST['email'])));
$email2 = mysql_real_escape_string(strip_tags(stripslashes(@$_POST['email2'])));
$d = date("Y-m-d"); // Year - Month - Day

if ($reg) {
if ($email==$email2) {
//Check if user already exists
$u_check = mysql_query("SELECT `username` FROM `users` WHERE `username`='$username'") or die("MySQL ERROR: ".mysql_error());
//Count the amount of rows where username = $username
$check = mysql_num_rows($u_check);
if ($check == 0) {
//Check all of the fields have been filled in
if ($first_name&&&gender&&age&&$username&&$password&&$password2&&$email&&$email2) {
//Check passwords match
if ($password == $password2) {
//check username characters
if (!ctype_alnum($username)){
  echo $characters_error;
}
else {
//check max length un fn ln
if (strlen($username)>25||strlen($first_name)>25) {
  echo $max_length_error;
}
else
{
//Check max length pw
if (strlen($password)>30||strlen($password)<8) {
  echo $max_length_password_error;
}
else
{
//encrypt
$password = md5($password);
$password2 = md5($password);
$query = mysql_query("INSERT INTO users VALUES ('','$username','$password','$email','$date','$first_name','$age','$gender','','profile_picture_here','banner_picture_here','','variables_next','0','','0','0','0','0','0','0')") or die("MySQL ERROR: ".mysql_error());
die($signedup);
}
}
}
}
else {
echo password_error;
}
}
else
{
echo field_error;
}
}
else
{
echo username_error;
}
}
else
{
echo email_error;
}
}
?>
  • 5
    Check your errorlogs. – putvande Feb 05 '16 at 21:24
  • 1
    Hi and welcome to SO, please add the code so we can see what might be wrong. Please read [ask] and [mcve] to ask a better received question. – davejal Feb 05 '16 at 21:24
  • I'd recommend changing the error_reporting to the most broad possible (E_ALL) and see if you get any php errors to handle. http://php.net/manual/en/function.error-reporting.php – Andy Arndt Feb 05 '16 at 21:24
  • Check the error logs if you have access. That should tell you what the problem is and where it is located; from there you should be able to resolve it or provide a minimal example of the issue. – chris85 Feb 05 '16 at 21:28
  • 1
    PHPMyAdmin *is not* a database. It is a web interface for your MySQL database. – Jay Blanchard Feb 05 '16 at 21:28
  • @putvande I've tried locating them in cPanel but I can't find them. –  Feb 05 '16 at 21:47
  • @davejal Added the code –  Feb 05 '16 at 21:48
  • @JayBlanchard I am aware of that, sorry for not making that clear. The database has been created and connected to the website. –  Feb 05 '16 at 21:48
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Feb 05 '16 at 21:51
  • @JayBlanchard I'll look into that, I doubt that's the reason why my file isn't working though, my PHP version is 5.5. Any idea why the 500 error is occurring? –  Feb 05 '16 at 21:58
  • 1
    It looks like you have three `&` in your last if statement. `&&&` is not valid PHP. Should probably have been `$gender` instead of `&gender` – putvande Feb 05 '16 at 22:01
  • @putvande Just fixed that and I think you just solved the issue. No longer getting the error, why did that stop the entire code from loading though? Usually in-line errors are displayed with a message saying 'error on line X'. –  Feb 05 '16 at 22:03
  • I would also highly suggest indentation and whitespace to make your code a lot more readable. Easier to spot mistakes this way. – putvande Feb 05 '16 at 22:05

1 Answers1

0

You just have a typo on line 46 you wrote '&&&' instead of '&&' it should work then.

if ($first_name&&&gender&&age&&$username&&$password&&$password2&&$email&&$email2) {

should be

if ($first_name&&$gender&&$age&&$username&&$password&&$password2&&$email&&$email2) {

I think.

Greetings Joe

Joe Pi
  • 372
  • 3
  • 13
  • Sure that is a mistake but not sure if that would cause a 500 error, would it? – putvande Feb 05 '16 at 22:02
  • 1
    Yes. The 500 error means while compiling your PHP code it produced an error and as &&& is not valid PHP it will produce an error. As you forgot the $ before age as well it should give you another error as age is not declared because you mean the variable $age. – Joe Pi Feb 05 '16 at 22:04
  • @putvande Exactly my thoughts but I fixed that and the error has gone so I guess it did. Thank you both :) –  Feb 05 '16 at 22:06
  • If you are working on a locally running server you should activate the PHP errors in your php.ini file so you will see what your mistake is instead of the 500 error and the need to lock in the php error log file. – Joe Pi Feb 05 '16 at 22:06
  • @JoePi I did that with the $gender variable too. Fixed them both, it's because I'm using a new text editor and this one doesn't highlight variables so when I glance over the & looks like $. –  Feb 05 '16 at 22:07