Pipo
Pipo

Reputation: 309

delete records from mysql where it like number

I want to delete records from mysql using php

the field name: Week

Week records values as below:

200801
200802
200803
200804
200805
etc

I created TextField named "tYear", user will insert first 4 numbers from the left and sql statement should remove all the records starting with them, example ... if the user insert 2008 all the above records as they starting with 2008 should be removed.

I am using sql statement as below but it's not working

$sql= "DELETE from sites WHERE Week like '". $_POST['tYear']."'";
$results = mysql_query($sql);

Upvotes: 0

Views: 1835

Answers (3)

Alexey
Alexey

Reputation: 7247

Try this

$sql= "DELETE from sites WHERE Week like '". $_POST['tYear']."%'"

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39724

Use LIKE

$sql= "DELETE FROM `sites` WHERE `Week` LIKE '". (int)$_POST['tYear']."%'";

Upvotes: 2

jogesh_pi
jogesh_pi

Reputation: 9782

your query is : $sql= "DELETE from sites WHERE Week like '". $_POST['tYear']."'";

and with like in mysql you have to use like '%%' or like '%' for so use :

$sql= "DELETE from sites WHERE 
        Week like '%". mysql_real_escape_string($_POST['tYear'])."%'";

Upvotes: 2

Related Questions