我使用php html和MySQL数据库编写Web应用程序。我正在尝试实现一个if,如果数据库中的那一行有一定值,它将改变它的显示方式。所以在我的数据库中有项目,如果项目完成(com =" yes"),它应该显示为绿色。在dbconnect.php中,我有以下内容:
<?php
session_start();
$username = "";
$email = "";
$projtitle = "";
$deadline = "";
$del1 = "";
$del2 = "";
$del3 = "";
$errors = array();
//Connect to the database
$db = mysqli_connect('localhost', 'root', '', 'hons');
//If the add project button is clicked
if(isset($_POST['addproj'])) {
$projtitle = mysqli_real_escape_string($db, $_POST['projtitle']);
$del1 = mysqli_real_escape_string($db, $_POST['del1']);
$del2 = mysqli_real_escape_string($db, $_POST['del2']);
$del3 = mysqli_real_escape_string($db, $_POST['del3']);
$deadline = mysqli_real_escape_string($db, $_POST['deadline']);
$colour = mysqli_real_escape_string($db, $_POST['colour']);
//Insert into the database
$sql = "INSERT INTO projects (title, del1, del2, del3, deadline)
VALUES ('$projtitle', '$del1', '$del2', '$del3', '$deadline')";
mysqli_query($db, $sql);
};
//If the project complete button is clicked
if(isset($_POST['projcom'])) {
$com = yes;
//Insert into database
$sql = "INSERT INTO evals (com)
VALUES ('$com')";
mysqli_query($db, $sql);
};
//If the project not complete button is clicked
if(isset($_POST['notcom'])) {
$com = no;
//Insert into database
$sql = "INSERT INTO evals (com)
VALUES ('$com')";
mysqli_query($db, $sql);
};
这是我的实际网页代码:
<?php
session_start();
if(!isset($_SESSION['username'])){
$_SESSION['msg'] = "You must log in first";
header('location: index.php');
}
if(isset($_GET['logout'])){
session_destroy();
unset($_SESSION['username']);
header("location: index.php");
}
?>
<?php include ('dbconnect.php') ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>UPM</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.col-sm-4{
display: inline-block;
padding: 50px 50px;
border-style: solid;
border-width: 3px;
border-radius: 5px;
font-size: 16px;
}
.col-sm-4.completed{
display: inline-block;
padding: 50px 50px;
border-style: solid;
border-width: 3px;
border-radius: 5px;
font-size: 16px;
border-color: green;
font-color: green;
}
.col-sm-4.notcompleted{
display: inline-block;
padding: 50px 50px;
border-style: solid;
border-width: 3px;
border-radius: 5px;
font-size: 16px;
font-color: red;
border-color: red;
}
.jumbotron {
background-color: #f4511e;
color: #ffffff;
padding: 100px 25px;
}
.bg-grey{
background-color: #f6f6f6;
}
.container-fluid{
padding: 60px 50px;
}
.logo {
font-size: 200px;
}
@media screen and (max-width: 768px) {
.col-sm-4 {
text-align: center;
margin: 25px 0;
}
}
.navbar {
margin-bottom: 0;
background-color: #f4511e;
z-index: 9999;
border: 0;
font-size: 12px !important;
line-height: 1.42857143 !important;
letter-spacing: 4px;
border-radius: 0;
}
.navbar li a, .navbar .navbar-brand {
color: #fff !important;
}
.navbar-nav li a:hover, .navbar-nav li.active a {
color: #f4511e !important;
background-color: #fff !important;
}
.navbar-default .navbar-toggle {
border-color: transparent;
color: #fff !important;
}
</style>
</head>
<body id="MyProjects" data-spy="scroll" data-target=".navbar" data-offset="60">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand">
<!-- logo not working-->
<!-- <IMG SRC="images/UPMLogo.jpg" width="230px" height="158px">-->
</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li><a href="home.php">HOME</a></li>
<li><a href="myprojects.php">MY PROJECTS</a></li>
<li><a href="eval.php">EVALUATIONS</a></li>
<li><a href="#pricing">MY STATS</a></li>
<li><a href="hints.php">HINTS & TIPS</a></li>
<li><a href="home.php?logout='1'">LOG OUT</a></li>
</ul>
</div>
</div>
</nav>
<div class="jumbotron text-center">
<h1 id =SiteTitle> Welcome to your MyProjects page! </h1>
<h3>Here you will be able to enter all of your university projects along with their deliverable's and deadlines. You can mark your projects as successful or unsuccessful depending on whether you met the deadline.
It is a great way to visually track all of your projects!</h3>
</div>
<div class=""container-fluid">
<p>
To get add a project click on the plus symbol. If a project is completed on time with all the deliverables' mark it with the tick! Otherwise click on the X.
</p>
</div>
<div class="container-fluid text-center bg-grey">
<div class="row text-center">
<div>
<form method="post" action="myprojects.php">
<div class=""input-group>
Project Title :<br>
<input type="text" name="projtitle"><br>
</div>
<div class=""input-group>
Deliverable 1 :<br>
</div>
<div class=""input-group>
<input type="text" name="del1"><br>
Deliverable 2 :<br>
</div>
<div class=""input-group>
<input type="text" name="del2"><br>
</div>
<div class=""input-group>
Deliverable 3 :<br>
<input type="text" name="del3"><br>
</div>
<div class=""input-group>
Final Deadline Date: <br>
<input type="date" name="deadline"><br>
</div>
<div class=""input-group>
Choose a Colour: <br>
<input type="color" name="colour"><br><br>
</div>
<button type="submit" class="btn" name="addproj">+</button>
<p><strong>Click me, click me!</strong></p>
</form>
</div>
</form>
</div>
<!--Display projects-->
<?php
$sql = "SELECT * FROM projects";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)){
//if com is equal to yes then else the normal echo
if($row['com'] == yes){
echo "<div id='projcompleted' class='col-sm-4.completed'>";
echo "<p>" . $row['title'] . "</p>";
echo "<p>Deliverables: </p>";
echo "<p>" . $row['del1'] . "</p>";
echo "<p>" . $row['del2'] . "</p>";
echo "<p>" . $row['del3'] . "</p>";
echo "<p>Deadline date:</p>";
echo "<p>" . $row['deadline'] . "</p>";
echo "<p>Deadline met and project complete!</p>";
echo "<button type=\"submit\" class=\"btn\" name=\"projcom\">√</button> <button type=\"submit\" class=\"btn\" name=\"notcom\">X</button>";
echo "</div>";
}
elseif ($row['com'] == no){
echo "<div id='projnotcom' class='col-sm-4.notcompleted'>";
echo "<p>" . $row['title'] . "</p>";
echo "<p>Deliverables: </p>";
echo "<p>" . $row['del1'] . "</p>";
echo "<p>" . $row['del2'] . "</p>";
echo "<p>" . $row['del3'] . "</p>";
echo "<p>Deadline date:</p>";
echo "<p>" . $row['deadline'] . "</p>";
echo "<p>Deadline missed/project incomplete! :(</p>";
echo "<button type=\"submit\" class=\"btn\" name=\"projcom\">√</button> <button type=\"submit\" class=\"btn\" name=\"notcom\">X</button>";
echo "</div>";
}
else {
echo "<div id='proj' class='col-sm-4'>";
echo "<p>" . $row['title'] . "</p>";
echo "<p>Deliverables: </p>";
echo "<p>" . $row['del1'] . "</p>";
echo "<p>" . $row['del2'] . "</p>";
echo "<p>" . $row['del3'] . "</p>";
echo "<p>Deadline date:</p>";
echo "<p>" . $row['deadline'] . "</p>";
$target = strtotime($row['deadline']);
$today = time();
$difference = $target - $today;
$days = (int)($difference / 86400);
print $days;
echo "<p>days until deadline</p>";
echo "<button type=\"submit\" class=\"btn\" name=\"projcom\">√</button> <button type=\"submit\" class=\"btn\" name=\"notcom\">X</button>";
echo "</div>";
}
}
?>
</body>
</html>
错误一直指向我有项目完成是或否的if语句的行(com = yes和com = no)。如果有人知道如何解决这个问题,或者对如何更改样式有更简单的建议,我会非常感激!
答案 0 :(得分:0)
您需要在诸如 com =='是'
之类的引号中输入是或否。