简体   繁体   English

If语句在PHP会话和SQL中无法正常工作

[英]If statement not working as expected with PHP sessions and SQL

So in my session is a table with various serial numbers that the user adds in. There is also a db with the information for that corresponding serial number. 因此,在我的会话中是一个表,其中包含用户添加的各种序列号。还有一个数据库,其中包含该序列号的信息。

What I want my code to do is, for each serial in the session, pull the corresponding data from the database. 我要我的代码执行的操作是,对于会话中的每个序列,从数据库中提取相应的数据。 For now, I'll just echo a string. 现在,我将仅回显一个字符串。

So I figure to do that, you must loop through each serial in the session, and for each you must check if the same one exists in the db and if it does, then echo something. 因此,我想这样做,您必须遍历会话中的每个序列,并且对于每个序列,您必须检查db中是否存在相同的序列,如果存在,则回显某些内容。

The problem is that it works, but it only runs that last if loop once when I have more than one serial in the session. 问题是它可以工作,但是当会话中有多个串行时,它仅在if循环最后运行一次。

Here's what gets echo'd out when you have 1,2,3 and 4 serials in the session. 当会话中有1,2,3和4个序列时,这就是回显的原因。 http://pastebin.com/BcWMZ542 http://pastebin.com/BcWMZ542

Any help would be greatly appreciated. 任何帮助将不胜感激。

Code: https://gist.github.com/anonymous/f961509aa407f270d9325ed2aa40b42d 代码: https//gist.github.com/anonymous/f961509aa407f270d9325ed2aa40b42d

After the first foreach item from $_SESSION["cart"] , you have looped trough the database results. $_SESSION["cart"]的第一个foreach项之后,就遍历了数据库结果。

This means, in the next loop, this will be empty: while($row = mysql_fetch_array($books, MYSQL_NUM)) 这意味着,在下一个循环中,它将为空: while($row = mysql_fetch_array($books, MYSQL_NUM))

First, don't use the deprecated mysql_ classes. 首先,不要使用不推荐使用的mysql_类。

Secondly, it is much better to first loop over the $_SESSION["cart"] info, and use the info in there to write your SQL to 其次,最好首先循环$_SESSION["cart"]信息,然后使用其中的信息将SQL写入

SELECT * from tblbooks WHERE id IN ('the', 'list', 'of', 'ids') ORDER BY bookTitle asc

then you don't need to verify again, all results you will get from the database will be entries that match your session. 那么您无需再次进行验证,您将从数据库中获得的所有结果都是与您的会话相匹配的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM