Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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 我能';t商店用户';答案在一个简单的.txt文件中?_Php_Forms_Store - Fatal编程技术网

Php 我能';t商店用户';答案在一个简单的.txt文件中?

Php 我能';t商店用户';答案在一个简单的.txt文件中?,php,forms,store,Php,Forms,Store,我是php新手,所以我尝试制作一个简单的多项选择测验应用程序,每次一个问题,因此当用户单击“提交”按钮时,会转到下一个问题,因此问题位于不同的php文件中,因此我尝试使用fopen、fwrite函数将答案存储在simple answers.txt文件中,但问题是,当我试图自己回答测验问题时。。它没有创建answers.txt文件,所以我手动创建了它,但它仍然是空的,下面是第一个问题的php代码: <?php if (isset($_GET['q1']) && !empty

我是php新手,所以我尝试制作一个简单的多项选择测验应用程序,每次一个问题,因此当用户单击“提交”按钮时,会转到下一个问题,因此问题位于不同的php文件中,因此我尝试使用fopen、fwrite函数将答案存储在simple answers.txt文件中,但问题是,当我试图自己回答测验问题时。。它没有创建answers.txt文件,所以我手动创建了它,但它仍然是空的,下面是第一个问题的php代码:

<?php 
if (isset($_GET['q1']) && !empty($_GET['q1'])) {
    $answer1 = $_GET['q1'];
    $heranswers = fopen("Nanswers.txt ", "a+");
    fwrite($heranswers, $answer1);
}
?>


这有什么不对

在if循环中添加以下代码

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        die(error_get_last());
    }

    // Write $answer1 to our opened file.
    if (fwrite($handle, $answer1) === FALSE) {
        echo "Cannot write to file ($filename)";
        die(error_get_last());
    }

    echo "Success, wrote ($answer1) to file ($filename)";

    fclose($handle);
} else {
    echo "The file $filename is not writable";
}

至少您会发现错误

在if循环中添加以下代码

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        die(error_get_last());
    }

    // Write $answer1 to our opened file.
    if (fwrite($handle, $answer1) === FALSE) {
        echo "Cannot write to file ($filename)";
        die(error_get_last());
    }

    echo "Success, wrote ($answer1) to file ($filename)";

    fclose($handle);
} else {
    echo "The file $filename is not writable";
}

至少您会发现错误

请确保文件权限。。它应该是可写的

<?php

$answer1 = 10;
$filename = "Nanswers.txt";

// Let's make sure the file exists and is writable first.

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a+')) {
        echo "Cannot open file ($filename)";
        die(error_get_last());
    }

    // Write $answer1 to our opened file.
    if (fwrite($handle, $answer1) === FALSE) {
        echo "Cannot write to file ($filename)";
        die(error_get_last());
    }

    echo "Success, wrote ($answer1) to file ($filename)";

    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
?>

确保文件权限。。它应该是可写的

<?php

$answer1 = 10;
$filename = "Nanswers.txt";

// Let's make sure the file exists and is writable first.

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a+')) {
        echo "Cannot open file ($filename)";
        die(error_get_last());
    }

    // Write $answer1 to our opened file.
    if (fwrite($handle, $answer1) === FALSE) {
        echo "Cannot write to file ($filename)";
        die(error_get_last());
    }

    echo "Success, wrote ($answer1) to file ($filename)";

    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
?>

你的文件至少打开了吗不,什么都没有发生,就像代码不存在一样。它可能看起来是中间的,但数据库实际上比操作txt文件要少。你的文件至少打开了吗不,什么都没有发生,这就像代码不存在一样。它可能看起来是中间的,但数据库实际上比处理txt文件要少。还注意到您的文件名“Nanswers.txt”中有一个空格。。它不应该有空格“Nanswers.txt”,还注意到您的文件名“Nanswers.txt”中有空格。。它不应该有空格“Nanswers.txt”