Jump to content

Code works on one server but not another


ambition13

Recommended Posts

Hello,

I have the same files on 2 servers, one server is running php 4.3.2 and the code works.  The newer server is running 4.4.3 and the same code does not work.

The code is as follows:

                foreach ($fav_people as $name=>$more) {
                        foreach ($more as $people_id=>$value) {
                                if ($people_id != 0) {
                                        $INSERT = "INSERT INTO favorites (user_id, fav_type, id) VALUES ('$_USER[user_id]', '2', '$people_id')";
                                        $iresult = mysql_query($INSERT);
                                }
                        }

On the newer server, it gives the following error:

Warning: Invalid argument supplied for foreach() in _db_functions.php on line 157

This warning shows up 4 times for that line.  Anyone have any suggestions on how to troubleshoot this?

Thanks in advance.

ambition13


Link to comment
https://forums.phpfreaks.com/topic/31293-code-works-on-one-server-but-not-another/
Share on other sites

I think it is screaming about the second while,
I cannot see the need for the second while either, test:
[code]

<?php

$fav_people = array( 1 => "John", 2 => "Fred");

foreach ($fav_people as $people_id => $value) {
if ($people_id != 0) {
$INSERT = "INSERT INTO favorites (user_id, fav_type, id) VALUES ('$_USER[user_id]', '2', '$people_id')";
echo $INSERT;
}
}

?>

[/code]

But it sertainly smells a register_globals issue
[quote]It is being defined in another file[/quote]

I assume you meen an included file? Place this, just prior to the code you have posted.

[code=php:0]
print_r($fav_people);
[/code]

Also... where might this $_USER array be defined. Thats a pretty poor naming convention IMO, its a little confusing.
Yes, an included file.  I inherited this code so I am not sure of all the naming conventions yet.

Here is the output of the print_r:

Array ( [1] => Tom hanks [2] => [3] => [4] => [Tom Hanks] => Array ( [16937] => Array ( [birthdate] => 1956-07-09 [image] => mf ) ) )

Ok, I have narrowed the problem down further.

The error is coming from the following code:

function update_fav_people($fav_people) {
        global $_USER;

        $DELETE = "DELETE FROM favorites WHERE user_id = '$_USER[user_id]' and fav_type = '2'";
        $dresult = mysql_query($DELETE);

        if ($fav_people) {
                natksort($fav_people);
                foreach ($fav_people as $name=>$more) {
                        if ($more) {
                                foreach ($more as $people_id=>$value) {
                                        if ($people_id != 0) {
                                                $INSERT = "INSERT INTO favorites (user_id, fav_type, id) VALUES ('$_USER[user_id]', '2', '$people_id')";
                                                echo $INSERT;
                                                $iresult = mysql_query($INSERT);
                                        }
                                }
                        }
                }
                $UPDATE = "UPDATE users SET fav_people = '" . addslashes(serialize($fav_people)) . "' WHERE user_id = '$_USER[user_id]'";
                echo $UPDATE;
                $uupdate = mysql_query($UPDATE);
        } else {
                $UPDATE = "UPDATE users SET fav_people = '' WHERE user_id = '$_USER[user_id]'";
                $uupdate = mysql_query($UPDATE);
        }
}

When running the UPDATE code, it inserts extra data.  The Mysql record then looks like this:  a:5:{i:1;s:13:"Julia Roberts";i:2;s:0:"";i:3;s:0:"";i:4;s:0:"";s:13:"Julia Roberts";a:1:{i:8483;a:2:{s:9:"birthdate";s:10:"1967-10-28";s:5:"image";s:2:"mf";}}}

It should just look like this:
a:1:{s:13:"Julia Roberts";a:1:{i:8483;a:2:{s:9:"birthdate";s:10:"1967-10-28";s:5:"image";s:2:"mf";}}}

Again I inherited this code and am just trying to make it work.  How can I rewrite the above function so that it doesn't insert the extra data?

Thanks again for all the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.