Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.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 - Fatal编程技术网

Php 如何将轮询数据写入文本文件?

Php 如何将轮询数据写入文本文件?,php,Php,我想用php做一个民意测验。我试图通过将信息写入txt文件来收集数据。如何获取将数据写入txt文件的代码 这是我处理程序中的所有代码,我如何使其写入我的txt文件。底部的大部分东西还不重要。试着看一下代码,如果$submit=='submit',接下来是什么 <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Poll</title> &l

我想用php做一个民意测验。我试图通过将信息写入txt文件来收集数据。如何获取将数据写入txt文件的代码

这是我处理程序中的所有代码,我如何使其写入我的txt文件。底部的大部分东西还不重要。试着看一下代码,如果$submit=='submit',接下来是什么

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8"/>
   <title>Poll</title>
 </head>
 <body>
 <?php
    //no need for sport validation is unimportant and doesnt work
    if (isset($_REQUEST['Soda'])) {
        $Soda = $_REQUEST['Soda'];
    } else {
        $Soda = NULL;
        echo '<p class="error">You forgot to select your favorite soda!</p>';
    }
    //This is end of soda validation
    if (!empty($_REQUEST['Book'])) {
        $Book = $_REQUEST['Book'];
    } else {
        $Book = NULL;
        echo '<p class="error">You forgot to write in your favorite book!</p>';
    }
    //End of book validation
    if (isset($_REQUEST['SOTU'])) {
        $SOTU = $_REQUEST['SOTU'];
    } else { 
        $SOTU = NULL;
        echo '<p class="error">You forgot to select the two biggest issues of the                 state of the union address!</p>';
    }
    //End of SOTU validation
    if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) &&                               isset($_REQUEST['SOTU'])) {
        echo' Thank You for filling out the survey!<br> You can see the results of the pole' . "<a href=\"poll_results.php\"> here</a>!<br><br> Your response has been recorded.";
    } else {
        echo '<p class="error">Please go ' . "<a href=\"poll_form.html\">back</a>" . ' and fill out the poll!<p>';
    }
    //End of link responses
    //Define variables and make sure file works
    $submit = $_REQUEST['submit'];
    $filename = 'poll_data.txt';
    $handle = fopen($filename, 'a');
    //next is the stuff that is to be appended
        if ($submit == 'Submit') {
            fopen($filename, 'w');
            $newdata = $Soda . PHP_EOL;
            fwrite($handle, $newdata);
        } else { echo 'You didn\'t click submit';}
    //Now to sort the data and present it
    /*explode('PHP.EOL', $filename);
    $CC = 0;
    $P = 0;
    $MD = 0;
    $SS = 0;
    $BR = 0;
    $DLS = 0;
    $O = 0;
    foreach($filename as $value) {
        if ($value = 'Coca-Cola') {
            $CC = $CC + 1;
        }
        elseif ($value = 'Pepsi') {
            $P = $P + 1;
        }
        elseif ($value = 'MtnDew') {
            $MD = $MD + 1;
        }
        elseif ($value ='Sprite/Sierra-Mist') {
            $SS = $SS + 1;
        }
        elseif ('BigRed') {
            $BR = $BR + 1;
        }
        elseif ('DontLikeSoda') {
            $DLS = $DLS + 1;
        }
        elseif ('Other') {
            $O = $O + 1;
        }
    }*/
?>

我想如果你用

fopen($filename, 'r'); 
这应该行得通

这应该起作用:

<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8"/>
   <title>Poll</title>
 </head>
 <body>
 <?php
    //no need for sport validation is unimportant and doesnt work
    if (isset($_REQUEST['Soda'])) {
        $Soda = $_REQUEST['Soda'];
    } else {
        $Soda = NULL;
        echo '<p class="error">You forgot to select your favorite soda!</p>';
    }
    //This is end of soda validation
    if (!empty($_REQUEST['Book'])) {
        $Book = $_REQUEST['Book'];
    } else {
        $Book = NULL;
        echo '<p class="error">You forgot to write in your favorite book!</p>';
    }
    //End of book validation
    if (isset($_REQUEST['SOTU'])) {
        $SOTU = $_REQUEST['SOTU'];
    } else { 
        $SOTU = NULL;
        echo '<p class="error">You forgot to select the two biggest issues of the                 state of the union address!</p>';
    }
    //End of SOTU validation
    if (isset($_REQUEST['Soda']) && !empty($_REQUEST['Book']) &&                               isset($_REQUEST['SOTU'])) {
        echo' Thank You for filling out the survey!<br> You can see the results of the pole' . "<a href=\"poll_results.php\"> here</a>!<br><br> Your response has been recorded.";
    } else {
        echo '<p class="error">Please go ' . "<a href=\"poll_form.html\">back</a>" . ' and fill out the poll!<p>';
    }
    //End of link responses
    //Define variables and make sure file works
    $submit = $_REQUEST['submit'];
    $filename = 'poll_data.txt';
    //next is the stuff that is to be appended
        if ($submit == 'Submit') {
            $handle = fopen($filename, 'a');
            fputs($handle, $Soda.PHP_EOL);
            fclose($handle);
        } else { echo 'You didn\'t click submit';}
    //Now to sort the data and present it
    /*explode('PHP.EOL', $filename);
    $CC = 0;
    $P = 0;
    $MD = 0;
    $SS = 0;
    $BR = 0;
    $DLS = 0;
    $O = 0;
    foreach($filename as $value) {
        if ($value = 'Coca-Cola') {
            $CC = $CC + 1;
        }
        elseif ($value = 'Pepsi') {
            $P = $P + 1;
        }
        elseif ($value = 'MtnDew') {
            $MD = $MD + 1;
        }
        elseif ($value ='Sprite/Sierra-Mist') {
            $SS = $SS + 1;
        }
        elseif ('BigRed') {
            $BR = $BR + 1;
        }
        elseif ('DontLikeSoda') {
            $DLS = $DLS + 1;
        }
        elseif ('Other') {
            $O = $O + 1;
        }
    }*/
?>

谢天谢地,原来是fopen权限让我绊倒了。我运行的服务器不允许我使用fopen。找到了一条绕过它的路。现在工作。