Forum Moderators: coopster

Message Too Old, No Replies

foreach() Confusion

What am I doing wrong?

         

GalaxyGames

9:06 pm on Jul 19, 2009 (gmt 0)

10+ Year Member



Hello! I'm an intermediate PHP coder, and today, I was trying to do a trade system. But for some reason, even though I've checked it many times, I still get:

[phpBB Debug] PHP Notice: in file /home/example/public_html/trade.php on line 292: Invalid argument supplied for foreach()

It's funny, because everything else comes out fine, but when it comes to serializing, an error shows up along with the rest of the script.

My MySQL/PHP file looks like this (it's also been synced with my phpBB forum) :

+++Username+++TradeID+++Description+++adoptsbeingtraded
PLACEHOLDER 1 PLACEHOLDER PLACEHOLDER

GalaxyGames 3 No Decription N;

PLACEHOLDER 2 PLACEHOLDER PLACEHOLDER
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : 'forum/';

$user->session_begin();
$auth->acl($user->data);
$user->setup();

$tradestationcontrolyourtrade = "/trade.php?id=cyt";
$tradestationcontrolyourtrades = "http://www.example.com/trade.php?id=cyt";
$currentpage = $_SERVER['REQUEST_URI'];
if($tradestationcontrolyourtrade==$currentpage ¦¦ $tradestationcontrolyourtrades==$currentpage) {

echo "<font size='2'><b>Manage Your Trades</b></font><br><br>";

mysql_connect("#*$!X", "#*$!X", "#*$!X") or die(mysql_error());
mysql_select_db("adopt19_points") or die(mysql_error());
$query = "SELECT * FROM `trade` WHERE `Username` = '$username' " ;
$result = mysql_query($query);

if(mysql_num_rows($result)==0){

echo "You have no adopts for trade!";

}

if(mysql_num_rows($result)!=0){

mysql_connect("#*$!X", "#*$!X", "#*$!X") or die(mysql_error());
mysql_select_db("adopt19_points") or die(mysql_error());
echo "<center>";
echo "<table width='580' cellspacing='0' cellpadding='4' style='border: 1px solid black;'>";
$query= "SELECT `adoptsbeingtraded` FROM `trade` WHERE `Username` = '$username' ";
$mys= "SELECT * FROM `trade` WHERE `Username` = '$username' ";
$doQuery=mysql_query($query);
$result=mysql_query($mys);
while($info=mysql_fetch_array($doQuery) && $next=mysql_fetch_array($result))
{
$info=unserialize($info['adoptsbeingtraded']);

echo "<tr><td><p>Trade ID: ".$next['TradeID']." Username: " .$next['Username']." </p></td><td colspan='2'>
<p><a href='http://www.example.com/trade.php?id=".$next['TradeID']."&mode=delete'>Delete Your Lot</a></p></td></tr><tr><td style='border-top: 1px solid black; border-bottom: 1px solid black;' colspan='3'><center>";

foreach($info as $somethingdifferent)
{
echo $somethingdifferent['adoptsbeingtraded'];
}

echo "</center></td></tr>
<center><tr><td colspan='3'><p><center>".$next['Description']. " </center></p></td></tr></center>";

}
Print "</table>";
Print "</center>";

}}

$tradestationnewloter = "/trade.php?id=setuplots";
$tradestationnewloters = "http://www.example.com/trade.php?id=setuplots";
$currentpage = $_SERVER['REQUEST_URI'];
if($tradestationnewloter==$currentpage ¦¦ $tradestationnewloters==$currentpage) {

$describe=$_POST['describe'];
$adoptsbeingtraded=serialize($_POST['adopts']);

mysql_connect("#*$!X", "#*$!X", "#*$!X") or die(mysql_error());
mysql_select_db("database name") or die(mysql_error());
$sql = mysql_query("SELECT * FROM `trade` ORDER BY `TradeID` DESC LIMIT 1 ");
while($super = mysql_fetch_array( $sql )){
$tradeid = $super['TradeID'];
$tradeid++;

mysql_query("INSERT INTO `trade` VALUES ('$username', '$tradeid','$describe','$adoptsbeingtraded')");

echo "Your lot was posted successfully.";

}}

$tradestationnewlot = "/trade.php?id=setuplot";
$tradestationnewlots = "http://www.example.com/trade.php?id=setuplot";
$currentpage = $_SERVER['REQUEST_URI'];
if($tradestationnewlot==$currentpage ¦¦ $tradestationnewlots==$currentpage) {

mysql_connect("#*$!X", "#*$!X", "#*$!X") or die(mysql_error());
mysql_select_db("adopt19_selgararegion") or die(mysql_error());
$getadopts = mysql_query("SELECT * FROM `adopts_owned_adoptables` WHERE `owner` = '$username' ORDER BY `aid` LIMIT 100")
or die(mysql_error());
echo "<form action='trade.php?id=setuplots' method='post'>";
echo "<p style='margin-bottom: 0px;'>Comments:</p><p style='margin-bottom: 10px;'>
<input type='text' name='describe'></p>";
while($display = mysql_fetch_array( $getadopts )){
echo "<center><img src='http://www.example.com/selgararegion/selgara-adopt/siggy.php?id=".$display['aid'] . "'style='border-style: none;''><input type='checkbox' id='adopts[]' value='".$display['aid']."'></center>";
}

echo "<input class='field' type='submit' value='Submit Adopts'>";

echo "</form>";
}

Any suggestions to what throws it off? I tried a couple validators, and all of them didn't catch the foreach().

[edited by: dreamcatcher at 6:14 am (utc) on July 23, 2009]
[edit reason] No personal urls, thanks. [/edit]

penders

10:53 pm on Jul 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<input type='checkbox' id='adopts[]' value='".$display['aid']."'>

Have you checked what comes back from your form?

You seem to be missing a NAME attribute on your INPUT. I think that may be your ID attrib should really be your NAME attrib, since your ID attribute would be invalid in this case. ...this would result in nothing coming back from your form, nothing to serialize, empty field in your DB, nothing to unserialize and consequently no array to foreach().

whoisgregg

10:04 pm on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would echo the $info['adoptsbeingtraded'] value to see what's in there. I've found when storing serialized values in a database that I sometimes underestimate how much data could be there. I've defined fields as VARCHAR(255) to store serialized data that ended up getting cut off (and causing unserialize calls to fail). You may need to bump it up to a LONGTEXT field if that's the case.

Also, it might make sense to simply wrap the foreach() call with a check to see if the variable really is an array and that there's at least one element?

if(is_array($info) && count($info) >= 1){
foreach($info as $somethingdifferent)
{
// loop code
}
}