如何以“匿名”名称将注释保存到数据库?

时间:2019-01-11 12:26:04

标签: php mysql

留言簿有以下PHP代码:

<?php require_once('config.php');

if(!empty($_POST['comment'])) {
    $stmt = $dbConn->prepare('INSERT INTO comments(`author`, `comment`) VALUES(:author, :comment)');
    $stmt->execute(array('author' => $_POST['author'], 'comment' => $_POST['comment']));
    header("location: /index.php");
}

$stmt = $dbConn->prepare('SELECT author, comment, created_at FROM comments ORDER BY id DESC');
$stmt->execute();
$comments = $stmt->fetchAll();
;?>

<title>Comments Page</title>
<link rel='stylesheet' href='style.css'>

<div id='comments-header'>
    <h1></h1>
</div>
<div id='comments-form'>
    <h3>Please add your comment</h3>
    <form method='post'>
        <div>
            <div>
                <input type="text" name="author" placeholder="Enter your name">
            </div>
            <div>
                <textarea name='comment' placeholder="Enter your comment"></textarea>
            </div>
            <div>
                <br>
                <input type='submit' name='submit' value='Save'>
            </div>
        </div>
    </form>
</div>
<div id='comments-panel'>
    <h3>Comments:</h3>
    <?php foreach ($comments as $comment): ?>
        <p><?=$comment['comment']?>
            <span class='comment-date comment-author'>
                (<?=$comment['author']?> <?=$comment['created_at'];?>)
            </span>
        </p>
    <?php endforeach; ?>
</div>

代码快要准备好了,但是我有一个问题。

根据问题的说明,如果用户未输入姓名,我们需要将其注释以“匿名”的名称保存到数据库中。如何执行呢? 预先谢谢你。

3 个答案:

答案 0 :(得分:3)

使用if。

if (!empty($_POST["author"]))
    $author = $_POST["author"]);
else
    $author = "Anon";

或将提供相同内容的三元表达式:

$author = ((!empty($_POST["author"])) ? ($_POST["author"]) : ("Anon"));

如果您使用的是php 7,则可以使用它,它提供的功能相同:

$author = ((!empty($_POST["author"])) ?? ("Anon"));

然后在您的参数中:$stmt->execute(array('author' => $author ...

请注意,您不需要将括号括在三元表达式的每个元素上。那是我的老习惯。

答案 1 :(得分:3)

只需检查作者是否已在POST数组中设置,以及是否未使用三元运算符将其设置为默认值,然后更改execute参数数组中的变量。

<?php require_once('config.php');

$auth = isset($_POST['author']) ? $_POST['author'] : 'ANON';

if(!empty($_POST['comment'])) {
    $stmt = $dbConn->prepare('INSERT INTO comments(`author`, `comment`) VALUES(:author, :comment)');
    $stmt->execute(array('author' => $auth, 'comment' => $_POST['comment']));
    // changed                       ^^^^^
    header("location: /index.php");
}

$stmt = $dbConn->prepare('SELECT author, comment, created_at FROM comments ORDER BY id DESC');
$stmt->execute();
$comments = $stmt->fetchAll();
;?>

<title>Comments Page</title>
<link rel='stylesheet' href='style.css'>

<div id='comments-header'>
    <h1></h1>
</div>
<div id='comments-form'>
    <h3>Please add your comment</h3>
    <form method='post'>
        <div>
            <div>
                <input type="text" name="author" placeholder="Enter your name">
            </div>
            <div>
                <textarea name='comment' placeholder="Enter your comment"></textarea>
            </div>
            <div>
                <br>
                <input type='submit' name='submit' value='Save'>
            </div>
        </div>
    </form>
</div>
<div id='comments-panel'>
    <h3>Comments:</h3>
    <?php foreach ($comments as $comment): ?>
        <p><?=$comment['comment']?>
            <span class='comment-date comment-author'>
                (<?=$comment['author']?> <?=$comment['created_at'];?>)
            </span>
        </p>
    <?php endforeach; ?>
</div>

答案 2 :(得分:3)

 if(!empty($_POST['comment'])) {
   /* this is used to check whether author variable is empty or not*/
    $author = (!empty($_POST['author'])) ? trim($_POST['author']) : 'Anonymous';
    $stmt = $dbConn->prepare('INSERT INTO comments(`author`, `comment`) VALUES(:author, :comment)');
    $stmt->execute(array('author' => $author , 'comment' => $_POST['comment']));
    header("location: /index.php");
}