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 the
catch. This is how I had it written with the
try`:
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!