Php 如何将注释保存到数据库中的名称为;匿名;?
留言簿有以下PHP代码:Php 如何将注释保存到数据库中的名称为;匿名;?,php,mysql,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'], 'c
<?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>
评论页
请添加您的评论
评论:
( )
代码差不多准备好了,但我有一个问题
根据问题说明,如果用户没有指明他的名字,我们需要将他的评论以“匿名”的名义保存到数据库中。如何实施?
提前谢谢。使用if
if (!empty($_POST["author"]))
$author = $_POST["author"]);
else
$author = "Anon";
或三元表达式,其将提供相同的:
$author = ((!empty($_POST["author"])) ? ($_POST["author"]) : ("Anon"));
$author = ((!empty($_POST["author"])) ?? ("Anon"));
如果您使用的是php 7,您可以使用它,它提供了相同的功能:
$author = ((!empty($_POST["author"])) ? ($_POST["author"]) : ("Anon"));
$author = ((!empty($_POST["author"])) ?? ("Anon"));
然后在参数中:$stmt->execute(数组('author'=>$author…
请注意,您不需要在三元表达式的每个元素周围都用括号括起来。这是我的老习惯。只需检查作者是否已在POST数组中设置,如果未设置,则使用三元运算符将其设置为默认值,并更改执行参数数组中的变量即可
<?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>
评论页
请添加您的评论
评论:
( )
测试用户值是否为空,如果为空,则将其替换为匿名。非常简单$author=!empty($\u POST['author'])?$\u POST['author']:“匿名”;
@splash58缺少h@JulesR是的!Thx:)