-1

I've got everything worked out for my Support Ticket website, except my newticket form isn't posting the values to the database. Here is what I have:

<?php
    ob_start();
    session_start();
    include 'dbconnect.php';

    if( !isset($_SESSION['user']) ) {
        header("Location: index.php");
        exit;
    $error = false;
    }
    if ( isset($_POST['btn-cancel']) ) {
        header("Location: home.php");
        exit;
    }

    if ( isset($_POST['btn-signup']) ) {

        // get form results
        $text = $_POST['description'];
        $text = strip_tags($text);

        $userid = $_POST['user'];
        $problem = $_POST['problem'];
        $room = $_POST['room'];
        $status = 1;
        $datetime = date('Y-m-d G:i:s');
        // description validation
        if (empty($text)) {
            $error = true;
            $textError = "Please describe the problem.";
        }   else if (strlen($text) > 200) {
            $error = true;
            $textError = "Description must be less than 200 characters in length.";
        }

        // dropdown validation
        if ($problem < 1){
            $error = true;
            $problemError = "Please choose a category and problem.";
        }   else if ($room < 1) {
            $error = true;
            $roomError = "Please choose a building and a room number.";

        // if there's no error, continue to signup
        if( !$error ) {

            $query = "INSERT INTO job (User_UserID,Problem_ProblemID,Status_StatusID,Room_RoomID,Description,Date_Time) VALUES({$userid},{$problem},{$status},{$room},'{$text}',{$dateTime})";
            echo '$query';
            $res = mysqli_query($conn,$query); 

            if ($res) {
                $errTyp = "success";
                $errMSG = "Successfully submited ticket";
                unset($text);
                unset($problem);
                unset($room);
                unset($datetime);
            } else {
                $errTyp = "danger";
                $errMSG = "Something went wrong, try again later."; 
            }   

        }

        }
    }
?>

HTML:

<!DOCTYPE html>
<html>
<head>
<SCRIPT language=JavaScript>
<!--
//function reload(form)
{
//var val=form.type.options[form.type.options.selectedIndex].value;
//self.location='newticket.php?type=' + val ;
}

//function reload2(form)
{
//var val=form.building.options[form.building.options.selectedIndex].value;
//self.location='newticket.php?building=' + val ;
}

function disableselect()
{
<?Php
if(isset($type) and strlen($type) > 0){
echo "document.f1.problem.disabled = false;";}
else{echo "document.f1.problem.disabled = true;";}

if(isset($building) and strlen($type) > 0){
echo "document.f1.room.disabled = false;";}
else{echo "document.f1.room.disabled = true;";}
?>
}
//-->

</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dog Tracks - Login & Registration System</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"  />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body onload=disableselect();>

<div class="container">

    <div id="login-form">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="off">

        <div class="col-md-12">

            <div class="form-group">
                <h2 class="">Create New Ticket</h2>
            </div>

            <div class="form-group">
                <hr />
            </div>

            <?php
            if ( isset($errMSG) ) {

                ?>
                <div class="form-group">
                </div>
                </div>
                <?php
            }
            ?>          
            <div class="form-group">
                <div class="input-group">

                    <?php

                    //Getting the data for first list box
                    $quer2="SELECT problem_typeid,problem_type FROM problem_type ORDER BY problem_type"; 

                    echo "<select name='type' onchange=\"reload(this.form)\"><option value=''>Pick problem category</option>";
                    //while($result2 = mysql_fetch_array($quer2)) { 
                    foreach (mysqli_query($conn,$quer2) as $result2) {
                        if($result2['problem_typeid']==$type){echo "<option selected value='$result2[problem_typeid]'>$result2[problem_type]</option>"."<BR>";}
                        else{echo  "<option value='$result2[problem_typeid]'>$result2[problem_type]</option>";}
                    }
                    echo "</select>";

                    $type=$_GET['type'];                
                    // for second drop down list
                    if(isset($type) and strlen($type) > 0){
                        $quer="SELECT problemid,problem FROM problem WHERE
                            Problem_Type_Problem_TypeID={$type} order by problem";
                    }else {
                        $quer="SELECT problemid,problem FROM problem order by problem";
                    }

                    echo "<select name='problem'><option value=''>What is the problem?</option>";
                    //while($result = mysql_fetch_array($quer)) { 
                    foreach (mysqli_query($conn,$quer) as $result) {
                        echo  "<option value='$result[problemid]'>$result[problem]</option>";
                    } 
                    echo "</select>";
                    //// Add your other form fields as needed here/////

                    ?>

                </div>
                <span class="text-danger"><?php echo $problemError; ?></span>
            </div>
                        <div class="form-group">
                <div class="input-group">

                    <?php
                    @$building=$_GET['building'];
                    //Getting the data for first list box
                    $quer3="SELECT buildingid,building FROM building ORDER BY building"; 

                    // for second drop down list
                    if(isset($building) and strlen($building) > 0){
                        $quer4="SELECT roomid,roomNum FROM room WHERE
                            building_buildingID=$building order by roomNum"; 
                    }else {
                        $quer4="SELECT roomid,roomNum FROM room order by roomNum";
                    } 

                    echo "<select name='building' onchange=\"reload2(this.form)\"><option value=''>In which building?</option>";
                    //while($result2 = mysql_fetch_array($quer3)) { 
                    foreach (mysqli_query($conn,$quer3) as $result3) {
                        if($result3['buildingid']==@$building){echo "<option selected value='$result3[buildingid]'>$result3[building]</option>"."<BR>";}
                        else{echo  "<option value='$result3[buildingid]'>$result3[building]</option>";}
                    }
                    echo "</select>";

                    echo "<select name='room'><option value=''>In what room?</option>";
                    //while($result = mysql_fetch_array($quer)) { 
                    foreach (mysqli_query($conn,$quer4) as $result4) {
                        echo  "<option value='$result4[roomid]'>$result4[roomNum]</option>";
                    } 
                    echo "</select>";
                    //// Add your other form fields as needed here/////

                    ?>

                </div>
                <span class="text-danger"><?php echo $roomError; ?></span>
            </div>

            <div class="form-group">
                <div class="input-group">
                <textarea cols='72' id='description' rows='6' placeholder="Description">
                Please describe the problem here. </textarea>
                </div>
                <span class="text-danger"><?php echo $textError; ?></span>
            </div>

            <div class="form-group">
                <hr />
            </div>

            <div class="form-group">
                <button type="submit" class="btn btn-block btn-primary" name="btn-submit">Submit</button>
                <button type="submit" class="btn btn-block btn-primary" name="btn-cancel">Cancel</button>
            </div>

            <div class="form-group">
                <hr />
            </div>
    </form>
    </div>  

