If statement problem. Accessing a php file through flash

I am accessing a php file through flash. Most of it works, but one textfield in flash seems to be throwing an undefined (&num) a lot (sometimes it doesn't happen all the time). doesn't happen when i comment out the two if statements that e-mail people. i'm thinking there's just a stupid little error.

here is my code, any help would be great.

<?PHP

error_reporting(E_ALL);

$user = $HTTP_GET_VARS['user'];
$subject = $HTTP_GET_VARS['subject'];

$link = mysql_connect('localhost', 'test', 'test321')
   or die('Could not connect: ' . mysql_error());

mysql_select_db('dest') or die('Could not select database');

$query = "SELECT * FROM test where name = '$user' and subject = '$subject' order by id desc limit 1";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

while ($rows = mysql_fetch_array($result)) {

     
$id = $rows['id'];
$name = $rows['name'];
$email = $rows['subject'];
$content = $rows['content'];
$notify = $rows['notify'];
$prod1 = $rows['prod1'];
$prod2 = $rows['prod2'];
$prod3 = $rows['prod3'];
$preandpost = $rows['preandpost'];
$filename = $rows['file'];
$background = $rows['background'];
$views = $rows['views'];
$viewnum = $rows['viewnum'];

}

$viewnum = $viewnum + 1;

print("&prod2=$prod2");
print("&prod1=$prod1");
print("&prod3=$prod3");
print("&preandpost=$preandpost");
print("&home=$content");
print ("&bg=$background");
print ("&file=$filename");

$max = "max";

if ($viewnum > $views) {
print ("&num=$max");
} else {
print ("&num=$viewnum");
}

$emailnum = $views - 5;


$query2 = "UPDATE test set viewnum='$viewnum' where id=$id";
$result2 = mysql_query($query2) or die ('Query failed: ' . mysql_error());

mysql_free_result($result);
mysql_close($link);


if ($viewnum == $emailnum) {
$to  = "jason@test.com";
$subject = "Notification";
$message = "message sent";
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: e-mercial Server <jason@test.com>\r\n";
mail($to, $subject, $message, $headers);
}

if ($notify = "Yes") {
$tonotify  = "jason@test.com";
$subjectnotify = "$name has accessed this";
$messagenotify = "$name has accessed this";
$headersnotify  = "MIME-Version: 1.0\r\n";
$headersnotify .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headersnotify .= "From: Server <jason@test.com>\r\n";
mail($tonotify, $subjectnotify, $messagenotify, $headersnotify);
}

?>
jmingoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marcus BointonCommented:
Several things wrong, but can't see they would be causing this problem:

$HTTP_GET_VARS['user'] is deprecated, use $_GET['user'] instead.

You have a while loop looping over a query result that will only ever contain 1 record.

If your printed output is to be used as a URL (as it looks), you need to encode the values:

print "&file=".urlencode($filename);
0
Marcus BointonCommented:
Ah, and this old problem:

if ($notify = "Yes") {

should be

if ($notify == "Yes") {
0
jmingoAuthor Commented:
well... i made some of those changes.

if ($notify == "yes") {

and the

$_GET['user']


still coming up undefined a lot of the time. my printed output isn't being used in a url. the query statement is meant to retreive one record.

any other ideas??
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

jmingoAuthor Commented:
should it be before or after the

mysql_free_result($result);
mysql_close($link);

it seems to do it before and after.

0
Marcus BointonCommented:
My point about the while loop is that it's unnecessary - you can just change 'while' to 'if', and for safety, you should move lots of later stuff inside its closing bracket as at present you'll be attempting to send emails etc even if no record is found. That may explain things not working occasionally.
0
jmingoAuthor Commented:
so it should be

if ($rows = mysql_fetch_array($result)) {

and move the }  down to the bottom, but before the mysql close stuff, and put the emailing code within the brackets?


0
jmingoAuthor Commented:
i changed the while to if, and there's still the recurring undefined problem.

any more ideas??

0
jmingoAuthor Commented:
could it have something to do with the fact that i'm doing two mysql statements here.

selecting and then update within the if statement?? plus i'm only using mysql_free_result with the $result and not $result2

i tried to do both of them, but got mysql errors.

any ideas??
0
jmingoAuthor Commented:
i have isolated the problem to this code

in the database the content is "yes"

$yesvar = "yes";

if ($notify == $yesvar) {
$tonotify  = "jason@test.com";
$subjectnotify = "$name has accessed the e-mercial";
$messagenotify = "$name has accessed the e-mercial";
$headersnotify  = "MIME-Version: 1.0\r\n";
$headersnotify .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headersnotify .= "From: Server <jason@test.com>\r\n";
mail($tonotify, $subjectnotify, $messagenotify, $headersnotify);
}
0
Marcus BointonCommented:
There's nothing wrong with that code. Are you saying that if you comment that out then it all works, and if you put it back the problem reappears?

You can live without calling mysql_free_result at all - it will clean up automatically when the connection closes.

You should always quote values in SQL, and also apply addslashes for safety:

$query2 = "UPDATE test set viewnum='$viewnum' where id='".addslashes($id)."'";

Might be good to repost your current version.
0
jmingoAuthor Commented:
actually i have added a field for notify in the flash, and it comes up undefined, so i think that's where the problem is by i don't know why.. all the other fields work perfectly. i checked database names, variable names. thanks for your help so far, here's the updated code.

<?PHP

error_reporting(E_ALL);

$user = $_GET['user'];
$subject = $_GET['subject'];

$link = mysql_connect('localhost', 'test', 'test321')
   or die('Could not connect: ' . mysql_error());

mysql_select_db('test') or die('Could not select database');

$query = "SELECT * FROM test where name = '$user' and subject = '$subject' order by id desc limit 1";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

if ($rows = mysql_fetch_array($result)) {
     
$id = $rows['id'];
$name = $rows['name'];
$email = $rows['subject'];
$content = $rows['content'];
$notify = $rows['notify'];
$prod4 = $rows['prod4'];
$prod2 = $rows['prod2'];
$prod1 = $rows['prod1'];
$prod3 = $rows['prod3'];
$preandpost = $rows['preandpost'];
$filename = $rows['file'];
$background = $rows['background'];
$views = $rows['views'];
$viewnum = $rows['viewnum'];

$viewnum = $viewnum + 1;

$max = "max";

print("&prod1=$prod1");
print("&prod2=$prod2");
print("&prod3=$prod3");
print("&preandpost=$preandpost");
print("&home=$content");
print("&bg=$background");
print("&file=$filename");

//THIS MAKES THE FIELD IN FLASH UNDEFINED
print("&notify2=$notify");

if ($viewnum > $views) {
print ("&num=$max");
} else {
print ("&num=$viewnum");
}

$emailnum = $views - 5;

if ($viewnum == $emailnum) {
$to  = "jason@test.com";
$subject = "notification";
$message = "Your webpage to $name has been accessed - five more to go";
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Server <jason@test.com>\r\n";
mail($to, $subject, $message, $headers);
}

$yesvar = "yes";

//UNDEFINED PROBLEM - AREA BELOW - DOESN'T HAPPEN When this is commmented
//if ($notify == $yesvar) {
//$tonotify  = "jason@test.com";
//$subjectnotify = "Your webpage to $name has been accessed";
//$messagenotify = "Your webpage to $name has been accessed";
//$headersnotify  = "MIME-Version: 1.0\r\n";
//$headersnotify .= "Content-type: text/html; charset=iso-8859-1\r\n";
//$headersnotify .= "From: Server <jason@test.com>\r\n";
//mail($tonotify, $subjectnotify, $messagenotify, $headersnotify);
//}

$query2 = "UPDATE dest set viewnum='$viewnum' where id=$id";
$result2 = mysql_query($query2) or die ('Query failed: ' . mysql_error());

}

mysql_free_result($result);
mysql_close($link);

?>
0
Marcus BointonCommented:
For the $notify problem, I suggest you make sure that all your vars are populated - try a var_dump($rows) after you get it.

I still can't see any good reason for that code to cause a problem.
0
jmingoAuthor Commented:
i put this in

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
var_dump($rows);
if ($rows = mysql_fetch_array($result)) {
etc.

Notice: Undefined variable: rows in /var/www/html/dest/data.php on line 25
NULL

and it outputed all the fields, and at the end where notify should be it put this.

&bg=background2&file=/dest/upload/¬ify2=yes&num=max

¬ify2 - this is suppose to be notify2

any ideas now?? thanks again
0
jmingoAuthor Commented:
ok, i changed the name for notify2 to ntify because it wasn't liking the word not i think

anyways it works not, sometimes everything gets entered, but if i hit refresh in the browser like 1/3 times or so the values for ntify and numviews (both with if statements) go undefined and it breaks the flash webpage.

any ideas now? i'll up the points if we can get through this.

thanks
0
Marcus BointonCommented:
No surprise it didn't work - you're displaying before it's defined, so switch the lines:

if ($rows = mysql_fetch_array($result)) {
var_dump($rows);
0
jmingoAuthor Commented:
ok i did that, but when i hit refresh it still breaks roughly 50% of the time, brings up undefined in the numview and ntify fields..

0
jmingoAuthor Commented:
its very unpredictable. do you think it could have something to do with my flash actionscript?? here it is.

myData = new LoadVars();

myData.onLoad = function(){
      placeTheDataIntoTheRightPlace();//call the function
};

myData.load("data.php?user=" + _root.user + "&subject=" + _root.subject + "&notify=" + _root.notify);

placeTheDataIntoTheRightPlace = function(){
     
      home = myData.home;
      prod1 = myData.prod1;
      prod2 = myData.prod2;
      prod3 = myData.prod3;
      preandpost = myData.preandpost;
      numview = myData.num
      backgroundvar = myData.bg
      //ntifyvar = myData.ntify
      };

if (_root.highspeed == "no") {
      loadMovieNum("itworks2.swf", 11);
}

if (numview == "max") {
      loadMovieNum("itworks.swf", 9);
}
0
Marcus BointonCommented:
Well, before you start blaming flash, confirm that your PHP output is correct - try hitting the script with a web browser instead of your flash movie and you will see the strings that it's generating. If they look OK there, then you know that the Flash is to blame. I should have mentioned this before - var_dump's ouptut is very likely to screw up the flash movie!
0
jmingoAuthor Commented:
array(28) { [0]=> string(3) "337" ["id"]=> string(3) "337" [1]=> string(17) "jason@test.com" ["name"]=> string(17) "test.com" [2]=> string(7) "Testing" ["subject"]=> string(7) "Testing" [3]=> string(66) "Prod1 Prod4=1 Cuisine Prod4= Opener Prod4=3 Overview Prod4= " ["Prod4"]=> string(66) "Prod2 Video=1 Cuisine Prod4= Opener Prod4=3 Overview Prod4= " [4]=> string(1361) "

etc. etc.

" [9]=> string(3) "yes" ["notify"]=> string(3) "yes" [10]=> string(13) "/dest/upload/" ["file"]=> string(13) "/dest/upload/" [11]=> string(11) "background2" ["background"]=> string(11) "background2" [12]=> string(2) "20" ["views"]=> string(2) "20" [13]=> string(2) "47" ["viewnum"]=> string(2) "47" }

this is some of the output
0
Marcus BointonCommented:
OK - I don't know what your values mean, but you need to go through them and make sure they are all there (no NULL values) and contain what they should. Next you need to check that the formatted string that you're printing is correctly formed. If it is all repeatably correct, then delete the var_dump line and start looking at Flash to see if it's getting stuff wrong. First thing to do will be to display the raw text that flash has picked up before you try to extract the variables from it.

One thing to note that I mentioned in my first post - if flash is expecting the data to be in a URL style you probably need to apply urlencode() to every value that you're printing - otherwise a space, question mark, =, &, . and others could mess up the reading into Flash, which may explain your intermittent failures. e.g. 'jason@test.com' should be formatted as 'jason%40test.com'. apply it like this:

print "&file=".urlencode($filename);
0
jmingoAuthor Commented:
i've been reading that theres a undefined problem because the onLoad event handler is not being called.

i don't know how to fix this though. this would be in the flash actionscript.

0
jmingoAuthor Commented:
the problem was that the server was too slow to handle all the things it needed to do, i moved it to a fater server and it works flawlessly.

0
OzzModCommented:
Closed, 25 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.