PHP-我无法保存输入内容

时间:2018-11-27 09:10:27

标签: php html web networking

我正在网站上解决此问题。我应该创建一个表格来填写,以显示新的信息/新闻。我有这样的代码:

    <?php
include "0begin.php";

$title=$_POST["title"];
isset($title) or $title=$_GET["title"];
$msg=$_POST["msg"];
isset($msg) or $msg=$_GET["msg"];

?>
<h1>News</h1>

<form method="POST">
Title<br><input type=text input name="title" value=<?=$title?> ><br>
News<br>
<textarea input name="msg" cols=40 rows=5> </textarea><br>
<input type="submit">
<br><br>
</form>
<?php
$dateposted=date("YmdHis");
if (!empty($title) and !empty($msg)) {
    $fp=fopen("/home/aqueen/public_html/news/".$dateposted."txt", "w");
    fwrite($fp,$title,$msg);
    fwirte($fp,$msg);
    fclose($fp);    
    include "/home/aqueen/public_html/news/".$dateposted."txt"; }

?> 

我的问题: 1)如何修复该代码?它生成新文件,但内部没有内容。 2)它未在网站上显示新文件/可能未正确包含该文件/ 3)当它开始工作时,如何允许某人从网站上删除新闻而无需打开代码? / online /

预先感谢您:)

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

<?php
//include "0begin.php";
$title = $_POST["title"];
$msg = $_POST["msg"];
?>
<h1>News</h1>
<form method="POST">
    Title<br>
    <input type="text" name="title"><br>
    News<br>
    <textarea name="msg" cols="40" rows="5"></textarea><br>
    <input type="submit">
    <br><br>
</form>

<?php
$dateposted=date("YmdHis");

if (!empty($title) and !empty($msg)) {
    if (!file_exists('news/')) {
        mkdir("news/", 0755);
    }

    if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/news/")){
        $pathGenerated = $_SERVER['DOCUMENT_ROOT'] . "/news/";
    }
    if(file_exists( "news/")){
        $pathGenerated = "news/";
    }
    $pathGenerated = str_replace('//', '', $pathGenerated);

    $fp=fopen($pathGenerated."".$dateposted.".txt", "w");
    $textInsert = "Titolo: ".$title." \nMessaggio: ".$msg;
    fwrite($fp,$textInsert);
    fclose($fp);
    include $pathGenerated."".$dateposted.".txt";
}
?>

自动创建文件夹/news/(如果不存在)。