?
Solved

If statement problem. Accessing a php file through flash

Posted on 2005-03-14
24
Medium Priority
?
308 Views
Last Modified: 2008-03-04
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);
}

?>
0
Comment
Question by:jmingo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 8
24 Comments
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13536438
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13536445
Ah, and this old problem:

if ($notify = "Yes") {

should be

if ($notify == "Yes") {
0
 

Author Comment

by:jmingo
ID: 13536833
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:jmingo
ID: 13536915
should it be before or after the

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

it seems to do it before and after.

0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13537245
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
 

Author Comment

by:jmingo
ID: 13537312
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
 

Author Comment

by:jmingo
ID: 13544253
i changed the while to if, and there's still the recurring undefined problem.

any more ideas??

0
 

Author Comment

by:jmingo
ID: 13544498
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
 

Author Comment

by:jmingo
ID: 13544874
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13545507
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
 

Author Comment

by:jmingo
ID: 13545826
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13546168
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
 

Author Comment

by:jmingo
ID: 13546492
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
 

Author Comment

by:jmingo
ID: 13546609
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13546655
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
 

Author Comment

by:jmingo
ID: 13546687
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
 

Author Comment

by:jmingo
ID: 13547315
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13548156
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
 

Author Comment

by:jmingo
ID: 13548368
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
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13548618
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
 

Author Comment

by:jmingo
ID: 13556489
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
 

Author Comment

by:jmingo
ID: 13643779
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
 

Accepted Solution

by:
OzzMod earned 0 total points
ID: 13686529
Closed, 25 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question