PHP / MySql Update problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Heppa
Forum Newbie
Posts: 1
Joined: Sun Aug 23, 2009 1:47 pm

PHP / MySql Update problems

Post by Heppa »

Hi guys,

I'm trying to update a field in my database based on a variables value that comes from a Flash script. It seem to work just fine at first glace but for some weird reason the value is set to 0 in the database afterwards. I have no clue as to why it would do that.

Basically I'm trying to store where the user exited a particular project made in Flash. I have some code in Flash that will send the username and the lastSlide variable to a PHP-script.

My Flash code is like this:

Code: Select all

 
this.stop();
 
 
// ---
// PHP file
// ---
_global.php_file = "user.php";
 
// set up LSO
local_data = SharedObject.getLocal("loggedInUser", "/");
 
// take the username and store it in the LSO for later use.
loggedInUser = local_data.data.username;
 
// grab the current slide
slideNo = _root.rdinfoCurrentSlide;
 
 
myVars2 = new LoadVars();
myVars2.username = loggedInUser;
myVars2.slideNumber = slideNo;
myVars2.action = 'bookmark';
myVars2.sendAndLoad(php_file, myVars2, 'POST');
myVars2.onLoad = function()
    {
        if(this.error != undefined)
        {
            status_txt.text = this.error;
        } else {
            status_txt.text = this.error;;
        }
            
    }
       

My PHP code looks like this: (I cut out the other functions that work fine)

Code: Select all

<?
require_once('conf.inc.php');
require_once('functions.php');
 
 
// ---- 
// bookmark / resume function
// ----
 
function bookmark($username,$slideNumber)
{
   GLOBAL $db, $table;
   $username = trim($username);
   $slideNumber = trim($slideNumber);
 
   $query = @mysql_query("UPDATE $table SET lastSlide = $slideNumber WHERE userName = '$username'");
   if(!$query)
   {
      return "error=" . mysql_error();
   } else {
      return "user=Bookmark set";
   }
}
 
 
 
if(isset($HTTP_POST_VARS["action"]))
{
   switch($HTTP_POST_VARS["action"])
   {
      case "register":
         $result = register($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass'],$HTTP_POST_VARS['email'],$HTTP_POST_VARS['question'],$HTTP_POST_VARS['answer']);
         print $result;
         break;
      case "login":
         $result = login($HTTP_POST_VARS['username'],$HTTP_POST_VARS['pass']);
         print "user=" . $result;
         break;
      case "forget":
         $result = forget($HTTP_POST_VARS['email']);
         print $result;
         break;
      case "new_password":
         $result = new_password($HTTP_POST_VARS['username'],$HTTP_POST_VARS['email']);
         print $result;
         break;
      case "bookmark":
         $result = bookmark($HTTP_POST_VARS['username'],$HTTP_POST_VARS['slideNumber']);
         print $result;
         break; 
   }
}
?>

My database setup is like this:

Code: Select all

Feltnavn Datatype Nulværdi Standardværdi 
userID  int(20)  Nej      
userName  varchar(15) Nej  0    
userPassword  varchar(32) Nej  0    
userMail  varchar(255) Nej      
userQuestion  varchar(255) Nej      
userAnswer  varchar(255) Nej      
courseCompleted  varchar(15) Nej      
quizScore  int(10) Nej  0    
dateCompleted  varchar(15) Nej      
dateStarted  varchar(15) Nej      
lastSlide  int(10) Nej      
 
It connects just fine to the database and for each page I can see that it sets the bookmark that I want. However, once I login with a different username then that bookmark will be set correctly but the one for the previous user is set to "0" in the database.


I'm not exactly a PHP guru but I tried for the last 5 hours trying to figure out what goes wrong. I tried saving the value as an INT, a string, in a different field of the database, using the intval() etc..

Anyone that can see what I did wrong and point me in the right direction?

Best regards,
Michael
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: PHP / MySql Update problems

Post by akuji36 »

Hello I worked on a similar problem the other day.

To solve it working in Flash 8(code below):
1. variable was set to zero
2. new field-- gave it an instance name(txtScore)
set it to variable(score)
3. using loadvars --send it to php
4. php send to mysql using update function
in mysql set the recieving field ="score" to type="int"
5. look into mysql table to check for update

Code: Select all

var score:Number = 0;
txtScore.text = String(score);
Post Reply