Hello everybody, I am new to Flash and PHP and I need a little help. Wonderful guys like you are making it possible for newbies like me to enjoy programming. Please help. Thanks.

I am trying to pass a text value from flash to php which would be picked up by an SQL query to select all from a table where book_id is equal to the variable value I have passed from Flash. The result would then be displayed via xml to flash. It works all right when I dont put a constraint and just select all but I need to select particular news items so that readers of a news item can view comments for the particular story.I have used all the tricks that I know but i still get undefined in my result. Could u please explain why?

Here are the codes please.

//Flash

on(release){
gotoAndStop(2);
contactVars=new LoadVars();
contactVars.theId=ivalue.text;
ontactVars.onLoad=function(ok){

if(ok){

//

}else{

//

}

				}

contactVars.sendAndLoad('comments.php',contactVars,'POST');

}

//PHP

<?PHP
$id=$_POST['theId'];

$link = mysql_connect("mysql","pojobauk","sakayayA");
mysql_select_db("newsdatabase");

$query= "SELECT * FROM comments WHERE book_id='$id' ORDER BY comment_id DESC";
$results = mysql_query($query);

echo "<?xml version=\"1.0\"?>\n";
echo "<comments>\n";

while($line = mysql_fetch_assoc($results)) {
echo"<group>\n";
echo "<name>" . $line["name"] . "</name>\n";
echo "<subject>" . $line["subject"] . "</subject>\n";
echo "<comment>" . $line["comment"] . "</comment>\n";
echo"</group>\n";
}

echo "</comments>\n";

//mysql_close($link);

?>

As I said I only get undefined error message on display in flash. Thanks in advance.

    if you work with mysql databases, first: lets handle the errors, ehck the inputs values, and save the reports in a file. In errorlog.txt if you see error, then check them.

    <?php
    function die2($error)
    {
    file_put_contents("errorlog.txt" , $error );
    }
    
    $results = mysql_query($query) or die2(mysql_error() . $query );
    ?>
    

    write your program to work with $GET variables, test the outputs of your php file while it generated the correct values, then set back to $POST your program.

    if you finish, close all error reporting in your php programs ( unwanted output still mess your flash program) , write your php program on a better quality not to allow undefined variables.

    $id=$_POST["something"];

    is still a bad designed code. Its not safe from sql injection.

    if(isset($_POST["myID"]))
    {
    do something...
    $sql=sprintf("select * from `table` where id=%d" , $_POST["myID"]);
    mysql.........
    }

      Thank you very much djjjozsi I will make the necessary corrections.

        I have made changes to my PHP but still cant get anything from the database. Please note 'theId' is accepting an integer from flash from ivalue.text. Can any one help please?

        //PHP CODE:

        <?PHP

        $link = mysql_connect("somesql","somesite","somepassword");
        mysql_select_db("newsdatabase");

        if (isset($POST['theId'])){
        $ivalue = $
        POST['theId'];
        $query = "SELECT * ".

        "FROM comments " .

        "WHERE book_id = '$ivalue' ";

        $result = mysql_fetch_array(mysql_query($query));

        echo "<?xml version=\"1.0\"?>\n";
        echo "<comments>\n";

        while($line = mysql_fetch_assoc($result)) {
        echo"<group>\n";
        echo "<name>" . $line["name"] . "</name>\n";
        echo "<subject>" . $line["subject"] . "</subject>\n";
        echo "<comment>" . $line["comment"] . "</comment>\n";
        echo"</group>\n";
        }

        echo "</comments>\n";

        }else{
        return NULL;
        }

        ?>

          Classic problem. You retrieve the first (and probably only?) row from the result set here:

          $result = mysql_fetch_array(mysql_query($query));

          but then, before you ever use the data, you define a new variable that loops on through the rest of the result set (starting at row #2, since you already retrieved the first row) here:

          while($line = mysql_fetch_assoc($result)) {

          So, some questions to ponder:

          • Why do you retrieve the data of the first row in $result but then never use it?

          • Is book_id common to multiple rows? If not, then you should only expect one (or zero) rows to be returned. In that case, why do you have a while() loop (or any sort of loop for that matter)?

            Thanks bradgrafelman: The book_id is a foreign key in comments table and so it is not unique hence i am searching for all comments of a particular book_id. Besides I think I got the $result wrong. I have changed it to this:

            $result = mysql_query($query);

            echo "<?xml version=\"1.0\"?>\n";
            echo "<comments>\n";

            while($line = mysql_fetch_assoc($result)) {
            //rest of code as above
            }

            However I still get nothing to flash. Please note that when I delcare a variable for $ivalue in php it works. I think it is the variable from flash or something. Am I retrieving the variable well from fash? Thanks in advance.

              MqTontoh wrote:

              Am I retrieving the variable well from fash?

              No idea; that would depend on your Flash application.

              You can use [man]print_r/man on both $POST and $GET, though, to see what each contains.

                I have done several changes to my php coding. When I change $theId=$_POST['theId']; tp $theId=12;(for example) it works fine but otherwise i get undefined. I have also tested to see if the variable is received and it is received because I can echo the variable value back to flash at the line// echo'&sent=...' Can you please tell me what is wrong? Is anything wrong with my coding still? Thanks in advance.

                //PHP

                <?php
                
                $theId =$_POST['theId'];
                $id=$theId;
                
                
                $username="";
                $password="";
                $database="";
                $hostname = "";
                
                mysql_connect($hostname, $username, $password) 
                	or die("Unable to connect to mysql");
                //print "connected to mysql<br>";
                mysql_select_db("newsdatabase") 
                	or die("Could not access comments database");
                $query="SELECT * FROM comments where book_id='$id'";
                $results = mysql_query($query);
                if(mysql_query($query)){   
                $sent = "Entry Successful"; //echo '&sent=Comments selected123!'.$id; #echo"=".$sent;
                #print "successfully inserted record"; } else { echo 'Failed to select comments. Error logged.'; #echo"=".$sent;
                } echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; echo "<comments>\n"; while($line = mysql_fetch_assoc($results)) { echo "<group>\n"; echo "<name>" . $line["name"] . "</name>\n"; echo "<subject>" . $line["subject"] . "</subject>\n"; echo "<comment>" . $line["comment"] . "</comment>\n"; echo "</group>\n"; } echo "</comments>\n"; mysql_close(); ?>

                //FLASH

                on(release){
                	gotoAndStop(2);
                	contactVars=new LoadVars();
                	contactVars.theId=ivalue.text;
                	contactVars.onLoad=function(ok){
                				if(ok){
                					//load worked
                					//display message from php
                					error_txt.htmlText=this.sent
                					}else{
                						//load didnt work - display error
                						error_txt.htmlText='Could not send comment reference.'
                						}
                
                				}
                contactVars.sendAndLoad("comments.php",contactVars,"POST");
                
                }

                  Did you do a print_r() on $_POST to see what was being sent to PHP?

                    while you'r testing you should save the erro log into a file and see...

                    <?php
                    
                    function log_error($error)
                    {
                    file_put_contents("errorlog.txt" , $error);
                    die();
                    }
                    
                    if(empty($_POST['theId']))
                    log_error("ID is empty!");
                    
                    $results = mysql_query($query) or log_error(mysql_error() . $query);
                    if(mysql_num_rows($results)>0)
                    {
                    echo '<?'."xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
                    ... rest of your code to fetch the results.
                    }
                    else
                    die("&sent=No_result");
                    ?>
                    
                      Write a Reply...