duankanjian4642 2013-08-30 12:17
浏览 34
已采纳

存储变量会话php

Hi, i am having trouble in understanding how session works. I am trying to write a program that will repeatedly request a number to be entered and each time the number is entered the program is to print out:

  1. that number
  2. the sum of all the numbers entered
  3. the count in how many times I submitted

This is my code so far:

<?php
session_start();
if(isset($_session['count']))
{
    $_session['count'] = $count;
}else{
    $_session['count'] = 0; 
}
?>

<html>
    <head>
        <title>number</Title>
            <style>
            </style>
        <body>
            <form action = "numbers.php" method = "post">
                Numbers: <input type "text" name = "number" size = "6"/>
                <input type = "submit" value = "submit" name = "submit"/>
                <p>
            </form>
        </body> 
    </head>
</Html>
<?php   
    if(isset($_POST["submit"]))
    {
        $number = $_POST['number'];
        If (is_numeric($number))
        {   
            $count = $_session['count'] + $number;
            print "Last number entered: ".$number;
            print "<br>Total internal numbers: ".$count;
        }
    }   
?>

Im trying to store my $count variable so each time i submit it outputs the total sum of the numbers entered

  • 写回答

4条回答 默认 最新

  • douran7929 2013-08-30 12:22
    关注

    On the start you are using undefined variable $count. Edit it to:

    if(!isset($_session['count']))
        $_session['count'] = 0; 
    

    Then add a new line:

    If (is_numeric($number))
    {   
        $count = $_SESSION['count'] + $number;
        $_SESSION['count'] = $count;
        print "Last number entered: ".$number;
        print "<br>Total internal numbers: ".$count;
    }
    

    OR second part edit to:

    If (is_numeric($number))
    {   
        $_SESSION['count'] += $number;
        print "Last number entered: ".$number;
        print "<br>Total internal numbers: ".$_SESSION['count'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退