Jump to content

Recommended Posts

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

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)

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

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?

 

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!

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.