我一直在学习php,目前正在尝试制作一个 简单的文章/博客。我希望用户输入“标题”和“正文” 文章,但作者是没有必要的,因此我想 作者的默认值为“匿名”。因此,当用户提交帖子时,它将具有id,title,name,body。如果“名称”是 未指定它的值为“匿名”
我的帖子表 Link
这是我的PHP代码
<?php
$errorClass = 'errorClass';
$msg = '';
$msgClass = 'alert-danger';
if(isset($_POST['submit'])){
// get data
$title = trim(htmlspecialchars($_POST['title']));
$name = trim(htmlspecialchars($_POST['name']));
$post = trim(htmlspecialchars($_POST['post']));
if(!empty($title) AND !empty($post)){
// connect to the database
$host = "localhost";
$username = "root";
$password = "";
$db = "blog";
$conn = new mysqli($host,$username,$password,$db);
if($conn->connect_error){
echo "Error:". $conn->connect_error;
} else {
$sql = "INSERT INTO post(title,name,post) VALUES ('$title','$name','$post')";
if($conn->query($sql)){
$msg = 'Success';
$msgClass = 'alert-success';
} else {
echo " error" .mysqli_error($conn);
}
}
} else {
$msg = 'Please fill in empty areas.';
$msgClass = 'alert-danger';
}
}
?>
还有HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://bootswatch.com/lumen/bootstrap.min.css">
<script>
function sendTo(){
setTimeout(function () {
window.location.href = "http://localhost/blog/";
}, 1000);
}
function validateInput(){
if(document.myForm.title.value === "" && document.myForm.body.value === ""){
document.myForm.title.style.borderColor = 'red';
document.myForm.body.style.borderColor = 'red';
document.myForm.title.focus();
return false;
}
return true;
}
</script>
<title>Document</title>
</head>
<body">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">MyBlog</a>
</div>
<ul class="nav navbar-nav">
<li><a href="index.php">Home</a></li>
<li><a href="new_post.php">Create Post</a></li>
</ul>
</div>
</nav>
<div class="container">
<?php if($msg != ""): ?>
<div class="alert <?php echo $msgClass; ?>"><?php echo $msg; ?></div>
<?php endif; ?>
<div class="jumbotron">
<form class="form-horizontal" method="post" name="myForm" onsubmit="return validateInput()" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<legend>Create new blog post</legend>
<div class="form-group">
<label class="col-lg-2 control-label">Title</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="title" placeholder="Enter a title for your post..." name="title" autocomplete="off" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAASCAYAAABSO15qAAAAAXNSR0IArs4c6QAAAPhJREFUOBHlU70KgzAQPlMhEvoQTg6OPoOjT+JWOnRqkUKHgqWP4OQbOPokTk6OTkVULNSLVc62oJmbIdzd95NcuGjX2/3YVI/Ts+t0WLE2ut5xsQ0O+90F6UxFjAI8qNcEGONia08e6MNONYwCS7EQAizLmtGUDEzTBNd1fxsYhjEBnHPQNG3KKTYV34F8ec/zwHEciOMYyrIE3/ehKAqIoggo9inGXKmFXwbyBkmSQJqmUNe15IRhCG3byphitm1/eUzDM4qR0TTNjEixGdAnSi3keS5vSk2UDKqqgizLqB4YzvassiKhGtZ/jDMtLOnHz7TE+yf8BaDZXA509yeBAAAAAElFTkSuQmCC"); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" placeholder="Enter your name..." name="name" autocomplete="off" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAASCAYAAABSO15qAAAAAXNSR0IArs4c6QAAAPhJREFUOBHlU70KgzAQPlMhEvoQTg6OPoOjT+JWOnRqkUKHgqWP4OQbOPokTk6OTkVULNSLVc62oJmbIdzd95NcuGjX2/3YVI/Ts+t0WLE2ut5xsQ0O+90F6UxFjAI8qNcEGONia08e6MNONYwCS7EQAizLmtGUDEzTBNd1fxsYhjEBnHPQNG3KKTYV34F8ec/zwHEciOMYyrIE3/ehKAqIoggo9inGXKmFXwbyBkmSQJqmUNe15IRhCG3byphitm1/eUzDM4qR0TTNjEixGdAnSi3keS5vSk2UDKqqgizLqB4YzvassiKhGtZ/jDMtLOnHz7TE+yf8BaDZXA509yeBAAAAAElFTkSuQmCC"); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Textarea</label>
<div class="col-lg-10">
<textarea class="form-control" rows="7" name="post" id="body"></textarea>
<span class="help-block">Write your feelings...</span>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default" onclick="sendTo()">Cancel</button>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
您可以检查名称字段是否为空并在将其插入mysql之前为其分配值?
if (empty($name)){
$name = 'Anonymous';
}
答案 1 :(得分:0)
在插入数据之前使用条件
constructor(private videoService: VideoService) {}
希望这会有所帮助:)