0

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">&times;</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">&times;</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>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
Chuck Robertson
  • 155
  • 1
  • 13

4 Answers4

0

change your query to:

$sql = mysqli_query($conn, "INSERT INTO `basic` (`title`, `entry`) VALUES ('$addTitle', '$addEntry')") or die(mysqli_error($conn));

The backticks are affecting the query. Note that the back ticks are around each column individualy not around all of them. The way you have it would suggest you have a column called title, entry. You should also remove the $result line because the query has been executed already. Now you also have to change the if statement to if (! $sql).

Update

<?php
if(isset($_POST['addContent'])) {
  $addTitle = $conn->real_escape_string($_POST['title']);
  $addEntry = $conn->real_escape_string($_POST['entry']);
 if(!empty($addTitle) && !empty($addEntry)) // There is a function for this
{
  // Because you are only inserting 2 values there only need to be 2 `?` and 2 parameters
  $sql = $conn->prepare("INSERT INTO `basic` (`title`, `entry`) VALUES (?,?)"); // Added column names just to be sure values are inserted on correct columns
  $sql->bind_param('ss', $addTitle, $addEntry); // Note the 2 "s" not 3
  $sql->execute();   

  if($sql->affected_rows < 1 ) {
    echo $sql->error(); // Don't know for shure what this should be `error` or `error()`
    }
  }
}
?>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
  • You forgot the double mysqli_query that is used, he is using the result of a result for the query – Can O' Spam Nov 27 '15 at 09:07
  • Yes, I just noted that and changed the answer – SuperDJ Nov 27 '15 at 09:08
  • @ChuckRobertson I have updated the answer. Note that you are now mixing `mysqli` OOP and procedural. – SuperDJ Nov 27 '15 at 14:19
  • Tried your code above and for some reason its no longer even rendering the html. – Chuck Robertson Nov 27 '15 at 14:26
  • You were missing the parenthesis on line $addEntry = $conn->real_escape_string$_POST['entry']); I added it in and its rendering now but not inserting still. No error either. – Chuck Robertson Nov 27 '15 at 14:36
  • Have you tried to add: `error_reporting( E_ALL ); ini_set( 'display_errors', '1' );` right after the opening php tag. This should force php to show all errors. – SuperDJ Nov 27 '15 at 14:55
  • I do have ```error_reporting( E_ALL ); ini_set( 'display_errors', '1' );``` added. No error still. Have had other errors unrelated and fixed so error reporting is working. – Chuck Robertson Nov 27 '15 at 15:03
  • How does your database connection look like and where do you add it in the page. Is it procedural stye or OOP? – SuperDJ Nov 27 '15 at 15:05
  • @ChuckRobertson 1 more thing you could try is this: instead of `if(isset($_POST['addContent'])) {` use `if($_POST) { ` This way all the code is only executed if the page is posted no matter what field is or isn't. I don't know if this works for you or if it will intervere with a other form but that's the way I do my forms. – SuperDJ Nov 27 '15 at 15:35
  • Just tried that too...still no dice. Totally baffled. What a crazy mystery. Not having any errors to check is not helping. – Chuck Robertson Nov 27 '15 at 15:39
0

The problem are these lines:

$sql = mysqli_query($conn, "INSERT INTO `basic` (`title`, `entry`) VALUES ('$addTitle', '$addEntry')") or die(mysqli_error($conn));
$result = mysqli_query($sql);

Remove the first mysqli_query and set $sql to equal just the string, this will give you the result and not the result of the result (it'll return boolean false when using the result as a query) and also, backtick each column name singularly, not as a whole :)

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
0

I can't believe none of us caught this sooner. But my submit button was not even inside the form tag! My buddy pointed this out and it fixed the issue. Thanks for trying everyone!

Chuck Robertson
  • 155
  • 1
  • 13
-1

change your query to:

$sql = mysqli_query($conn, "INSERT INTO `basic` (title, entry) VALUES ('$addTitle', '$addEntry')") or die(mysqli_error($conn));

The backticks are affecting the query.

William Madede
  • 727
  • 4
  • 8
  • 2
    Backticks do not affect queries, they help mysql know what's a keyword and what's a table/column name. There has to be something else wrong here – Can O' Spam Nov 27 '15 at 08:58
  • I can, and I agree, but putting backticks around the column names (in a lot of cases) help the query to execute, it is best to have them – Can O' Spam Nov 27 '15 at 09:06
  • In this case they were incorrectly used, and thats what i was refering to when i said they affect his query...done – William Madede Nov 27 '15 at 09:14