0

I have a simple form I use to submit a new list item.

HTML Form

   <form class="form-horizontal" action="" method="post">
        <fieldset>
            <!-- List Desription-->
            <div class="form-group">
                <label class="col-md-4 control-label" for="listDescription"></label>
                <div class="col-md-4">
                    <div class="input-group">
                        <span class="input-group-addon">Title / Description</span>
                        <input id="titleDescription" name="titleDescription" class="form-control" placeholder="Title / Descriptiopn" type="text" required="">
                    </div>
                </div>
            </div>
            <!-- List Status -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="listStatus"></label>
                <div class="col-md-4">
                    <select id="listStatus" name="listStatus" class="form-control">
                        <option value="1">In Progress</option>
                        <option value="2">On Hold</option>
                        <option value="3">Complete</option>
                    </select>
                </div>
            </div>
            <!-- Submit Button -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="btnAddList"></label>
                <div class="col-md-4">
                    <input type="submit" id="submit" name="submit" class="btn btn-info" value="Create List">
                </div>
            </div>
        </fieldset>
    </form>

The form submits to the following PHP on the same page:

Form submit PHP

if(isset($_POST['submit'])) {
    $listDescription = (isset($_POST['listDescription']) ) ? $_POST['listDescription'] : '';
    $listStatus = (isset($_POST['listStatus']) ) ? $_POST['listStatus'] : '';
    $createdDate = date('Y-m-d');

    $query = $pdo->prepare("INSERT into lists ('createdDate', 'listDescription', 'listStatus') VALUES (:createdDate, :listDescription, :listStatus)");
    $query->bindParam(':listDescription', $listDescription, PDO::PARAM_STR);
    $query->bindParam(':listStatus', $listStatus,PDO::PARAM_STR);
    $query->bindParam(':createdDate', $createdDate, PDO::PARAM_STR);
    $query->execute();
}

The form is not actually getting data into the MySQL database. I had it display the vars for the $_POST:

Dumping array from $_POST

var_dump($_POST)

This is actually displaying my form data:

Array from $_POST output

array(3) { ["titleDescription"]=> string(5) "Title" ["listStatus"]=> string(1) "1" ["submit"]=> string(11) "Create List" }

I cannot for the life of me see why this isnt submitting to the database. I also wanted to wrap it in a try and catch the exception if any but when I do that, I get other errors such as, expected statement right before thecatch. This is how I had it written with thetry`:

PHP Code with 'try' included

try {
        if(isset($_POST['submit'])) {
            $listDescription = (isset($_POST['listDescription']) ) ? $_POST['listDescription'] : '';
            $listStatus = (isset($_POST['listStatus']) ) ? $_POST['listStatus'] : '';
            $createdDate = date('Y-m-d');

            $query = $pdo->prepare("INSERT into lists ('createdDate', 'listDescription', 'listStatus') VALUES (:createdDate, :listDescription, :listStatus)");
            $query->bindParam(':listDescription', $listDescription, PDO::PARAM_STR);
            $query->bindParam(':listStatus', $listStatus,PDO::PARAM_STR);
            $query->bindParam(':createdDate', $createdDate, PDO::PARAM_STR);
            $query->execute();
        } catch (PDOException $e) {
        echo "Error";
    }
}

As I mentioned however I get basically a syntax error when I include the try. I am sure I am missing something stupid with that.

UPDATE

I got it working. After banging my head against the desk and not understanding why it wasnt working, I pretty much started with the most basic form I could create and expanded from there:

try{
    if(isset($_POST['newItem'])) {

        $createdOn = date("Y-m-d H:i:s");
        $listTitle = $_POST['listTitle'];
        $createdBy = $_SESSION['user_id'];
        $listStatus = $_POST['listStatus'];

        $insert = $pdo->prepare("INSERT INTO userlists (createdOn, listTitle, createdBy, listStatus) VALUES(:createdOn, :listTitle, :createdBy, :listStatus)");
        $insert->bindParam(':createdOn', $createdOn);
        $insert->bindParam(':listTitle',$listTitle);
        $insert->bindParam(':createdBy',$_SESSION['user_id']);
        $insert->bindParam(':listStatus',$listStatus);
        $insert->execute();
        echo "insert it";
    }
} catch(PDOException $e) {
    echo "error:".$e->getMessage();
}

So this does the following:

  • Submits a new list item
  • Submits the new list item to the database
  • Submits the current user_id i.e., SESSION identifier to the database

Lower in my page, I have my PHP displaying all list items that belong to the user with the matching user_id.

PROFIT!

Marty Lavender
  • 105
  • 1
  • 12
  • 1
    Your catch block comes after the closing bracket of the if-statement. Take a close look at what brackets belong together. – Qirel Aug 04 '17 at 16:22
  • Also follow this https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql. Not sure if it is a duplicate, but very close – u_mulder Aug 04 '17 at 16:26
  • It seems that your param values are different, 1 is titleDescription and the other one is listDescription: name="titleDescription" and $_POST['listDescription'] – Cedric Guindon Aug 04 '17 at 16:39
  • Ok I did have titleDescription left in the form itself. That has been fixed. My form still will not submit however. I have pasted the entire source of the page here if this helps: https://pastebin.com/9q0Q9v8V – Marty Lavender Aug 04 '17 at 16:44
  • Your input still says titleDescription: – Cedric Guindon Aug 04 '17 at 16:52
  • Sorry my paste had the typo. It is in fact fixed in my actual running code.https://pastebin.com/8Q55wdqb – Marty Lavender Aug 04 '17 at 16:56

1 Answers1

1

I got it working. After banging my head against the desk and not understanding why it wasnt working, I pretty much started with the most basic form I could create and expanded from there:

try{
    if(isset($_POST['newItem'])) {

        $createdOn = date("Y-m-d H:i:s");
        $listTitle = $_POST['listTitle'];
        $createdBy = $_SESSION['user_id'];
        $listStatus = $_POST['listStatus'];

        $insert = $pdo->prepare("INSERT INTO userlists (createdOn, listTitle, createdBy, listStatus) VALUES(:createdOn, :listTitle, :createdBy, :listStatus)");
        $insert->bindParam(':createdOn', $createdOn);
        $insert->bindParam(':listTitle',$listTitle);
        $insert->bindParam(':createdBy',$_SESSION['user_id']);
        $insert->bindParam(':listStatus',$listStatus);
        $insert->execute();
        echo "insert it";
    }
} catch(PDOException $e) {
    echo "error:".$e->getMessage();
}

So this does the following:

Submits a new list item Submits the new list item to the database Submits the current user_id i.e., SESSION identifier to the database Lower in my page, I have my PHP displaying all list items that belong to the user with the matching user_id.

PROFIT!

Marty Lavender
  • 105
  • 1
  • 12