Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.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 一个文本文件中的增量值,并将文本写入另一个文本文件_Php_Html_Text_Int_Fwrite - Fatal编程技术网

Php 一个文本文件中的增量值,并将文本写入另一个文本文件

Php 一个文本文件中的增量值,并将文本写入另一个文本文件,php,html,text,int,fwrite,Php,Html,Text,Int,Fwrite,我有一个HTML脚本,其中包含一个表单,这个表单向PHP脚本提交一个名称值。在这个PHP脚本中,我打开了两个不同的文本文件,第一个文件是获取其中的数字,然后将其递增1。另一个文件是打开,然后将新增加的数字与Post中的名称值一起写入。 第一个文件的内部只有一个数字,从“0”开始,这就是我遇到问题的地方。当运行代码时,什么也没有发生,表单被完美地提交,PHP脚本被调用。但两个不同文本文件中的唯一值都是“0”。相反,它应该在“amount.txt”文件中有“1”,在“textContent.txt”

我有一个HTML脚本,其中包含一个表单,这个表单向PHP脚本提交一个名称值。在这个PHP脚本中,我打开了两个不同的文本文件,第一个文件是获取其中的数字,然后将其递增1。另一个文件是打开,然后将新增加的数字与Post中的名称值一起写入。 第一个文件的内部只有一个数字,从“0”开始,这就是我遇到问题的地方。当运行代码时,什么也没有发生,表单被完美地提交,PHP脚本被调用。但两个不同文本文件中的唯一值都是“0”。相反,它应该在“amount.txt”文件中有“1”,在“textContent.txt”文件中有“Text-to-been:1 Other-Text:Name”

我不完全确定我错在哪里,在我看来,这在理论上是正确的

下面是PHP部分,这是无法工作的部分

$nam = $_POST['Name'];

$pastAmount = (int)file_get_contents('/user/site/amount.txt');
$fileOpen1 = '/user/site/amount.txt';
$newAmount = $pastAmount++;
file_put_contents($fileOpen1, $newAmount);

$fileOpen2 = '/user/site/textContent.txt';

$fileWrite2 = fopen($fileOpen2 , 'a');
$ordTxt = 'Text to appear:  ' + $newAmount + 'Other text: ' + $nam;
fwrite($fileWrite2, $ordTxt . PHP_EOL);
fclose($fileWrite2);

首先,代码中的错误:

  • $newAmount=$passamount++=>这将分配
    $pastAmount
    的值,然后增加不是您目标的值
  • $ordTxt='要显示的文本:'+$newAmount+'其他文本:'+$nam=>串联是用
    而不是
    +
    完成的
  • 正确代码:

    $nam = $_POST['Name'];
    
    $pastAmount = (int)file_get_contents('/user/site/amount.txt');
    $fileOpen1 = '/user/site/amount.txt';
    $newAmount = $pastAmount + 1;
    // or
    // $newAmount = ++$pastAmount;
    
    file_put_contents($fileOpen1, $newAmount);
    
    $fileOpen2 = '/user/site/textContent.txt';
    
    $fileWrite2 = fopen($fileOpen2 , 'a');
    $ordTxt = 'Text to appear:  ' . $newAmount . 'Other text: ' . $nam;
    fwrite($fileWrite2, $ordTxt . PHP_EOL);
    fclose($fileWrite2);
    
    而不是:

    $newAmount = $pastAmount++;
    
    你应使用:

    $newAmount = $pastAmount + 1;
    
    因为$pastAmount++将直接更改$pastAmount的值

    而不是

    $ordTxt = 'Text to appear:  ' + $newAmount + 'Other text: ' + $nam;
    
    你应使用:

    $ordTxt = 'Text to appear:  '.$newAmount.' Other text: '.$nam;
    
    因为在PHP中,我们使用的是。用于连接

    PHP代码:

    <?php
    $nam = $_POST['Name'];
    
    
    // Read the value in the file amount
    $filename = "./amount.txt";
    $file = fopen($filename, "r");
    $pastAmount = fread($file, filesize($filename));
    $newAmount = $pastAmount + 1;
    echo "Past amount: ".$pastAmount."-----New amount:".$newAmount;
    fclose($file);
    
    // Write the value in the file amount
    $file = fopen($filename, "w+");
    fwrite($file, $newAmount);
    fclose($file);
    
    
    // Write your second file 
    $fileOpen2 = './textContent.txt';
    $fileWrite2 = fopen($fileOpen2 , 'w+  ');
    $ordTxt = 'Text to appear:  '.$newAmount.' Other text: '.$nam;
    fwrite($fileWrite2, $ordTxt . PHP_EOL);
    fclose($fileWrite2);
    ?>
    
    
    
    不是连接运算符“.”而不是“+”?--$ordTxt='要显示的文本:'+$newAmount+'其他文本:'+$nam@可汗:是的,我刚刚发现这是我的错误。谢谢。非常感谢你的回答,它现在正按照我想要的方式工作。