Why do I keep getting a 500 error with my PHP? - php

I am trying to create an account registration page and when I add a system to check the database and make sure that there are not multiple rows with the same username, I get a 500 error.
Here's the code that works:
<?php
if(isset ($_POST['submit']))
{
include( 'connection.php' );
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(empty($username) || empty($email) || empty($password))
{
echo 'Please check the required fields.';
}
elseif(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
echo 'Please enter a correct email address.';
}
else
{
$password = md5($password);
$sql = mysql_query("INSERT INTO users (email,username,password) VALUES ('$email','$username','$password')") or die(mysql_error());
if($sql)
{
echo 'Successfully submitted.';
}
}
}
?>
Here's the code that gives me a 500 error:
<?php
error_reporting(E_ALL)
if(isset ($_POST['submit'])) {
include( 'connection.php' );
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$fetch = mysql_query("SELECT * FROM users WHERE username = '$email'") or die(mysql_error();
$num_rows = mysql_num_rows($fetch);
if(empty($username) || empty($email) || empty($password)) {
echo 'Please check the required fields.';
}
elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
echo 'Please enter a correct email address.';
}
elseif($num_rows >= 1);
{
echo 'This username is taken.';
else
}
$password = md5($password);
$sql = mysql_query("INSERT INTO users (email,username,password) VALUES ('$email','$username','$password')") or die(mysql_error());
if($sql)
{
echo 'Successfully submitted.';
}
}
}
?>

Maybe add a semi colon:
error_reporting(E_ALL);
EDIT:
You are also missing a ) on die(mysql_error();
Remove the ; from elseif($num_rows >= 1);
And then fix your else block to be } else {

You missed a semi-colon off your first statement

you have
else }
in your code. That's a syntax error. You're looking for
}
else {
instead.

Related

Mysql query fails on remote server but works on xampp

i have a php script that works well on xampp server, but when I upload to my online server it fails to execute the mysql_query always. weird part is that it works well offline. the registration script always returns 'failure!' on my online server.
i have downgraded the php version on my server to 5.4 but still no luck.
this is the part of the script that returns the error string:
$test = mysql_query("INSERT INTO users(fname,lname,email,city,gender,password) VALUES('$fname','$lname','$email','$city','$gender','$passwordhash')");
if($test) {
echo "Registration successful";
$last_id = mysql_insert_id();
Registration_WelcomeMessage($fname,$email);
}else{echo "failure!";}
and this is the full code:
<?php
require_once 'db.php';
require_once 'utilities/cleaner.php';
require_once 'reg_welcome_message.php';
if(isset($_POST)){
$broom = new cleaner();
$fname = $broom->clean($_POST['fname']);
$lname = $broom->clean($_POST['lname']);
$email = $broom->clean($_POST['email']);
$city = $broom->clean($_POST['city']);
$gender = $broom->clean($_POST['gender']);
$password = $broom->clean($_POST['password']);
$cpassword = $broom->clean($_POST['cpassword']);
$count = 2;
if ($fname && $lname && $email && $city && $gender && $password && $cpassword) {
if (preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) {
if (strlen($password) > 5) {
if ($password == $cpassword) {
$remail = mysql_query("SELECT email FROM users WHERE email='$email'");
$checkemail = mysql_num_rows($remail);
if ($checkemail > 0) {
echo "This email is already registered! Please type another email...";
} else {
$passwordhash = hash('sha1',$password);
$test = mysql_query("INSERT INTO users(fname,lname,email,city,gender,password) VALUES('$fname','$lname','$email','$city','$gender','$passwordhash')");
if($test) {
echo "Registration successful";
$last_id = mysql_insert_id();
Registration_WelcomeMessage($fname,$email);
}else{echo "failure!";}
}
} else {
echo "Your passwords don't match!";
}
} else {
echo "Your password is too short! A password between 6 and 15 charachters is required!";
}
}else{
echo "Please use a valid email!";
}
} else {
echo "You have to complete the form!";
}
}else{echo "NO data sent";}
?>

Php mysqli_fetch_array is no working

I tried to create a register login feature in Android app. I tried to send data from my app to database by using this Php file. However, I received br/ error when I clicked register button on my app. Look like mysqli_fetch_array is not working. Anyone know how to solve this problem? Thanks!
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if($name == '' || $username == '' || $password == '' || $email == ''){
echo 'please fill all values';
}else{
require_once('dbConnect.php');
$sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";
$result=mysqli_query($con,$sql);
$check = mysqli_fetch_array($result,MYSQLI_BOTH);
if(isset($check)){
echo 'username or email already exist';
}else{
$sql = "INSERT INTO users (name,username,password,email) VALUES('$name','$username','$password','$email')";
if(mysqli_query($con,$sql)){
echo 'successfully registered';
}else{
echo 'oops! Please try again!';
}
}
mysqli_close($con);
}
} else{
echo 'error';
}
dbConnect.php
<?php
define('HOST','localhost');
define('USER','username');
define('PASS','password');
define('DB','database');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
I think you should remove isset check. It'll always return true no matter what. Do count check instead.
if(count($check)){
echo 'username or email already exist';
}else{
$sql = "INSERT INTO users (name,username,password,email) VALUES('$name','$username','$password','$email')";
if(mysqli_query($con,$sql)){
echo 'successfully registered';
}else{
echo 'oops! Please try again!';
}
}

