简体   繁体   中英

php undefined index error mysql css if statement

I am coding a web application using php html and a MySQL database. I am trying to implement an if, where if that row in the database has a certain value it will change the CSS of how it is displayed. So in my database there are projects and if a project is completed (com = "yes") it should display in green. In dbconnect.php I have the following:

<?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);
};

And here is my actual web page code:

<?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>

The errors keep pointing me to lines where I have the if statements for project complete yes or no (com = yes and com = no). If anyone knows how to fix this or has a easier suggestion on how to change the styling I would really appreciate it!

你需要在像com =='yes'这样的引号中写是或

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM