首页 \ 问答 \ mysql SET类型和多数据进程用php在sql查询中(Mysql SET type and multi data process with php in sql query)

mysql SET类型和多数据进程用php在sql查询中(Mysql SET type and multi data process with php in sql query)

所以我一直在尝试在我的网站上创建一个搜索表单,这取决于用户检查的内容(复选框),我花了很多时间在网上研究,但没有找到任何东西,所以我不得不自己做。 所以这里是html的代码

<li><input type="checkbox" name="checkbox[]" value="action"> Action</li>
<li><input type="checkbox" name="checkbox[]" value="Adventure"> Adventure</li>
<li><input type="checkbox" name="checkbox[]" value="Animation"> Animation</li>
<li><input type="checkbox" name="checkbox[]" value="cars"> Cars</li>
<li><input type="checkbox" name="checkbox[]" value="cartoon"> Cartoon</li>
<li><input type="checkbox" name="checkbox[]" value="comedy"> Comedy</li>

这是php

<?php

     if(!empty($_POST['checkbox'])) {

        $Gen3 = implode(" AND * ", $_POST['checkbox']);

     $Gen2= str_replace('*', 'Genre like ', $Gen3); 

                $table='animelist';

                $sql = "SELECT * FROM $table  WHERE Genre like '".$Gen2."' ORDER BY NAME DESC" ;  

                echo $sql;
                $result = mysqli_query($conn, $sql) or die('Erreur SQL !'.$req.'<br>'.mysql_error());

    while($row = $result->fetch_assoc()){

?> 

<li><a href="Anime.php?/anime=<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?></a></li>

<?php ;
}
};
?>

echo $ sql如果我检查两个复选框,例如结果

SELECT * 
FROM animelist 
WHERE Genre like 'action AND Genre like Adventure' 
ORDER BY NAME DESC

所以这里的事情是用户研究的“电影”可能是“动作,冒险”的一套,所以当用户选中复选框时,结果必须是“电影”,这不仅仅是动作和冒险行动。

现在,如果我只检查一个复选框,evrything工作正常,但当我检查两个或更多时,我什么都没得到

所以我虽然在变量之前和之后的sql查询是破坏它的那个,所以我不得不骑它们,所以我尝试使用mysql_real_escape_string,但是因为它不再是受理者而且mysqli_real_escape_string不做我被困在这里的工作。


So I've been trying to create a search form on my website that depends on what the user check ( checkboxes), I've spent many time researching on web but didn't find anything, so I had to do things myself. so here is the code for the html

<li><input type="checkbox" name="checkbox[]" value="action"> Action</li>
<li><input type="checkbox" name="checkbox[]" value="Adventure"> Adventure</li>
<li><input type="checkbox" name="checkbox[]" value="Animation"> Animation</li>
<li><input type="checkbox" name="checkbox[]" value="cars"> Cars</li>
<li><input type="checkbox" name="checkbox[]" value="cartoon"> Cartoon</li>
<li><input type="checkbox" name="checkbox[]" value="comedy"> Comedy</li>

and here is the php

<?php

     if(!empty($_POST['checkbox'])) {

        $Gen3 = implode(" AND * ", $_POST['checkbox']);

     $Gen2= str_replace('*', 'Genre like ', $Gen3); 

                $table='animelist';

                $sql = "SELECT * FROM $table  WHERE Genre like '".$Gen2."' ORDER BY NAME DESC" ;  

                echo $sql;
                $result = mysqli_query($conn, $sql) or die('Erreur SQL !'.$req.'<br>'.mysql_error());

    while($row = $result->fetch_assoc()){

?> 

<li><a href="Anime.php?/anime=<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?></a></li>

<?php ;
}
};
?>

the echo $sql if I check two checkboxes for example result

SELECT * 
FROM animelist 
WHERE Genre like 'action AND Genre like Adventure' 
ORDER BY NAME DESC

so the thing here is that the "movie" that the user would research might be a SET of "Action,Adventure" , so when the user checks the checkboxes the result have to be the "movies" which are both Action and adventure not only Action.

for now if I only check one checkbox evrything works fine but when I check two or more I get nothing

so I though that the ' in the sql query before and after the variable are the one ruining it, so I had to get ride of them, so I tried to use mysql_real_escape_string, but since its not accepter anymore and the mysqli_real_escape_string doesn't do the job I am stuck here.


原文:https://stackoverflow.com/questions/33979888
更新时间:2021-12-30 19:12

最满意答案

有许多集合支持这一点。 例如,jME3使用自己的ArrayList变体,称为SafeArrayList 。 这是一个开源项目,所以你可以在那里找到实现。

比每次读取克隆更好的解决方案是在修改时复制列表。 只需创建一个新列表,对其进行更改,然后将其分配回您的侦听器列表。

在大多数情况下,观察者的变化发生在循环观察者身上的次数较少。