PHP getting error message when i try to register with php page in android

PFB code. DB connection is success but when try to register with register.php script in my android phone getting error page or getting please fill all values. I am not getting success. Kindly help. I am also using the URL register url in my java code as well http://xxxx/Userregistration/register.php.
register.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if ($name == '' || $username == '' || $password == '' || $email == '') {
echo 'please fill all values';
} else {
require_once('dbConnect.php');
$sql = "SELECT * FROM KumbhaApp WHERE username='$username' OR email='$email'";
$check = mysqli_fetch_array(mysqli_query($con, $sql));
if (isset($check)) {
echo 'username or email already exist';
} else {
require_once('dbConnect.php');
$sql = "INSERT INTO KumbhaApp (name,username,password,email) VALUES('$name','$username','$password','$email')";
}
}
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = "Only letters and white space allowed";
} else {
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
$emailErr = "Invalid email format";
}
}
if (mysqli_query($con, $sql)) {
echo 'successfully registered';
header('Location: securedpage.php');
} else {
echo 'oops! Please try again!';
}
mysqli_close($con);
}
?>
It is not required, it is require. Your code
required ("http://localhost:8080/UserRegistration/dbConnect.php");
^
is wrong.

My php is not entering the values into the sql database, what is wrong with my code?

I'm having this problem where I have a registration form and I am using PHP and MySQL. The problem is that even when all the data is valid it wont enter the information into the database. I know the database is connected because I can use it with the login part of my website. I think it is the problem with the email and username cross check against the database but I am not sure. Is the positioning of the curly braces or alot more complex?
<?php
include_once('db.php');
$name = mysql_real_escape_string( $_POST["name"] );
$username = mysql_real_escape_string( ($_POST["username"]) );
$password = mysql_real_escape_string( md5 ($_POST["password"]) );
$repeatpassword = mysql_real_escape_string( $_POST['repeatpassword'] );
$email = mysql_real_escape_string( $_POST["email"] );
$confirmemail = mysql_real_escape_string( $_POST['confirmemail'] );
// the below if statement is for when the user does NOT have JS enabled in their browser
if(empty($name) || empty($username) || empty($password) || empty($email)){
echo "(*) indicate that the fields are mandatory.";
exit();
}
if($email == $confirmemail){
exit();
}else{
echo "Your Email address does not match.";
}
if($email == $repeatpassword){
exit();
}else{
echo "Your Passwords do not match.";
exit();
}
$res = mysql_query("SELECT username FROM users WHERE username='$username'");
$row = mysql_fetch_row($res);
$res1 = mysql_query("SELECT email FROM users WHERE email='$email'");
$row1 = mysql_fetch_row($res1);
if( $row > 0 ){
echo nl2br("The username $username is already in use");
}else{
if( $row1 > 0 ){
echo nl2br("the email address $email is already in use");
}else{
$sql = "INSERT INTO users VALUES('','$name', '$username', '$password', '$email')";
if( mysql_query($sql) ){
echo "Inserted Successfully";
}else{
echo "Insertion Failed";
}
}
}
?>
if($email == $confirmemail) {
exit();
}
else {
echo "Your Email address does not match.";
}
So what you're doing in the above code is "if email and confirmation email are the same, stop the script execution else print out 'Your Email address does not match.' and continue execution".
if ($email == $repeatpassword) {
exit();
}
else {
echo "Your Passwords do not match.";
exit();
}
And here you are saying if "email and repeatpassword are the same (???), stop script execution else print out 'Your Passwords do not match.' and also stop script execution".
So because of this logic obviously you never reach the code to insert data to database.

why won't this page redirect?

I'm having a problem with redirecting a page in php.
<?php
include '../include/dbfunctions.php';
$email = $password = "";
$err = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['login']) && !empty($_POST['password'])) {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$link = get_db_connection();
if (mysqli_connect_errno()) {
die(" Something went wrong ! ");
}
$user_email = mysqli_real_escape_string($link, $email);
$user_password = mysqli_real_escape_string($link, $password);
$query = "SELECT username FROM user WHERE user_email = '$user_email' AND user_password = SHA1('$user_password') AND user_active = '1';";
$data = mysqli_query($link, $query);
if (mysqli_num_rows($data) == 1) {
$row = mysqli_fetch_array($data);
$username = $row['username'];
mysqli_close($link);
if (!empty($username)) {
header('location:http://www.xxxxxxxxxxxxxx.be/login/dashboard.php');
exit();
}
} else {
$err = "Invalid combination of e-mail and password";
echo $err;
}
} else {
}
}
?>
I can't figure it out. If i fill in an invalid password or email, i get the error message. But when they are correct, nothing happens.
if (!empty($username)) {
header('location:http://www.yoursite.be/login/dashboard.php?error=error in login please try agine');
exit();
}
if (!empty($username)) {
header('location:http://www.xxxxxxxxxxxxxx.be/login/dashboard.php');
exit();}
$username might be empty.

Categories