PDA

View Full Version : AS string INTO php then use as ind. variables


twistoff
02-12-2005, 11:14 PM
HI,

I'm trying to rework the voting (http://www.actionscript.org/tutorials/intermediate/Vote_system_flash_php_mySQL/index.shtml) script written by August Jørgensen to work with multiple questions.

I've successfully output the string (choiceFour=19&choiceThree=10&choiceTwo=5&choice=2) from the Flash questions with help from scot in the ActionsScript forum and am now trying to import the string into php.

It seems no matter what I try the php script only picks up one of the four answers/variables from the four Flash questions an only updates one field (and usually the wrong one) to the DB - it should update four fields.

Any ideas? Here's what I have - this is my latest broken version - using the switch command (only because I gave up on if, esleif!).

//Bring in the answers from the four Flash Questions
$choice = $_POST['choice'];
$choiceTwo = $_POST['choiceTwo'];
$choiceThree = $_POST['choiceThree'];
$choiceFour = $_POST['choiceFour'];
//User, password & database
$user="theusername";
$password="thepassword";
$database="thedatabase";
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to connect to database");

// Question 1 using the php switch feature to check what choice was made in the frist Flash radio group
switch ($choice)
{
case 1
$query="UPDATE votesystem SET vote1=vote1+1";
break;
case 2
$query="UPDATE votesystem SET vote2=vote2+1";
break;
case 3
$query="UPDATE votesystem SET vote3=vote3+1";
break;
}

// Question 2 using the php switch feature to check what choice was made in the second Flash radio group
switch ($choiceTwo)
{
case 4
$query="UPDATE votesystem SET vote4=vote4+1";
break;
case 5
$query="UPDATE votesystem SET vote5=vote5+1";
break;
case 6
$query="UPDATE votesystem SET vote6=vote6+1";
break;
}

// Question 3 using the php switch feature to check what choice was made in the Flash pulldown menu
switch ($choiceThree)
{
case 8
$query="UPDATE votesystem SET vote8=vote8+1";
break;
case 9
$query="UPDATE votesystem SET vote9=vote9+1";
break;
case 10
$query="UPDATE votesystem SET vote10=vote10+1";
break;
case 11
$query="UPDATE votesystem SET vote11=vote11+1";
break;
case 12
$query="UPDATE votesystem SET vote12=vote12+1";
break;
case 13
$query="UPDATE votesystem SET vote13=vote13+1";
break;
case 14
$query="UPDATE votesystem SET vote14=vote14+1";
break;
case 15
$query="UPDATE votesystem SET vote15=vote15+1";
break;
case 16
$query="UPDATE votesystem SET vote16=vote16+1";
break;
case 17
$query="UPDATE votesystem SET vote17=vote17+1";
break;
}
// Question 4 using the php switch feature to check what choice was made in the fourth Flash radio group
switch ($choiceFour)
{
case 18
$query="UPDATE votesystem SET vote18=vote18+1";
break;
case 19
$query="UPDATE votesystem SET vote19=vote19+1";
break;
}

Any direction would be Greatly appreciated, thanks.

Rob

Laguana
02-12-2005, 11:50 PM
It looks like you're just overwriting hte $query variable in each switch thing, so only the last one actually does anything. You might want to have 4 variables or add the strings to the same variable. Just overwriting the same variable without doing anything in between is pointless.

Dark_Element
02-13-2005, 02:52 AM
laguana is right. you should let it query mysql right after each block of switch

also sometimes string/integer types gets confused with PHP when the var is passed through POST or GET methods... try using the intval() function on the vars that you are recieving. eg: to get $_POST['foo'] you write intval($_POST['foo'])

the problem is that the switch thingie will always select something even if it dont meet the quiteria in this case its proberbly selecting the last query since php do not automatically convert strings that contain only integer values to actual integers

you should try adding a last case to your switchs called default: that ways you can catch unexpected results. anything else php will just select the last possible case

twistoff
02-13-2005, 02:46 PM
Thanks Folks! I appreciate the help.

