Page 1 of 1

how to delete a row like this

Posted: Mon Aug 25, 2014 8:56 pm
by donny
hello,

can someone help me i need to make a SQL code that deletes a row WHERE id = 5 AND WHERE name = mouse

i basically need to make a SQL code that will delete a row but i need 2 WHERE statements in it.

Re: how to delete a row like this

Posted: Mon Aug 25, 2014 9:25 pm
by Celauran

Code: Select all

DELETE FROM tablename WHERE id = 5 AND name = 'mouse'

Re: how to delete a row like this

Posted: Mon Aug 25, 2014 9:49 pm
by donny
ok i got that down can you help me incorporate variables in my sql code..

my code right now

Code: Select all


$order = "DELETE FROM  $DB2 WHERE ClientID = $CID AND email = $email;

Re: how to delete a row like this

Posted: Mon Aug 25, 2014 11:10 pm
by donny
right now i got

Code: Select all


<?php
$CID = $_GET['CID'];
$email = $_GET['email'];
$DB2 = $_GET['DB'];
mysql_connect("localhost","username","password");//database connection
mysql_select_db("database");
$order = "DELETE FROM '$DB2' WHERE ID = '$CID' AND email = '$email'";
$result = mysql_query($order);
if($result)
{
 echo("
Input data is succeed");
}
else
{
echo("
Input data is fail");
}
echo $order;

?>
i know my connections are good. right now i am getting input data is fail.. which means the SQL code is failing.

in my DB ID is a INT column and email is a TINYTEXT column. any ideas?

i echoed $order to see my SQL was good with my added variables.. i know for sure i am trying to delete a row that has this data in it in those columns.

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 6:18 am
by Celauran
Lose the quotes around the table name. You want back ticks there instead.

Also escape your inputs, don't use mysql_ functions, use prepared statements, etc.

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 6:39 am
by donny

Code: Select all


<?php
$CID = $_GET['CID'];
$email = $_GET['email'];
$DB2 = $_GET['DB'];
mysql_connect("localhost","username","password");//database connection
mysql_select_db("database");
$order = "DELETE FROM `$DB2` WHERE ID = '$CID' AND email = '$email'";
$result = mysql_query($order);
if($result)
{
 echo("
Input data is succeed");
}
else
{
echo("
Input data is fail");
}
echo $order;

?>
 

i changed it to back ticks it still doesn't work. can you please update the code for me the way it should work.. i copied my format of code from other working mysql functions on my site and they work no problem.

the db username has the appropriate authority level

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 6:44 am
by Celauran
How does it not work? What does mysql_error() say? Have you checked that all the variables contain values?

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 6:50 am
by donny
yes i have checked the variables are correct.. i echoed the sql code to make sure they were in there too and they're showing up. i made sure the data does exists in the db and what I'm trying to delete actually exist.. can you tell ne how to add mysql error reporting.

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 6:53 am
by Celauran
Instead of echoing "Input data is fail", echo mysql_error()

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 6:54 am
by donny
Unknown column '8431' in 'where clause'

it's saying $CID value is the column when that should be row data under column ID


column names are ID and email and the $variables are data that should be in the rows... both $variable contents are on the same row and my column names are correct

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 7:06 am
by donny
i ran the SQL query in phpmyadmin and it deleted it no problem

Re: how to delete a row like this

Posted: Tue Aug 26, 2014 7:12 am
by donny
i fixed it

Query had to look like this

Code: Select all

$order = "DELETE FROM `$DB2` WHERE `ID` = '$CID' AND `email` = '$email'";