如果用户输入的数据是表单中现有数据的副本,如何用PHP用用户输入的数据替换部分文本文件?

时间:2017-07-16 23:02:19

标签: php

我对编程的了解仅限于面向对象的语言,所以请耐心等待。

我已经为用户创建了一个表单,可以对他们最喜欢的教授进行投票。表格的一列应显示教授的姓名,另一列应显示有多少人投票。

如果以前没有输入过教授的名字,那么会创建一个新行,该教授的投票数为1.如果教授的名字有< / em>之前已输入,然后投票计数应递增。

我使用逗号来区分姓名和投票数。

我很可能在这里做错了什么,但这是我迄今为止所做的。它不起作用,因为像我说的那样,我完全不了解PHP。

<?php

    $name = $_POST["name"];

    $file = fopen("vote.txt", "r+");

    $copy = false;

    foreach($file as $row){
        list($nameOrig,$countOrig) = explode(',', $row);
        if($nameOrig == $name){
            $countNew = $countOrig + 1;
            preg_replace($countOrig,$countNew,$string);
            $copy = true;
            break;
        }
    }

    if($copy == false){
        fwrite($file, $name);
        fwrite($file, ',');
        fwrite($file, '1');
    }

    fclose($file);

?>

示例文件:

Doe,1
Smith,3
Clark,1
John,8

警告:

Warning: fopen(vote.txt): failed to open stream: Permission denied in /index.php on line 20 
Warning: Invalid argument supplied for foreach() in /index.php on line 24 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /index.php on line 37 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /index.php on line 38 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /index.php on line 39 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /index.php on line 40 
Warning: fwrite() expects parameter 1 to be resource, boolean given in /index.php on line 41 
Warning: fclose() expects parameter 1 to be resource, boolean given in /index.php on line 44

我知道一个事实,vote.txt位于正确的位置,因为/vote.txt显示了该文件。

1 个答案:

答案 0 :(得分:0)

我创建了一个完成这项工作的php脚本,即使不建议将数据存储在txt文件中,你仍然可以这样做。

我写的所有代码目前都可用,如果你把它全部放在一个php文件中,就在这里:

<form method="post">
    <input type="text" name="name">
    <input type="submit">
</form>
<?php
if(isset($_POST['name'])){ //if post name is set it means vote button was pressed
    $name = $_POST["name"]; //get name in the input field
    $file = fopen("vote.txt", "r+"); //open the file, in this case the txt file must be located in the same folder as this php file
    $fileContent = file_get_contents("vote.txt"); //get current content of txt file
    $teachers = (explode("\n",$fileContent)); //file has a line break (\n) at the end of every vote so create an element in an array called teachers for every line break
    $exists = false; //for future use
    $valueMatch; //if teacher exists save the string of his name and vote count in this variable
    foreach($teachers as $key => $value){ //check if teacher exists
        $parts = explode(",", $value); //create an array of items containing the name and votes of a record
        if($parts[0]==$name){ //$parts[0] contains the name of the current teacher, if matches with the name in the input it means it exists
            $exists = true; //for future use
            $valueMatch = $value; //value that the loop had at the moment of the match
        }
    }
    if($exists){ //if teacher exists
        $parts = explode(",", $valueMatch);
        $curentVotes = (int)$parts[1]; //put current votes in an int variable
        $newStr = $parts[0] . ", " . (++$curentVotes); //new string will contain the same string with a vote added
        $fileContent =  str_replace($valueMatch, $newStr, $fileContent); //the new file content //change the old vote for the new vote
        fwrite($file, $fileContent); //rewrite the file with the new vote
        echo "You voted for teacher " . $name . ". Current teacher votes: " . $curentVotes; //print feedback message
    }else{ //doesn't exist so add to the list
        $txt = $name . ", 1"; //teacher name plus comma and vote number of 1
        $myfile = file_put_contents('vote.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX); //append txt to the file
        echo "The teacher " . $name . " was added to the votes list. Current teacher votes: 1"; //print feedback message
    }
    fclose($file); //close file
}
?>

我测试了代码并且它工作得非常好。

备注

  1. 这可能需要改进,例如验证一些东西
  2. 在某些情况下您可能会遇到一些问题
  3. 我发现,在测试代码时,我打开了txt文件,当添加记录时,它被添加到我打开文件时的插入位置
  4. 该脚本区分大小写,因此在记录中您可以找到Ana和ana作为2个不同的记录,即使它们可以引用同一个老师
  5. <强>更新

    在清理代码时,我搞砸了一部分,我不会工作,我发布了这样但现在已经修复了。

    我希望它对你有所帮助,来自墨西哥的问候!