I'm working on a php script and using mysql databases to make a sports picks website. The users will submit their picks into a mysql database, and based on whether those picks are right or wrong, I want to give them a certain score.
The problem is of course the users will be making these picks BEFORE each sporting event so I need a way to update the page after each event to score users based on the information(picks) they gave in the table. I have everything scripted out including a FUNCTION called fun();
that would score users based on their picks. I tested the script, and after I submitted my picks I tried to add fun();
to the script and save it. The score table didn't update.
How can I update the PHP script? I hope all this makes sense. Here is the function code and the script I'm trying to add the function to.
Here is the Function. It tests pick1.
function fun()
{
//loop declare begin
$quer = "SELECT * FROM sffedorvsdan WHERE username = '$_SESSION[username]'";
if($quer_run = mysql_query($quer))
{
while($row = mysql_fetch_assoc($quer_run))
{
$pick1 = $row['pick1'];
$pick2 = $row['pick2'];
$pick3 = $row['pick3'];
$pick4 = $row['pick4'];
$pick5 = $row['pick5'];
$pick6 = $row['pick6'];
//loop end EXCEPT CLOSE
$user = $_SESSION['username'];
if($pick1 == '11')
{
$score = mysql_query("UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10), wins = (wins + 1), games =(games + 1) WHERE username = '$user'");
return $score;
}
else if($pick1 == '21')
{
$score2 = mysql_query("UPDATE stats SET score = (score - 5), mmascore = (mmascore - 5), games = (games + 1) WHERE username = '$user'");
return $score2;
}
}
}else{
echo mysql_error();
}
}
?>
Heres the main script including html and where I want fun(); to go. In the //Pick Start comments only pay attention to //Pick 1 Start as it's the only one I'm trying to test.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
session_start();
if ($_SESSION['username'])
{
$connect2 = //WHERE I CONNECT TO DATABASE
$sql = "SELECT * FROM access WHERE username = '$_SESSION[username]'";
$query = mysql_query($sql,$connect2);
if ($query) {
while ($row = mysql_fetch_assoc($query))
{
$activated = $row['sf'];
if ($activated!='0')
{
die("You've already submitted your answers<br><a href='mmanav.php'>BACK</a>");
}
}
}
$user = "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>";
}
else
{
die("You must be logged in!");
}
//PICK VARIABLES
$dan_status = 'unchecked';
$fedor_status = 'unchecked';
$paul_status = 'unchecked';
$tyron_status = 'unchecked';
$tim_status = 'unchecked';
$robbie_status = 'unchecked';
$tarec_status = 'unchecked';
$scott_status = 'unchecked';
$marloes_status = 'unchecked';
$miesha_status = 'unchecked';
//function file
include('fun.php');
//function file end
if (isset($_POST['submit']))
{
$connect = //WERE I CONNECT TO DATABASE
//PICK 1 START
$selected_radio = $_POST['fighter'];
if ($selected_radio == 'dan')
{
$dan_status = 'checked';
mysql_query("INSERT INTO sffedorvsdan (id,username,pick1) VALUES ('','$_SESSION[username]','11')"); //insert table data picks
}
else if ($selected_radio == 'fedor')
{
$fedor_status = 'checked';
mysql_query("INSERT INTO sffedorvsdan (id,username,pick1) VALUES ('','$_SESSION[username]','21')"); //insert table data picks
}
//PICK 2 START
$selected2_radio = $_POST['fighter2'];
if ($selected2_radio == 'paul')
{
$paul_status = 'checked';
/*mysql_query("UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10)
WHERE username = '$_SESSION[username]'");*/
}
else if ($selected2_radio == 'tyron')
{
$tyron_status = 'checked';
/*mysql_query("UPDATE stats SET score = (score - 5), mmascore = (mmascore - 5)
WHERE username = '$_SESSION[username]'");*/
}
//PICK 3 START
$selected3_radio = $_POST['fighter3'];
if ($selected3_radio == 'tim')
{
$tim_status = 'checked';
/*mysql_query("UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10)
WHERE username = '$_SESSION[username]'");*/
}
else if ($selected3_radio == 'robbie')
{
$robbie_status = 'checked';
/*mysql_query("UPDATE stats SET score = (score - 5), mmascore = (mmascore - 5)
WHERE username = '$_SESSION[username]'");*/
}
//PICK 4 START
$selected4_radio = $_POST['fighter4'];
if ($selected4_radio == 'tarec')
{
$tarec_status = 'checked';
//mysql_query("UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10)
//WHERE username = '$_SESSION[username]'");
}
else if ($selected4_radio == 'scott')
{
$scott_status = 'checked';
//mysql_query("UPDATE stats SET score = (score - 5), mmascore = (mmascore - 5)
//WHERE username = '$_SESSION[username]'");
}
//PICK 5 START
$selected5_radio = $_POST['fighter5'];
if ($selected5_radio == 'marloes')
{
$marloes_status = 'checked';
//mysql_query("UPDATE stats SET score = (score + 10), mmascore = (mmascore + 10)
//WHERE username = '$_SESSION[username]'");
}
else if ($selected5_radio == 'miesha')
{
$miesha_status = 'checked';
//mysql_query("UPDATE stats SET score = (score - 5), mmascore = (mmascore - 5)
//WHERE username = '$_SESSION[username]'");
}
mysql_query("UPDATE access SET sf ='1'
WHERE username = '$_SESSION[username]'");
fun(); //WHERE FUN WILL GO AFTER I RUN THE SCRIPT ONCE
die("Your picks have been submitted!<br><a href='gamenav.php'>Return</a>");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="home.css" />
</head>
<body>
<div class="container">
<div class="log">
<?php echo $user; ?>
</div>
<div class="logo">
<img src="IB SportsTV Logo.png" width="240px" height="180px"/>
</div>
<FORM name ="form" method ="POST" action ="picks.php">
<div class="mainform">
<Input type = 'Radio' Name ='fighter' value= 'dan'
<?PHP print $dan_status; ?>
>Dan Henderson<br>
<img src="Dan_Henderson.jpg" width="100" height="100"/><br>
<Input type = 'Radio' Name ='fighter' value= 'fedor'
<?PHP print $fedor_status; ?>
>Fedor Emelianenko<br>
<img src="Fedor_Emelianenko.png" width="100" height="100"/><br><br><br>
</div>
<div class="form2">
<Input type = 'Radio' Name ='fighter2' value= 'paul'
<?PHP print $paul_status; ?>
>Paul Daley<br>
<img src="Paul_Daley.png" width="100" height="100"/><br>
<Input type = 'Radio' Name ='fighter2' value= 'tyron'
<?PHP print $tyron_status; ?>
>Tyron Woodley<br>
<img src="Tyron_Woodley.jpg" width="100" height="100"/><br><br><br>
</div>
<div class="form3">
<Input type = 'Radio' Name ='fighter3' value= 'tim'
<?PHP print $tim_status; ?>
>Tim Kennedy<br>
<img src="Tim_Kennedy.png" width="100" height="100"/><br>
<Input type = 'Radio' Name ='fighter3' value= 'robbie'
<?PHP print $robbie_status; ?>
>Robbie Lawler<br>
<img src="Robbie_Lawler.png" width="100" height="100"/><br><br><br>
</div>
<div class="form4">
<Input type = 'Radio' Name ='fighter4' value= 'tarec'
<?PHP print $tarec_status; ?>
>Tarec Saffiedine<br>
<img src="Tarec_Saffiedine.png" width="100" height="100"/><br>
<Input type = 'Radio' Name ='fighter4' value= 'scott'
<?PHP print $scott_status; ?>
>Scott Smith<br>
<img src="Scott_Smith.png" width="100" height="100"/><br><br><br>
</div>
<div class="form5">
<Input type = 'Radio' Name ='fighter5' value= 'marloes'
<?PHP print $marloes_status; ?>
>Marloes Coenen<br>
<img src="Marloes_Coenen.jpg" width="100" height="100"/><br>
<Input type = 'Radio' Name ='fighter5' value= 'miesha'
<?PHP print $miesha_status; ?>
>Miesha Tate<br>
<img src="Miesha_Tate.png" width="100" height="100"/><br>
</div>
<div cl开发者_开发问答ass="submitbutton">
<P>
<Input type = "Submit" Name = "submit" VALUE = "SUBMIT PICKS">
</FORM>
</div>
</div>
</body>
</html>
There is a lot going on there, so I think I'm just going to leave a general outline as to how I would handle this for a small website (a website with a large amount of traffic would include cache tables, cron jobs, and all sorts of funness):
- User logs in and goes to that page.
- Compare predictions for past events with the winners.
- Calculate scores.
- Display them.
My select statement might be (if the table EVENTS
was for the events, and PREDICTIONS
is for predictions)
SELECT SUM( WINS ) FROM (
SELECT
-- if they are correct, they will get 1, otherwise 0
IF( EVENTS.WINNER = PREDICTIONS.CHOICE, 1, 0 ) AS WINS
FROM
-- since you need information from two tables, you need to use JOIN.
EVENTS, PREDICTIONS
-- assuming that EVENTS.ID and PREDICTIONS.EVENT_ID are related through
-- something called a foreign key
ON ( EVENTS.ID = PREDICTIONS.EVENT_ID )
WHERE
-- an entry in EVENTS should only have a winner if the event has passed
-- so there is no need for a time calculation.
-- ... unless someone is taking a dive... ;-)
EVENTS.WINNER IS NOT NULL AND
PREDICTIONS.USER_ID = 'userID' )
That query will mean that you can always have a current total which directly relates the user with his number of correct predictions. All he does is update the prediction. All you do is update the EVENTS.WINNER.
Actually, I would explicitly avoid storing the score without tracking this data as well -- if you have the raw, non-calculated data, you can always populate something based on it (such as my example).
Try to use this line instead:
$quer = "SELECT * FROM sffedorvsdan WHERE username = '{$_SESSION['username']}'";
精彩评论