how to delete a row like this

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

how to delete a row like this

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to delete a row like this

Post by Celauran »

Code: Select all

DELETE FROM tablename WHERE id = 5 AND name = 'mouse'
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post 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;
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to delete a row like this

Post 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.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post 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
Last edited by donny on Tue Aug 26, 2014 6:44 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to delete a row like this

Post by Celauran »

How does it not work? What does mysql_error() say? Have you checked that all the variables contain values?
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to delete a row like this

Post by Celauran »

Instead of echoing "Input data is fail", echo mysql_error()
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post 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
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post by donny »

i ran the SQL query in phpmyadmin and it deleted it no problem
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: how to delete a row like this

Post by donny »

i fixed it

Query had to look like this

Code: Select all

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