trainmania100 trainmania100 - 2 months ago 17
MySQL Question

mysqli_query(): Empty query in /home/trainman/public_html/Register.php on line 26

I am creating a sign up page.
My code was working perfectly before on an intranet, but now, 5 years later I must use MySQL i.

What happens is I connect to the database using external PHP file, dblogin.php

<?php
$connection = mysqli_connect('mywebhost','username','password','db');
?>


That bit works fine, as the login system works using this.

Then comes my registration system.
It has been a while since I coded in PHP, mostly working using Wordpress now.

<?php
include 'dblogin.php';

if(isset($_GET['i'])){
if($_GET['i'] == '1'){ //if we want to insert a new user
$tblName="tblUsers";

//Form Values into store
$FirstName=$_POST['firstnamecreate'];
$Surname=$_POST['Surnamecreate'];
$Username=$_POST['UsernameCreate'];
$UserType="stu"; //never mind this, it just seperates admins from standard users
$Email=$_POST['EmailCreate'];
$Password=$_POST['PasswordCreate'];



$ExistingUserVerification = mysqli_query ($connection,"SELECT COUNT(*) as num FROM tblUsers WHERE UserName = $Username");

$UserResults = mysqli_query($connection,$ExistingUserVerification);




if($UserResults[0] == 1){

$CreatedStatus = "$Username already exists in the user database. Please choose a different Username.";

}else{



$sql="INSERT INTO $tblName(UserName, Password, UserType, FirstName, Surname, EmailAddress)VALUES('$Username', '$Password', '$UserType', '$FirstName', '$Surname', '$Email')";
$result=mysqli_query($connection,$sql);
if($result){
$CreatedStatus = "$FirstName, you have registered successfully. Click " . "<a href=Login.php>". "HERE". "</a>" . " to login. " . "<br />"."Please note: Hacking of this site is not permitted.";
}
else {
$CreatedStatus = "Unfortunately $Username was not created successfully. Please check your entry or check whether the user already exists.";
}
}
}
}
?>


The problem i am getting is around the

$ExistingUserVerification = mysqli_query ($connection,"SELECT COUNT(*) as num FROM tblUsers WHERE UserName = $Username");

$UserResults = mysqli_query($connection,$ExistingUserVerification);


part.
I have tried all sorts. With the current format, it results in:

Warning: mysqli_query(): Empty query in /home/trainman/public_html/Register.php on line 26

removing $connection results in it expecting 2 parameters and removing i says depricated.

Any help much appriciated. It has been a while since I last used php so sorry if the code is untidy. The select COUNT (*) checks if there is another user with the same username, if there isnt it will submit form values to the DB

Answer

This error is coming from the extremely simple fact that you are sending an empty query to mysqli. The query is empty. It's but an empty string. Nothing.

So just check your variables.

The second parameter to mysqli_query() should be a PHP string contains a legitimate SQL query. Anything else will cause an error.