简体   繁体   中英

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.

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.

Here's what gets echo'd out when you have 1,2,3 and 4 serials in the session. http://pastebin.com/BcWMZ542

Any help would be greatly appreciated.

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

After the first foreach item from $_SESSION["cart"] , you have looped trough the database results.

This means, in the next loop, this will be empty: while($row = mysql_fetch_array($books, MYSQL_NUM))

First, don't use the deprecated mysql_ classes.

Secondly, it is much better to first loop over the $_SESSION["cart"] info, and use the info in there to write your SQL to

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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