如果您复制列表并修改克隆,那么迭代上一版本列表的任何内容都不会看到更改,但也不会例外。


There are a number of collections that support this. For example jME3 uses their own variant of ArrayList called SafeArrayList. It's an open source project so you can find the implementation there.

A better solution than cloning on every read is to copy the list on modification. Just create a new list, make the change to it, assign that back to your list of listeners.

In most cases changes to observers happens less often that looping over observers.

If you copy the list and modify the clone then anything iterating over the previous version of the list will not see the change but will also not exception.

相关问答

更多
  • 没关系。 你有一个对象, Presenter ,它在一个线程内的无限循环中使用。 这并不意味着你也不能在线程调用它时调用它的方法。 您需要注意的唯一考虑因素是,如果线程读取/使用的数据与观察者通知相同,则您应该同步它的访问权限。 因此,总而言之,当Presenter在无限循环内运行时,对它的方法的任何调用都将立即得到解答(除非它们具有同步访问权限,在这种情况下它将阻塞直到获得锁定所有权) 以下是完全正常的: class Presenter implements IObserver { public ...
  • 在支持多重继承的语言中,支持这种功能的对象来自支持这些方法的Observable类的INHERIT并不罕见。 我从来没有遇到过使用聚合来支持这个的解决方案,这就是你所描述的,但鉴于PHP不支持多重继承,这听起来像是一个合理的解决方法。 It's not unusual for an object that supports this functionality to INHERIT from an Observable class, which supports these methods, in lang ...
  • 要小心,在创建新对象时, new Subject()会有一个提供新对象的隐式return 。 您不需要像往常一样返回对象。 现在,对于方法,您需要将它们附加到对象的原型 。 这样,当创建对象时, 它将附加这些方法 。 例如: function MySuperObject( value ) { this.property = value; }; MySuperObject.prototype.attachedMethod = function() { console.log("Propert ...
  • 简答:看看这个问题的第一个答案: 带代表的C#观察者/观察者的超简单例子 我理解你想尝试自己实现它,但委托和事件在这里真的很合适(实际上是c#中内置的观察者模式的实现)。 如果仍想自己做,我建议使用接口而不是抽象/具体类。 public interface ISubject { void AttachObserver(IObserver observer); void DetachObserver(IObserver observer); void NotifyObservers( ...
  • 经典的设计模式不涉及并行和线程。 你必须为N个观察者产生N个线程。 但要小心,因为它们之间的交互必须以线程安全的方式完成。 Classic design patterns do not involve parallelism and threading. You'd have to spawn N threads for the N observers. Be careful though since their interaction to this will have to be done in a t ...
  • MVVM-和Observable-模式是不同的模式,你会发现很多很好的例子。 假设您正在实施MVVM电话应用程序,这两种模式可以很好地结合使用: 您的ViewModel(MV VM )具有要在XAML-VIEW(M V VM)中显示/更新的属性。 无论何时设置(或更新)属性值(在ViewModel中),都会触发类似()=> PropertyChanged("PropertyName); Observer现在位于MVVM Framework(或ViewModel的基类)中,此组件观察这些更改并使用VIEW管理 ...
  • 你的问题是比较苹果和梨。 观察者模式是问题的解决方案。 它没有告诉你如何实现解决方案,但更像是蓝图。 因此,.NET中的事件模型是观察者模式的实现。 EventHandler委托定义观察者, event关键字负责处理主题中的所有通知,正如观察者模式中所定义的那样。 Your question is comparing apples and pears. The observer pattern is a solution to a problem. It doesn't tell you how to im ...
  • 有许多集合支持这一点。 例如,jME3使用自己的ArrayList变体,称为SafeArrayList 。 这是一个开源项目,所以你可以在那里找到实现。 比每次读取克隆更好的解决方案是在修改时复制列表。 只需创建一个新列表,对其进行更改,然后将其分配回您的侦听器列表。 在大多数情况下,观察者的变化发生在循环观察者身上的次数较少。 如果您复制列表并修改克隆,那么迭代上一版本列表的任何内容都不会看到更改,但也不会例外。 There are a number of collections that support ...
  • 简单的说,让你了解观察者模式 如果1000人订阅每日新闻报纸。 每当新副本到达时,即每天,发布者都会向其订阅者发送一份副本。 同样的方法一个类在获取新信息时就会向其观察者发送信息。 in Simple words ,to make you understand Observer pattern if 1000 people are subscribed to a daily news paper. Publisher will send a copy to his subscribers whenever ...
  • 观察者模式仍然适合。 但是模式不是一成不变的,如果你不需要一组观察者,你可以简化一下:实现观察者 - >主题关联为1:1 The observer pattern still suits. But patterns are not set in stone, you can simplify it a bit if you don't need a collection of observers: implement the observer-->subject association as 1:1

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)