bscyb Posted June 5, 2011 Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 5, 2011 Share Posted June 5, 2011 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'; } Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 the 1st with the function conn is not working the 2nd is working but even if i put a travel_id that not exist on database i get the succes message Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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'; } Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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()); Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 If the code works reverse the error messages if(!$result){ echo 'success'; }else{ echo 'error'; } Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 <?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 Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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) ?> Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 <?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 Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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'; Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 yeah i did this it deletes the record but i get the error message even if the record is deleted Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 So you get error on sucess and on error the same message ? Maybe try and explain better saying Yes I Done This does not explain to me what you did and what the result was. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 5, 2011 Share Posted June 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 5, 2011 Share Posted June 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 5, 2011 Share Posted June 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 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() Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted June 5, 2011 Share Posted June 5, 2011 ye but i have no idea how to use mysql_affected_rows() Have a read of the manual on mysql_affected_rows. Quote Link to comment Share on other sites More sharing options...
bscyb Posted June 5, 2011 Author Share Posted June 5, 2011 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('Δεν έγινε η σύνδεση με την βάση δεδομένων'.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(); } ?> Quote Link to comment Share on other sites More sharing options...
lordshoa Posted June 5, 2011 Share Posted June 5, 2011 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.