422,951 Members | 2,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,951 IT Pros & Developers. It's quick & easy.

PHP Insert adding wrong values

P: 96
I have a form like this:

Expand|Select|Wrap|Line Numbers
  1. <form action="CreateNewNews.php" method="post" class="newNews">
  2. //rest of the form
  3. </form>
  4.  
And the php file that is actioned is this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. include ('includes/DbCon.php');
  4.  
  5. $headline = isset($_POST['headline']);
  6. $body = isset($_POST['body']);
  7. $image = isset($_POST['image']);
  8. $current_date = date("j/M/y");
  9.  
  10. if(isset($_POST['submit'])){
  11. $query = $mysqli->prepare("INSERT INTO news (`headline`, `body`, `image`, `date`) VALUES ('$headline', '$body', '$image', '$current_date');");
  12. $sql->execute(array(":headline" => headline, ":body" => body, ":image" => image, ":date" => $current_date));
  13.  
  14. echo '<script type="text/javascript">';
  15. echo 'alert("News Items Saved")';
  16. echo 'document.location.href = "/pc.v.2/admin-news.php";';
  17. echo '</script>';
  18. }
  19.  
  20. else
  21. {
  22. echo '<script type="text/javascript">';
  23. echo 'alert("The news item was not able to be created. Please try again."). mysql_error()';
  24. echo '</script>';
  25. }
  26.  
  27. $mysqli->close();    
  28. ?>
  29.  
  30.  
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?
Jun 10 '15 #1

✓ answered by RonB

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

Share this Question
Share on Google+
2 Replies


Expert Mod 100+
P: 588
You're assigning those vars the return value of a boolean test, not the values of the form submission.
Jun 10 '15 #2

P: 96
Ok. So I changed them to this:

Expand|Select|Wrap|Line Numbers
  1. if (isset($_POST['headline']))$headline = $_POST['headline'];
  2.  
Jun 11 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.