Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67527

No error when submitting form, but the SQL database is not updated [duplicate]

$
0
0

I am making a simple helpdesk system for fun, but im running into some issues. I have a form where a user can submit a new case, but when the user presses the "submit" button, the page refreshes but nothing has happened in my SQL database, and i get no error messages. I have been troubleshooting for a long long time, and im just getting more and more confused, so any suggestions are welcome. This project is just something im doing to learn, and alot of code has been "borrowed" from here and there, so its a bit of a mess.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Initialize the session
session_start();


$link = mysqli_connect("localhost", "root", "aTotallyRealPwd", "helpdesk");

$creator = $issue = $comment = $solution = $severity = $user = "";
$creator_err = $issue_err = $comment_err = $solution_err = $severity_err = $user_err = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Validate issue ----- Probably shouldnt use trim, but this is just for testing, yaknow
    if(empty(trim($_POST["issue"]))){
        $issue_err = "Please enter an issue.";
    } else{
            $issue = trim($_POST["issue"]);
            } 

        // Validate comment
    if(empty(trim($_POST["comment"]))){
        $comment_err = "Please enter a comment.";
    } else{
            $comment = trim($_POST["comment"]);
            }


     // Validate solution
    if(empty(trim($_POST["solution"]))){
        $solution_err = "Please enter a solution.";
    } else{
            $solution = trim($_POST["solution"]);
            }

     // Validate severity
    if(empty(trim($_POST["severity"]))){
        $severity_err = "Please enter a number between 1 and 10.";
    } else{
            $severity = trim($_POST["severity"]);
            }

     // Validate user
    if(empty(trim($_POST["user"]))){
        $user_err = "Please enter an affected party.";
    } else{
            $user = trim($_POST["user"]);
            }
    $creator = $_SESSION["id"];

    if(empty($issue_err) && empty($comment_err) && empty($solution_err) && empty($severity_err) && empty($user)){

        $sql = "INSERT INTO cases (creator, issue, comment, solution, severity, user) VALUES (?, ?, ?, ?, ?, ?)";

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "ssssss", $param_creator, $param_issue, $param_comment, $param_solution, $param_severity, $param_user);

            // Set parameters
            $param_creator = $creator;
            $param_issue = $issue;
            $param_comment = $comment;
            $param_solution = $solution;
            $param_severity = $severity;
            $param_user = $user;

            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                //header("location: helpdesk.php");
                echo "EXECUTED STATEMENT";
            } else{
                echo "Something went horribly wrong. Please try again later.";
            }
            echo "Something went kinda wrong. Please try again later.";
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($link);
}

I have 8 columns in my database, but only 6 need to be submitted by the user. The two other ones should be filled out automagically if i have set it up right. SQL Database

Here is the HTML form aswell:

<form id="case-form" autocomplete="off" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        <div class="field <?php echo (!empty($issue_err)) ? 'has-error' : ''; ?>">
                            <input type="textfield" class="case-field" name="issue" id="case-issue" placeholder="What's the issue type? (IE: Outlook)"></input>
                        </div>
                        <div class="field <?php echo (!empty($comment_err)) ? 'has-error' : ''; ?>">
                            <textarea type="textfield" class="case-field" name="comment" required id="case-comment" placeholder="Can you explain the error? (In-depth explanation)"></textarea>
                        </div>
                        <div class="field <?php echo (!empty($solution_err)) ? 'has-error' : ''; ?>">
                            <textarea type="textfield" class="case-field" name="solution" required id="case-solution" placeholder="Is there a solution?"></textarea>
                        </div>
                        <div class="field <?php echo (!empty($severity_err)) ? 'has-error' : ''; ?>">
                            <input type="textfield" class="case-field" name="severity" required id="case-severity" placeholder="How important is this? (1 - 10)"></input>
                        </div>
                        <div class="field <?php echo (!empty($user_err)) ? 'has-error' : ''; ?>">
                            <input type="textfield" class="case-field" name="user" required id="case-user" placeholder="Who is affected? (Ident or dept)"></input>
                        </div>
                        <button type="submit" form="case-form" id="case-button">Submit</button>
                    </form>

I might be missing something simple, or i might just be totally off, as i can't really say im experienced in PHP and mySQL.

Any suggestions are very much welcome, thanks!

EDIT: I have changed "creator id" in the database to "creator", thanks for the suggestions. It didn't fix the issue, but it would've caused an issue in the future, so thanks :)


Viewing all articles
Browse latest Browse all 67527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>