php表单如何为添加到我的csv的每个数据添加id

时间:2017-12-09 01:54:18

标签: php jquery html css post

我正在尝试做博客网站,只有管理员可以添加新帖子,为此我想要找到一种方法来添加ID到我的csv上添加的数据。到目前为止,这是我的PHP代码。

<?php
 if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";

if(empty($_POST['first']))
{
    $errorMessage .= "<li>first name</li>";
}
if(empty($_POST['last']))
{
    $errorMessage .= "<li>last name</li>";
}

$varMovie = $_POST['first'];
$varName = $_POST['last'];

if(empty($errorMessage)) 
{
    $fs = fopen("mydata.csv","a");
    fwrite($fs,$varName . ", " . $varMovie . "\n");
    fclose($fs);

    header("Location: thankyou.html");
    exit;
}

}       ?&GT;

1 个答案:

答案 0 :(得分:0)

我相信这就是您的HTML文件的样子     

<head></head>
<body>
    <form action="phpHandler.php" method="post">
        <input type="text" name="first" />
        <input type="text" name="last" />
        <input type="hidden" name="id"  value="1"  />
        <input type="submit" name="Submit" />
    </form>
</body>
</html>

请注意,我添加了一个存储用户ID的隐藏字段

<input type="hidden" name="id"  value="1"  />

PHP phpHandler文件

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errorMessage = "";

    if(empty($_POST['first'])) {
        $errorMessage .= "<li>first name</li>";
    }
    if(empty($_POST['last'])) {
        $errorMessage .= "<li>last name</li>";
    }

    $varMovie = $_POST['first'];
    $varName = $_POST['last'];
    $userId = $_POST['id'];

    if(empty($errorMessage)) {
        $fs = fopen("mydata.csv","a");
        fwrite($fs, $userId .",".$varName . ", " . $varMovie . "\n");
        fclose($fs);

        header("Location: thankyou.html");
        exit;
    }

}

如果您想确保只有管理员可以添加有关Sessions

的内容