Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Php通过Id递增在文件中写入数据_Php - Fatal编程技术网

如何使用Php通过Id递增在文件中写入数据

如何使用Php通过Id递增在文件中写入数据,php,Php,当用户打开页面时,有一个递增1的计数变量。count变量和数据都写入一个文件中 问题与会话变量有关,该变量不是递增的。每次加载页面时,会话中都应该有一个增量,该增量应该放在文件中 <?php session_start(); $count=1; if(!isset($_POST['submit'])){ $txt = "abc.txt"; $_SESSION["hello"] = $count++; if (isset($_POST['field1']) &

当用户打开页面时,有一个递增1的计数变量。count变量和数据都写入一个文件中

问题与会话变量有关,该变量不是递增的。每次加载页面时,会话中都应该有一个增量,该增量应该放在文件中

<?php
session_start();
$count=1;
if(!isset($_POST['submit'])){
    $txt = "abc.txt"; 
    $_SESSION["hello"] = $count++;
    if (isset($_POST['field1']) && isset($_POST['field2'])) { 
        $_SESSION["favanimal"] = "cat";
        $fh = fopen($txt, 'a'); 
        $txt=$a . '-' .$_POST['field1'].' - '.$_POST['field2']; 

        fwrite($fh,$txt); // Write information to the file
        fclose($fh); // Close the file

    }
    echo $_SESSION["hello"];
}

?>

HTML代码是:

<form action="index.php" method="POST">
    <input name="field1" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

您正在存储hello会话变量,但没有从中读取。实际上,每次都将其设置为1

更改此项:

$_SESSION["hello"] = $count++;
致:

请注意,当前您没有将此值写入文件,而只是回显它。我想你知道该怎么办。它可以是这样一行(在文件关闭之前):


注意:$a未在您提供的代码段中定义。在表达式中使用它之前,请确保它有一个值。

您不需要$count变量。如果保持这样,则每次加载页面时$count都将变为1。我想这就是你想要的:

<?php
session_start();
//$count=1;

//change session variable here!!!
if(isset($_SESSION["hello"]))
    $_SESSION["hello"]++;
else 
    $_SESSION["hello"] = 1;

if(isset($_POST['submit'])){//you don't need ! here
    $txt = "abc.txt"; 
    if (isset($_POST['field1']) && isset($_POST['field2'])) { 
    $_SESSION["favanimal"] = "cat";
    $fh = fopen($txt, 'a'); 

    $txt=$_POST['field1'].' - '.$_POST['field2']; 

    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file

    }
}

echo $_SESSION["hello"];

?>

<form action="index.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

哪里定义了
$a
?我已经详细阐述了这个问题。对不起,问题中我的英语不是很好。这是一个小问题,因为你改变了问题。首先是计数器的问题,您已经解决了,现在您提到了另一个关于追加的问题。这应该是一个新问题。你忽略了反问题的答案,而只是在你的问题中纠正它,这是不礼貌的。好吧,我接受你的答案,但是我的英语很差,我无法详细说明我想说什么。你的英语足够好,没有问题。但是您知道您已经更正了会话计数器的代码。请把你的问题像以前一样放回去,把附加的问题作为新问题提出来。我很乐意帮你回答这个问题。当你发布新问题时,请在评论中告诉我,我会看一看。我已经发布了新问题,请检查
    fwrite($fh, "Number of visits during session: " . $_SESSION['hello']);
<?php
session_start();
//$count=1;

//change session variable here!!!
if(isset($_SESSION["hello"]))
    $_SESSION["hello"]++;
else 
    $_SESSION["hello"] = 1;

if(isset($_POST['submit'])){//you don't need ! here
    $txt = "abc.txt"; 
    if (isset($_POST['field1']) && isset($_POST['field2'])) { 
    $_SESSION["favanimal"] = "cat";
    $fh = fopen($txt, 'a'); 

    $txt=$_POST['field1'].' - '.$_POST['field2']; 

    fwrite($fh,$txt); // Write information to the file
    fclose($fh); // Close the file

    }
}

echo $_SESSION["hello"];

?>

<form action="index.php" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>