Jump to content

Query was empty


bscyb

Recommended Posts

hi 1st of all im new on PHP so i dont know allot of things

i created a form via php with that i delete 1 record of my database on mysql

the php script is this one:

 

<?php

 

$con=mysql_connect("localhost","developer","javalab");

mysql_query("SET NAMES UTF8");

if(!$con)

{

  die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());

}

$tID = $_POST["tID"];

mysql_select_db("cycladestravel", $con);

mysql_query("DELETE FROM travels WHERE travel_id=$tID");

 

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

{

  die('Σφάλμα: ' . mysql_error());

}

 

echo " 1 εγγραφή διεγράφη ";

 

mysql_close($con)

?>

 

 

when i put 1 travel_id on form and press submit the record on mysql is deleted but i get the error Query was empty

Link to comment
Share on other sites

this line:

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

is failing because there is no $sql variable defined.

 

there are also some other strange things with the order of your lines, try creating a little function to handle all sql conections, where you pass the database name into, something like this: (it keeps things nice and tidy and allows you to re-use the code)

function conn($db_name){
	$db_host = "localhost";
	$db_user = "developer";
	$db_pass = "javalab";
	$c = mysql_connect($db_host, $db_user, $db_pass) or die('Cannot connect to sql server '.mysql_error());
	mysql_select_db($db_name,$c)or die('Cannot select database '.mysql_error());
	return $c;
}

 

then what you do is something like this:

$con = conn('database_name');
$result = mysql_query("delete from `travels` where `travel_id`='$tID' limit 1",$con);
@mysql_close($conn);
if(!$result){
echo 'error';
}else{
echo 'success';
}

Link to comment
Share on other sites

old code

 

		
$db_host = "localhost";		
$db_user = "developer";		
$db_pass = "javalab";

 

new code

 

		
$db_host = "localhost";		
$db_user = "developer";		
$db_pass = "javalab";
$db_name = "cycladestravel";            // added the database name variable

 

 

You will have to try to with a $tID =value one that you either put there or know is there for testing.

 

so let say

 

$tID = '1234';

$con = conn('database_name');
$result = mysql_query("DELETE `*` FROM `travels` WHERE `travel_id`='$tID' LIMIT = '1' ",$con);
@mysql_close($conn);
if(!$result){ 
echo 'error';
}else{
echo 'success';
}

Link to comment
Share on other sites

also not working dont tell me again about the conn function i dont need it i have another script in which i add a record to a database and i also not use a fuction there and this is working correct

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('connection error'.mysql_error());
}

mysql_select_db("cycladestravel", $con);

 

i only need this to make working

 

$tID = '1234';

$con = conn('database_name');
$result = mysql_query("DELETE `*` FROM `travels` WHERE `travel_id`='$tID' LIMIT = '1' ",$con);
@mysql_close($conn);
if(!$result){ 
echo 'error';
}else{
echo 'success';
}

that also not work

Link to comment
Share on other sites

tell us the error you are getting then more info needed as you speak bad english

 

 

Re: Query was empty

 

« Reply #2 on: Today at 10:19:01 AM »

 

Quote

 

 

the 1st with  the function conn is not working

 

 


if (!mysql_query($sql, $con))
{
echo " 1 εγγραφή διεγράφη ";
}
die('Σφάλμα: ' . mysql_error());

 

Link to comment
Share on other sites

This will solve it then if your code works

 

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
mysql_query("DELETE FROM travels WHERE travel_id=$tID");

if (!mysql_query($sql, $con))
{
echo " 1 εγγραφή διεγράφη ";
}
   die('Σφάλμα: ' . mysql_error());

}
mysql_close($con)
?>

 

No need to be rude we are trying to help you for free so be polite or  :rtfm:

Link to comment
Share on other sites

sorry you misunderstand me i was not rude i just wanted to tell that the function was not needed

 

about the code its even not working i get message that the delete was succeed error query was empty even if i add a travel_id that exist even 1 that not exist

Link to comment
Share on other sites

I was looking at the wrong post after reading was the trouble I did not scroll all the way up to the top like i thought i did for the code on my first post.

 

Your connection works so we need to fix the query to work ?

You said the mysql query deleted your row ?

Or does your query only delete the email address ?

 

If it is deleting the row then change the error message around the other way and it will be right.

If its just deleting the email address then use * after DELETE

 

Link to comment
Share on other sites

the query works fine it deletes the whole record

i just dont want to get the error query is empty

if the record exists and gets deleted i wanna get the succeed message

if the record doesn't exist i wanna get the error message

Link to comment
Share on other sites

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
$result = mysql_query("DELETE `*` FROM `travels` WHERE `travel_id`='$tID' LIMIT = '1' ",$con);
@mysql_close($conn);
if(!$result){ 
echo 'success';
}else{
echo 'error';
}
?>

 

