noname_clark Posted June 12, 2008 Share Posted June 12, 2008 hi all. i am new to this php community. i am trying to do a simple delete a record from a database using variables. here is a sample of the code: <?php $ip = $_POST['ip']; $vote = $_POST['vote']; $limit = $_POST['limit']; //and a $conn=mysql_connect which i removed for this //and a mysql_select_db which i removed for this $query_delete = "DELETE FROM `vote` WHERE hidden=48 AND ip = $ip AND number = $vote LIMIT $limit"; $result_delete = mysql_query($query_delete) or die(mysql_error()); echo "finished"; ?> I have also tried: $sql = 'DELETE * FROM `vote` WHERE `hidden` = 48 AND `number` = "$vote" AND `ip` = "$ip" LIMIT 1'; $result=mysql_query($sql,$conn) or die(mysql_error()); every time i either get: 1) a blank page 2) the code working properly (known by the echo) or 3) i get a mysql error. if $ip = 209.120.190.225 and $vote = 99 and $limit = 4 the error is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.190.255 AND number = 99 LIMIT 4' at line 1 which makes me thenk there is a problem with the ipaddress where the mysql is looking for a number, with one decimal instead of 3 in the ipaddress. i have tried putting single ( ' )and double ( " ) quotes around $ip and that does not work. if i try and submit the query my data base by entering: DELETE FROM `vote` WHERE hidden=48 AND ip = "209.120.190.225" AND number = 99 LIMIT 4 and click "go" my database confirms me that i want to delete it. so i know that the code is correct (and that is only if i have quotes "209.120.190.225") i feel like i have tried everything.... can anyone help? and i know its going to be something stupid and i'm going to kick my butt for it..... thanks in advance Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/ Share on other sites More sharing options...
Jabop Posted June 13, 2008 Share Posted June 13, 2008 Try single quotes around all of your equals values Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-564474 Share on other sites More sharing options...
hansford Posted June 13, 2008 Share Posted June 13, 2008 prior commenter is right, try this: $sql = "DELETE * FROM vote WHERE hidden = '48' AND number = '$vote' AND ip = '$ip' LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-564507 Share on other sites More sharing options...
noname_clark Posted June 13, 2008 Author Share Posted June 13, 2008 if i put in $sql = "DELETE * FROM vote WHERE hidden = '48' AND number = '$vote' AND ip = '$ip' LIMIT 1"; and run the code with $vote = 50 and $ip = 209.120.190.255 i get a mysql error of: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM vote WHERE hidden = '48' AND number = '50' AND ip = '209.120.190.255' LIM' at line 1 so the only thing not in this error that is in the php code is the $sql = "DELETE" and the IT 1 at the very end (the LIMIT 1) Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-565173 Share on other sites More sharing options...
hansford Posted June 14, 2008 Share Posted June 14, 2008 has to be double quotes around single quotes. $sql = "DELETE * FROM vote WHERE hidden = '48' AND number = '$vote' AND ip = '$ip' LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-565299 Share on other sites More sharing options...
noname_clark Posted June 14, 2008 Author Share Posted June 14, 2008 that is what i tried..... the: $sql = "DELETE * FROM vote WHERE hidden = '48' AND number = '$vote' AND ip = '$ip' LIMIT 1"; and that gives me the same error but i just had my mysql create code for me and it said the correct code is: $sql = 'DELETE FROM `vote` WHERE `hidden` = 48 AND `number` = 99 AND CONVERT(`ip` USING utf8) = \'209.120.190.225\' LIMIT 1;'; putting that in works correctly and will delete an entry where number = 99 and ip = 209.120.190.225 if i try and put in a "$vote" or a '$vote' (and switching the starting quotes $sql = "DEL.......) or a $vote for the "$vote" i get no error, it appears that it finished but the records are still in the database for the '$vote' i get mysql error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'209.120.190.225\' LIMIT 1' at line 1 and for the $vote starting with $sql = "DEL...... i get the exact same mysql error as the '$vote' but with single quotes ($sql = 'DEL.....) i get a: Unknown column '$vote' in 'where clause' also the code mysql created for me had back slashes and double quotes arount the ipaddress \'209.120.190.225\' this is really bugging me..... ugh... i wish it would just work. lol. but i'll keep dreaming. lol Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-565450 Share on other sites More sharing options...
noname_clark Posted June 14, 2008 Author Share Posted June 14, 2008 so just playing aroud with the code, i got the $vote to work and the $limit to work by doing a: $sql = "DELETE FROM `vote` WHERE `hidden` = 48 AND `number` = '$vote' AND CONVERT(`ip` USING utf8) = \"209.120.190.225\" LIMIT $limit;"; but i still have no idea how to do the $ip because it is wrapped around a backslash and quotes \"209.120.190.225\" simply doing a: ...AND CONVERT(`ip` USING utf8) = '$ip' LIMIT $limit;"; or a: AND CONVERT(`ip` USING utf8) = \"$ip\" LIMIT $limit;"; or a: AND CONVERT(`ip` USING utf8) = \"'$ip'\" LIMIT $limit;"; does not give errors, and runs the code, but nothing gets deleated.... so i guess the real question is how do i put the $ip inside the backslash double quotes? Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-565452 Share on other sites More sharing options...
fenway Posted June 16, 2008 Share Posted June 16, 2008 First, use double-quotes for the outer string, and singles for the mysql literals. Second, echo the $sql variable. Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-566444 Share on other sites More sharing options...
noname_clark Posted June 16, 2008 Author Share Posted June 16, 2008 thanks, tat worked my brother (who happens 2 build webpages for a living) also showed me that putting a simple "counter" variable that is auto incrementing will work eaisier by just doing a delete from vote where counter = '$counter' thanks fo all the help! Link to comment https://forums.phpfreaks.com/topic/109966-solved-delete-from-wont-work/#findComment-566738 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.