PHP Insert adding wrong values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tdrsam
    New Member
    • May 2015
    • 97

    PHP Insert adding wrong values

    I have a form like this:

    Code:
    <form action="CreateNewNews.php" method="post" class="newNews">
    //rest of the form
    </form>
    And the php file that is actioned is this:

    Code:
    <?php
    include ('includes/DbCon.php');
    
    $headline = isset($_POST['headline']);
    $body = isset($_POST['body']);
    $image = isset($_POST['image']);
    $current_date = date("j/M/y");
    	
    if(isset($_POST['submit'])){
    $query = $mysqli->prepare("INSERT INTO news (`headline`, `body`, `image`, `date`) VALUES ('$headline', '$body', '$image', '$current_date');");
    $sql->execute(array(":headline" => headline, ":body" => body, ":image" => image, ":date" => $current_date));
    
    echo '<script type="text/javascript">';
    echo 'alert("News Items Saved")';
    echo 'document.location.href = "/pc.v.2/admin-news.php";';
    echo '</script>';
    }
    
    else
    {
    echo '<script type="text/javascript">';
    echo 'alert("The news item was not able to be created. Please try again."). mysql_error()';
    echo '</script>';
    }
    
    $mysqli->close();	
    ?>
    But all it adds to the DB is a '1' for the headline and the same for the body, the image isn't working, neither is the date. Where have I gone wrong?
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    You're assigning those vars the return value of a boolean test, not the values of the form submission.

    Comment

    • tdrsam
      New Member
      • May 2015
      • 97

      #3
      Ok. So I changed them to this:

      Code:
      if (isset($_POST['headline']))$headline = $_POST['headline'];

      Comment

      Working...