2012-11-23 55 views
0

我想创建一个脚本,选择给定的任期和城市给定的MySQL列中的所有用户。该城市将针对会员,但每个会员可以有3或4个不同的职位(调酒师,服务员,主持人等)。我试图使用的代码在下面,但是,它给了我一个错误。如果您需要更多信息,请与我们联系。谢谢!mysql_query使用LIKE时返回错误

Could not find staff: Unknown column 'Bartender' in 'where clause' 

 

<?php 

    $perpage = 15; 
    $city = $_GET['city']; 
    $type = $_GET['type']; 

    if(isset($_GET["page"])) 
    { 
     $page = intval($_GET["page"]); 
    } 
    else 
    { 
     $page = 1; 
    } 

    $calc = $perpage * $page; 
    $start = $calc - $perpage; 
    $result = mysql_query("SELECT * FROM staff WHERE titles LIKE $type AND city=$city LIMIT $start, $perpage"); 
    $rows = mysql_num_rows($result); 
    if($rows) 
    { 
     $i = 0; 
     while($post = mysql_fetch_array($result)) 
     { 
?> 
      <tr style="background-color: #cccccc;"> 
       <td style="font-weight: bold;font-family: arial;"><?php echo $post["staffnum"]; ?> >> <?php echo $post["titles"]; ?></td> 
      </tr> 
      <tr> 
       <td style="font-family: arial;padding-left: 20px;"><?php echo $post["abt1"]; ?></td> 
      </tr> 
<?php 
     } 
    } else { 
      die('Could not find staff: ' . mysql_error()); 
    } 
?> 
+1

'喜欢\ “$型\”'。但你需要修复SQL注入使用PDO准备 –

+0

请发布员工的表定义。 – ethrbunny

回答

4

为了使用LIKE只要你想,你需要用它在引号和使用适当的%字符。

$type = mysql_real_escape_string($_GET['type']); 
// do the same with $city and any other user input 

$result = mysql_query("SELECT * FROM staff WHERE titles LIKE '%" . $type . "%' AND city='" . $city . "' LIMIT $start, $perpage"); 
+0

如果$ city持有一个字符串,也必须引用这个字符串。 – david

+0

@david,谢谢 - 添加到答案。我怀疑城市可能是一个整数,但否则会导致语法错误。 – MrCode

0

尝试

$type = mysql_real_escape_string($_GET['type']); 
$city = mysql_real_escape_string($_GET['city']); 

$result = mysql_query("SELECT * FROM staff WHERE titles LIKE '%$type%' AND city='$city' LIMIT $start, $perpage");