lundi 30 décembre 2019

I really couldn't describe it in the title, it's better if you read the content inside

I'm having trouble doing PHP right now and I'm on the verge of going insane because I can't seem to show the data I want to show. All I get are errors and the "No tasks" I plan to echo out when there are no tasks available for the user (in my case I have tasks ready for the user in the database). If you guys could point out what's wrong with my code/db, it would be much of great help. Thanks :D

The code for the page function:

<?php

include("dbh.sys.php");
$user = $_SESSION['client_id'];
$sql = "SELECT * FROM incom_tasks WHERE taskUser=?;";
$stmt = $conn->stmt_init();

if(!$stmt = $conn->prepare($sql)) {
    echo '<p>SQL had an error!</p>';
} elseif($stmt = $conn->prepare($sql)) {
    //binder
    $stmt->bind_param("i", $user);
    //run!
    $stmt->execute();
    //get num rows
    $stmt->store_result();
    $num = $stmt->num_rows();
    $result = $stmt->get_result();
    if($num > 0){
        while($row = $result->fetch()){

        $points = $row['taskPoints'];
        $date = $row['taskDate'];
        $taskID = $row['taskID'];
        $sql = "SELECT * FROM tasks_list WHERE taskID=?;";
        $stmt = $conn->stmt_init();
        if(!$stmt = $conn->prepare($sql)) {
            echo '<p>SQL had an error!</p>';
        } elseif($stmt = $conn->prepare($sql)){
            $stmt->bind_param("i", $taskID);
            $stmt->execute();
            $stmt->store_result();

            $result = $stmt->get_result();

            while($row = $result->fetch()) {

                $name = $row['taskTitle'];
                $desc = $row['taskDesc'];

                //POINTS SUFFIX
                if($points == 1) {$pointsSuffix = "point";} else if($points > 1) {$pointsSuffix = "points";}

                echo('<div class="taskBox mdc-elevation--z1"><button class="material-icons taskCheck" title="Options..." onclick="markDone()">check_box_outline_blank</button><h2 class="taskName">'.$name.'</h2><p class="taskPoints">'.$points.' '.$pointsSuffix.'</p><p class="taskDesc">'.$desc.'</p><p class="taskDate">'.$date.'</p></div>');
               }    
            }
        }
    } else {
        echo '<p class="taskBox mdc-elevation--z1">No tasks for now!</p>';
    }
}

The code for the database handler:

<?php

$servername = "localhost";
$dbUsername = "root";
$dbPass = "";
$dbName = "climate_webapp";

$conn = mysqli_connect($servername, $dbUsername, $dbPass, $dbName);

if(!$conn) {
    die("Connection failed! ". mysqli_connect_error());
}

a picture of the database:

https://i.stack.imgur.com/45nhx.png




Aucun commentaire:

Enregistrer un commentaire