PHP语法错误或访问冲突:1064 -在where子句中使用变量


PHP syntax error or access violation: 1064 - using variable in where clause

拉我的头发在这一个,我已经尝试了一些事情在改变转义字符的变量和没有工作,包括改变为'"。$ r。

如果我将变量硬编码为常量,它可以工作。例如$r = "Word";

我已经将变量重命名为随机字母,以防它们是保留词。

我已经为mysql设置了日志记录,我比较了语句,它们是相同的,唯一的区别是一个是通过文本框提交的,另一个是通过常量设置的。

模型:

 public static function searchProfile()
    {
    $r = Request::post('w');
    $args = "";
    if ($r) {$args = "`info_tradingstatus` = '$r'";}
    $database = DatabaseFactory::getFactory()->getConnection();
    $sql = "SELECT profile_id, profile_name FROM profile_seeker WHERE $args";

视图:

  <input type="text" name="w" class="form-control" placeholder="Enter Keyword" aria-describedby="basic-addon1">

控制器:

public function search_action()
{
ProfileModel::searchProfile(
Request::post('w'),
Redirect::to('profile/searchresults'));
}

嗨,我认为问题是当$r是空的,请尝试:

public static function searchProfile()
    {
    $r = Request::post('w');
    $args = " 1 ";
    if ($r) {$args = "`info_tradingstatus` = '".$r."'";} // just in case
    $database = DatabaseFactory::getFactory()->getConnection();
    $sql = "SELECT profile_id, profile_name FROM profile_seeker WHERE $args";