将表单提交到数据库

时间:2016-12-09 02:27:51

标签: php database submit

我的支持票务网站已经解决了所有问题,除了我的新票据表单没有将值发布到数据库。这就是我所拥有的:

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

1 个答案:

答案 0 :(得分:0)

第一

1  testsql 3456 
2  tese weqe 
3  testdfs sfd 
4  tese weqe

糟糕,您不会关闭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."; ,因此始终会跳过else if

下一个问题

看起来狡猾的代码的一部分是这些行:

INSERT

您的$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})"; ^^^^^^^^^^^ 将类似$dateTime,因此SQL语句将为:

2016-12-09 11:43:42

这是语法错误。请注意,您的INSERT INTO job (...) VALUES(..., 'This is my text', 2016-12-09 11:43:42) 字段周围有引号以将其保留为单个元素,但您的日期不会。使用$text将解决此问题,但无法保证不存在其他问题......