OK, I've tried to add more queries (can you tell I'm new at this :confused: ) as suggested and in the process I tried to streamline the code down a bit and ditch the SWITCH as well.

But, of course, it still doesn't work...

What is the proper way to include my variables into the queries? Where I once had:
switch ($choice)
{
case 1
$query1="UPDATE votesystem SET vote1=vote1+1";
break;
case 2
$query1="UPDATE votesystem SET vote2=vote2+1";
break;
case 3
$query1="UPDATE votesystem SET vote3=vote3+1";
default:
}

now I have (here's the new full BROKEN version):


$choice = intval($_POST['choice']);
$choiceTwo = intval($_POST['choiceTwo']);
$choiceThree = intval($_POST['choiceThree']);
$choiceFour = intval($_POST['choiceFour']);
//User, password & database
$user="theusername";
$password="thepassword";
$database="thedatabase";
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to connect to database");



// Question 1 - check what choice was made in the frist Flash radio group and update the DB
mysql_query($query1);
$query1=("UPDATE votesystem SET vote"$choice"=vote"$choice"+1");

// Question 2 - check what choice was made in the second Flash radio group and update the DB
mysql_query($query2);
$query2=("UPDATE votesystem SET vote"$choiceTwo"=vote"$choiceTwo"+1");

// Question 3 - check what choice was made in the Flash pulldown menu and update the DB
mysql_query($query3);
$query3=("UPDATE votesystem SET vote"$choiceThree"=vote"$choiceThree"+1");

// Question 4 - check what choice was made in the fourth Flash radio group and update the DB
mysql_query($query4);
$query4=("UPDATE votesystem SET vote"$choiceFour"=vote"$choiceFour"+1");


Thanks again in advance - as always, it is greatly appreciated!

Rob

Dark_Element
02-14-2005, 08:37 AM
you forgot dots before and after the vars in your queries. eg: "UPDATE votesystem SET vote"$choiceThree"=vote"$choiceThree"+1" needs to be either:
"UPDATE votesystem SET vote".$choiceThree."=vote".$choiceThree."+1"
or
"UPDATE votesystem SET vote$choiceThree=vote$choiceThree+1"

Also i strongly advice you to change your double quotes to single quotes unless you are using escape characters or using the second suggested method...

Laguana
02-14-2005, 11:24 AM
Also, shouldn't you be setting the variable before you query with it? Otherwise you're querying with a blank string, then defining a string which does nothing.

twistoff
02-14-2005, 12:17 PM
Thanks again, folks!

I'll try the suggestions, and report back!

I'm reading the manuals and tuturials, honest (they be big, though!)!

Rob

twistoff
02-14-2005, 06:57 PM
Finally - Thanks!

For those who'd like to see the final (working) version:


$choice = intval($_POST['choice']);
$choiceTwo = intval($_POST['choiceTwo']);
$choiceThree = intval($_POST['choiceThree']);
$choiceFour = intval($_POST['choiceFour']);
//User, password & database
$user="*******";
$password="******";
$database="******";
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to connect to database");

// Question 1 - check what choice was made in the frist Flash radio group and update the DB
$query1=("UPDATE votesystem SET vote$choice=vote$choice+1");
mysql_query($query1);

// Question 2 - check what choice was made in the second Flash radio group and update the DB
$query2=("UPDATE votesystem SET vote$choiceTwo=vote$choiceTwo+1");
mysql_query($query2);

// Question 3 - check what choice was made in the Flash pulldown menu and update the DB
$query3=("UPDATE votesystem SET vote$choiceThree=vote$choiceThree+1");
mysql_query($query3);

// Question 4 - check what choice was made in the fourth Flash radio group and update the DB
$query4=("UPDATE votesystem SET vote$choiceFour=vote$choiceFour+1");
mysql_query($query4);


Once again, a very LARGE thanks, Folks!

Rob

Dark_Element
02-15-2005, 09:05 AM
errh mate... why bother declaring a new variable when your only going to only use it once?

i suggest you just pass the string straight through the mysql_query() function.

btw. your welcome lol