Can someone please help me understand, why my code is not inserting data into my database from this form? I can get it to Select and Echo the same info from the same database to the page but when I go to add new data through the form it never makes it to the db.
I'm not getting any errors at all and I have error reporting set at the top of my page. I even set a javascript alert to capture the values of the input when clicking the submit button and they return the true values. They just won't insert into the database.
What am I doing wrong here?
<?php
if(isset($_POST['addContent'])) {
$addTitle = $_POST['title'];
$addEntry = $_POST['entry'];
$sql = mysqli_query($conn, "INSERT INTO `basic` (`title, entry`) VALUES ('$addTitle', '$addEntry')") or die(mysqli_error($conn));
$result = mysqli_query($sql);
if(!$result) {
echo mysql_error($sql);
}
}
?>
<!-- Modal -->
<div class="modal fade" id="addContentModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Data</h4>
</div>
<div class="modal-body">
<form method="POST" action="">
<input type="hidden" name="addContent" id="addContent" value="1">
<div class="form-group">
<label for="title">Title</label>
<input id="addTitle" name="addTitle" type="text" class="form-control">
</div>
<div class="form-group">
<label for="entry">Entry</label>
<textarea id="addEntry" name="addEntry" class="form-control" ></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" id="saveAddBtn" class="btn btn-primary" data-dismiss="modal">Save changes</button>
</div>
</div>
</div>
</div>
UPDATE: Ok so here's my attempt at addressing some of the issues you all have enlightened me to. Still same result...any other ideas?
<?php
if(isset($_POST['addContent'])) {
$addTitle = $_POST['title'];
$addEntry = $_POST['entry'];
if($addTitle != "" && $addEntry != "")
{
$sql = $conn->prepare("INSERT INTO basic VALUES ('',?,?)");
$sql->bind_param('sss', $addTitle, $addEntry);
$result = mysqli_query($sql);
if(!$result) {
echo mysqli_error($sql);
}
}
}
?>
<!-- Modal -->
<div class="modal fade" id="addContentModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add Data</h4>
</div>
<div class="modal-body">
<form method="POST" action="">
<input type="hidden" name="addContent" id="addContent" value="1">
<div class="form-group">
<label for="title">Title</label>
<input id="addTitle" name="title" type="text" class="form-control">
</div>
<div class="form-group">
<label for="entry">Entry</label>
<textarea id="addEntry" name="entry" class="form-control" ></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" id="saveAddBtn" class="btn btn-primary" data-dismiss="modal" value="Save changes">
</div>
</div>
</div>
</div>