PHP脚本执行后PHP更新mysql数据库

时间:2011-07-15 01:30:42

标签: php mysql

我正在开发一个PHP脚本并使用mysql数据库制作一个体育选秀网站。用户将他们的选择提交到mysql数据库,并根据这些选择是对还是错,我想给他们一定的分数。

问题当然是用户将在每次体育赛事之前制作这些选秀权,所以我需要一种方法来在每次比赛后更新页面,根据他们在表格中提供的信息(选秀权)对用户进行评分。我编写了所有内容,包括一个名为fun();的FUNCTION,它会根据用户的选择对用户进行评分。我测试了脚本,在我提交选择后,我尝试将fun();添加到脚本中并保存。分数表没有更新。

如何更新PHP脚本?我希望这一切都有道理。这是函数代码和我试图将函数添加到的脚本。

这是功能。它测试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();
  }
}    
?>

继承主要脚本,包括html和我想要的乐趣();去。在// Pick Start评论中只注意//选择1开始,因为它是我试图测试的唯一一个。

<!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 class="submitbutton">

<P>
<Input type = "Submit" Name = "submit" VALUE = "SUBMIT PICKS">
</FORM>

</div>


</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

请尝试使用此行:

$quer = "SELECT * FROM sffedorvsdan WHERE username = '{$_SESSION['username']}'";

答案 1 :(得分:0)

那里有很多事情,所以我想我只是要留下一个关于我如何为一个小网站处理这个问题的大纲(一个拥有大量流量的网站将包括缓存表,cron工作和各种乐趣):

  1. 用户登录并转到该页面。
  2. 将过去事件的预测与获胜者进行比较。
  3. 计算分数。
  4. 显示它们。
  5. 我的select语句可能是(如果表EVENTS用于事件,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' )
    

    该查询意味着您可以始终拥有一个当前总数,该总数直接将用户与其正确预测数量相关联。他所做的就是更新预测。您所做的只是更新EVENTS.WINNER。

    实际上,我会明确避免存储分数而不跟踪这些数据 - 如果你有原始的非计算数据,你总是可以根据它填充一些东西(比如我的例子)。