如何使用的TextInput值添加MySQL查询条件? [英] How to add mysql query condition using the textinput value?

查看:219
本文介绍了如何使用的TextInput值添加MySQL查询条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理在一个控制器动作形式?

例如在我看来:

 <形式ID =formid>
 <输入名称=yrlevel>< /输入>
< /形式GT;
 

我将如何处理像这样在我的动作(加从什么文本框的值MySQL查询条件):

  $ criteria-> addSearchCondition('yrLvl',$ _ GET ['yrlevel']
 

解决方案

您已经得到了一半的工作方面所做的工作;您需要将值添加到标准,你所做的一切,然后你需要,当你调用一个搜索,像这样指定的标准:

  $标准=新CDbCritera;
//如果你想使用MySQL的喜欢语法,你可以使用addSearchCondition()
$ criteria-> addSearchCondition('yrlevel',$ _ GET ['yrlevel'],真正的);
//如果你想要做一个直接的比较,你可以使用条件()
$ criteria->条件('yrlevel =:yrlevel');
$ criteria-> PARAMS =阵列(
    :yrlevel'=> $ _ GET ['yrlevel']
);

$模式=为MyModel ::模型() - >的findAll($条件);
 

$车型然后将包含符合搜索条件的车型阵列。我已经展示了 addSearchCondition()条件()以上是关于如何使用它们(您可能不会告发的例子'T想使用它们共同为同一领域,因为我已经如上图所示),看一看的 CDbCriteria 了解关于如何构建查询的详细信息。

Yii中会使用PDO绑定你的PARAMS,这样你就不是绝对需要自己消毒的数据,虽然你可能想不管是什么原因。

How to process forms in a Controller Action?

for example in my view:

<form id="formid">
 <input name="yrlevel"></input>
</form>

How will I process it like this in my action (adding mysql query condition from what is the value of the textfield):

$criteria->addSearchCondition('yrLvl', $_GET['yrlevel']

解决方案

You've got half the work done there; you need to add the value to the criteria, as you have done, and then you need to specify that criteria when you call a search, like so:

$criteria = new CDbCritera;
// If you want to use MySQL's 'LIKE' syntax you could use addSearchCondition()
$criteria->addSearchCondition('yrlevel',$_GET['yrlevel'], true);
// If you want to do a direct comparison you could use condition()
$criteria->condition('yrlevel = :yrlevel');
$criteria->params = array(
    ':yrlevel'=>$_GET['yrlevel']
);

$models = MyModel::model()->findAll($criteria);

$models will then contain an array of models that match the search criteria. I've shown addSearchCondition() and condition() above as an example on how to use them (you probably wouldn't want to use them together for the same field as I've shown above), take a look at CDbCriteria for more info on how you can structure queries.

Yii will bind your params using PDO so you don't absolutely need to sanitize the data yourself, although you may want to for whatever reason.

这篇关于如何使用的TextInput值添加MySQL查询条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