2013-08-30 173 views
0

嗨,我很難理解會話如何工作。我試圖寫一個程序,將多次請求輸入一個數字,每一個數字輸入編程時間打印出:商店變量會話php

  1. 這個數字
  2. 所有號碼的總和進入
  3. 在多少次的計數我提交

這是我到目前爲止的代碼:

<?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; 
     } 
    } 
?> 

我試着來存儲我$count變量,因此每次我提交時它輸出數字的總和進入

+0

FYI:'$ CNT = 0; '然後'isset($ cnt)'將是** false **! – undone

回答

0

在開始使用的是不確定的變量$ count 。它編輯到:

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

然後添加一個新行:

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

或第二部分編輯:

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

第一個如果可以安全地提交到thedailywtf.com;)認真,你不能想到更好的實施?提示:反轉isset檢查,從真到假 –

+0

我剛編輯他的代碼,現在我有簡短的版本。 –

+0

謝謝,這有助於很多 – user2732815

0
<?php 
    if(isset($_POST["submit"])) 
    { 
     $number = $_POST['number']; 
     If (is_numeric($number)) 
     { 
      $count = $_session['count'] + $number; 
      $_session['count'] = $count; 
      print "Last number entered: ".$number; 
      print "<br>Total internal numbers: ".$count; 
     } 
    } 
?> 
2

您設置$_session['count'] = $count;您有一個名爲$count變量之前。你必須更新會話值,你已經設置$count等之後:

$count = $_session['count'] + $number; 
$_session['count'] = $count; 

然後,您可以取代你的腳本的頂部:

if(!isset($_session['count'])) 
{ 
    $_session['count'] = 0; 
} 
+1

+1解釋爲什麼代碼不能像問題提出者期望的那樣工作。 – Fluffeh

+1

啊好的非常感謝很多:) – user2732815

0

這裏的工作示例。 [TESTED]

您必須分配$_SESSION['count']=$count;

<?php 
session_start(); 
//Code commented as not required. 
/*if(isset($_session['count'])) 
{ 
    $_session['count'] = $count; 
}else{ 
    $_session['count'] = 0; 
} 
*/?> 

<html> 
<head> 
    <title>number</Title> 
    <style> 
    </style> 
    <body> 
    <form action = "" 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; 
     $_SESSION['count']=$count; 
     print "Last number entered: ".$number; 
     print "<br>Total internal numbers: ".$count; 
    } 
} 
?> 
+0

不要說在開始的代碼不是必需的。 '$ count = $ _SESSION ['count'] + $ number;'將在第一次調用該腳本時通過未定義的索引通知。 – insertusernamehere