this is not working i always got the succeed message but i doesn't delete anything

Link to comment
Share on other sites

lets try this and make it easy post the code that is working properly with the wrong error message please.

 

 

If this is your working code which I do not know why its working with a missing curlybrase then use what I have put below

 

 

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die(' my error message '.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
mysql_query("DELETE FROM travels WHERE travel_id=$tID");

if (!mysql_query($sql, $con))
{
  echo " works great"; 
}
die(' my error message : ' . mysql_error());
}
mysql_close($con)
?>

Link to comment
Share on other sites

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
$result = mysql_query("delete from `travels` where `travel_id`='$tID' limit 1",$con);
@mysql_close($conn);
if(!$result){
echo 'error';
}else{
echo 'success';
}

?>

the only problem on that code

is even if i put a travel_id that not exist it return the success message

Link to comment
Share on other sites

But you are not reading what i am saying are you  read this  "REVERSE THE ERROR MESSAGE ONLY"

This what when it is wrong it will be wrong and when its right it will say its right there is no big mystery in this bit of code if it works and puts the wrong error mesage up then reverse it.

 

Find

echo 'success'; 

 

replace with

echo 'error';

 

 

Find

echo 'error';

 

replace with

echo 'success'; 

Link to comment
Share on other sites

This code here is correct

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
$result = mysql_query("delete from `travels` where `travel_id`='$tID' limit 1",$con);
@mysql_close($conn);
if(!$result){
echo 'error';
}else{
echo 'success';
}

You need to use mysql_affected_rows to check whether the query actually deleted a row from your database.

This line

if(!$result){

Will only check to see if mysql_query did not return false. mysql_query will only return false when there is a problem with the query.

Link to comment
Share on other sites

This code here is correct

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
$result = mysql_query("delete from `travels` where `travel_id`='$tID' limit 1",$con);
@mysql_close($conn);
if(!$result){
echo 'error';
}else{
echo 'success';
}

You need to use mysql_affected_rows to check whether the query actually deleted a row from your database.

This line

if(!$result){

Will only check to see if mysql_query did not return false. mysql_query will only return false when there is a problem with the query.

 

this is also not working correct i get the success  and then die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());

both times even i put a existing travel_id and even i put a not existing travel_id

Link to comment
Share on other sites

this is also not working correct i get the success  and then die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());

both times even i put a existing travel_id and even i put a not existing travel_id

I have not modified your code. I have suggested that use the mysql_affected_rows() function when checking to see if your query has deleted a record from your database.

Link to comment
Share on other sites

The error you're getting is from this code:

mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());
}

 

It isn't coming from the DELETE query, from what I can tell.

Link to comment
Share on other sites

this is also not working correct i get the success  and then die('Δεν έγινε η σύνδεση με την βάση δεδομένων'.mysql_error());

both times even i put a existing travel_id and even i put a not existing travel_id

I have not modified your code. I have suggested that use the mysql_affected_rows() function when checking to see if your query has deleted a record from your database.

ye but i have no idea how to use mysql_affected_rows()

Link to comment
Share on other sites

ye but i have no idea how to use mysql_affected_rows()

Have a read of the manual on mysql_affected_rows.

i put it so but its not working

 

<?php

$con=mysql_connect("localhost","developer","javalab");
mysql_query("SET NAMES UTF8");
if(!$con)
{
   die('&#916;&#949;&#957; &#941;&#947;&#953;&#957;&#949; &#951; &#963;&#973;&#957;&#948;&#949;&#963;&#951; &#956;&#949; &#964;&#951;&#957; &#946;&#940;&#963;&#951; &#948;&#949;&#948;&#959;&#956;&#941;&#957;&#969;&#957;'.mysql_error());
}
$tID = $_POST["tID"];
mysql_select_db("cycladestravel", $con);
$result = mysql_query("delete from `travels` where `travel_id`='$tID' limit 1",$con);
@mysql_close($conn);
if(!$result){
echo 'error'.mysql_affected_rows();
}else{
echo 'success'.mysql_affected_rows();
}

?>

Link to comment
Share on other sites

all he is trying to do is get a good message when it has deleted not if it has deleted he knows its deleting it.

So swapping the error message around would make a false positive a positive false.

He did not say he wanted to know if it has deleted anything from the mysql just to get a good message when the right id is used.

As he knows the rows are being deleted.

 

if(mysql_query($result,$con)){
        echo "$i  encountered an error.<br/>";
   } else {
        echo "$i successfully inserted.<br/>";
       }

 

not correct

@mysql_close($conn);

 

correct

mysql_close($con);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.