Kaloyan Dimov
Kaloyan Dimov

Reputation: 57

Error: Query was empty

$nam=$_POST['name'];
$fname=$_POST['family'];
$dat=$_POST['date'];
$bal=$_POST['balance'];
$curr=$_POST['currency'];
$con=mysql_connect('localhost', 'xxxx', 'xxxx', 'xxxx');
$db=mysql_select_db('users',$con);
$ins=mysql_query("INSERT INTO users (Name, FamilyName, Date, Balance, Currency) VALUES  ('$nam', '$fname', '$dat', '$bal', '$curr'))",$con);
 if (!mysql_query($ins,$con))
{
 die('Error: ' . mysql_error($con));
}

So guys, I got this code and I am trying to do something like a registration form. I have tripple checked the names of the variables and the query itself is working when executed in SQL database. The thing is when I include it in my php script it returns that the Query was empty. I've looked around but all errors on the Web are around not assigning to a variable or having several insert statements and so on. So my question is why am i getting this when I am actually inputting data from a web form? Error: Query was empty

P.S. Ok so what I mde of this: I removed the check that you said was for a second time that is the if (!mysql_query($ins,$con)) { die('Error: ' . mysql_error($con)); } part now i get execution but it does not really add the entry to the database and i cannot call it. That is the new name.

Upvotes: 2

Views: 138

Answers (2)

Aditya Vikas Devarapalli
Aditya Vikas Devarapalli

Reputation: 3473

this is incorrect

 if (!mysql_query($ins,$con))

why are you performing a query of a query ??

just use if (!$ins||!$con)) if you are trying to check if the query and connection has been successful

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76656

You're basically trying to use mysql_query() twice:

$ins=mysql_query("INSERT INTO users (Name, FamilyName, Date, Balance, 
Currency) VALUES  ('$nam', '$fname', '$dat', '$bal', '$curr'))",$con);

if (!mysql_query($ins,$con))
{

$ins will contain a valid MySQL resource if the query was executed correctly, but you're attempting to use it again in the if condition.

Just remove the mysql_query() part from the condition, like so:

if(!$ins) {
    # code ...
}

That should fix this particular issue. But note that your code is vulernable to SQL injection. Also, mysql_* functions are deprecated and are soon to be removed. I recommend you switch to MySQLi or PDO and start using parameterized queries to be safe.

Upvotes: 3

Related Questions