</div>

</body>
</html>
<?php 
mysqli_close($conn);
ob_end_flush();
?>
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • hmm, maybe i should also start posting my support tickets here on SO to make my job easier, lol – Kevin Dec 09 '16 at 02:30
  • Can you please narrow down the problem - what is going wrong; what errors are you seeing, and what line of code is causing the problem. (BTW, I deleted the EMERGENCY stuff as it is a distraction and is probably counter-productive here.) – Ken Y-N Dec 09 '16 at 02:32
  • 1
    narrow down your problem, this question is just a whole blocks of code, check for error messages and turn them ON. funnel down the problem starting getting the values up to the server, a simple `var_dump` can help – Kevin Dec 09 '16 at 02:32
  • I'm not getting an error. The problem is simply that pressing the Submit button does not save the data into the database. – Phantomwang Dec 09 '16 at 02:33
  • [How to turn on error reporting](https://stackoverflow.com/questions/5438060/showing-all-errors-and-warnings). Also, `{$dateTime}` seems to have a space character in it, so your SQL is probably failing. (And watch out for [Little Bobby Tables](http://bobby-tables.com/)). – Ken Y-N Dec 09 '16 at 02:36
  • I turned on error reporting, nothing comes up. Also what do you mean about $dateTime? – Phantomwang Dec 09 '16 at 02:39

1 Answers1

0

First

if ($problem < 1){
    $error = true;
    $problemError = "Please choose a category and problem.";
}   else if ($room < 1) {
    $error = true;
    $roomError = "Please choose a building and a room number.";

Oops, you don't close that else if, so your INSERT is always skipped.

The next problem

One part of the code that looks dodgy are these lines:

$datetime = date('Y-m-d G:i:s');

//...

$query = "INSERT INTO job (User_UserID,Problem_ProblemID,Status_StatusID,Room_RoomID,Description,Date_Time) 
    VALUES({$userid},{$problem},{$status},{$room},'{$text}',{$dateTime})";
                                                            ^^^^^^^^^^^

Your $dateTime will be something like 2016-12-09 11:43:42, so the SQL statement will be:

INSERT INTO job (...) VALUES(..., 'This is my text', 2016-12-09 11:43:42)

This is a syntax error. Note that your $text field has quotes around it to keep it as a single element, but your date does not. Using '{$dateTime}' will fix this problem, although there is no guarantee that there is no other issue...

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • Okay I fixed the 'datetime' but what do I need to do about the else if statement? – Phantomwang Dec 09 '16 at 03:02
  • Rather than spoon-feed you, please use [a PHP formatter](http://beta.phpformatter.com/) and you will see how your code is incorrectly formatted. – Ken Y-N Dec 09 '16 at 03:34
  • I get where you're coming from, but I literally have cancer and my grade depended on this so thanks but no thanks. Also, just ran my code through the PHP formatter and nothing changed – Phantomwang Dec 09 '16 at 03:37
  • Well then, please ask your university for an extension as you seem to have a valid reason for not being able to submit on time, but it will only take you a minute to copy and paste your code into that site then look at the code at `$roomError` onwards and see that you have a misplaced `}`. I can quite understand silly typos being overlooked (I make them myself often enough!), but I've taught you the tools you need to find them. I could have told you exactly what to do with less effort than this long comment, but I'm being obtuse so you can learn one more trick to add to your debugging toolbox. – Ken Y-N Dec 09 '16 at 03:51
  • My professor wouldn't give me an extension. That's the whole reason I'm panicking. I corrected the } placement and the code is still not working. – Phantomwang Dec 09 '16 at 03:57
  • Have you tried [academia.se]? They take questions on what to do in case of illness, [such as this one here](https://academia.stackexchange.com/questions/13694/what-do-i-do-with-my-exam-tomorrow-given-that-i-supplied-medical-certificates) and may have good advice for you. – Ken Y-N Dec 09 '16 at 04:00