Jump to content

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
https://forums.phpfreaks.com/topic/238476-query-was-empty/
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225437
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225453
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225462
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225463
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225465
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225473
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225485
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225487
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225491
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225493
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225496
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225497
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225502
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225505
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225507
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225515
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
https://forums.phpfreaks.com/topic/238476-query-was-empty/#findComment-1225519
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.