SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict Braveheart4's Avatar
    Join Date
    Nov 2005
    Location
    Macedonia
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Simple INSERT Statement - How to make it work only when I click submit?

    I'm learning the basics of PHP/mySQL so I'm little stuck at the INSERT stuff.

    Basically my code works great, it's just that I want data to be inserted into the database only when I click submit but it shows some error "Error: Query was empty"

    here is my PHP code:
    PHP Code:
    if (isset($_POST[submit])){
        
    $insertNews "INSERT INTO news (title, short_text, long_text)
        VALUES ('
    $_POST[title]', '$_POST[short_text]', '$_POST[long_text]')";
    }
    if (!
    mysql_query($insertNews$connect)) {
      die(
    'Error: ' mysql_error());
    }
    echo 
    "1 record added"
    and my HTML code for the <form> tag
    HTML Code:
    <form name="news" method="post" action="<?php $_SERVER['php self'] ?>" enctype="multipart/form-data">
    I'm not sure if my "action" and "enctype" attribute are right, basically I want just to refresh the page itself and not to go at other file after submitting.
    Author at GraphicRiver

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use single quotes in your past vars so $_POST[var] becomes $_POST['var'].

    I only don't use single quotes when the name of the variable is set to the value of another value, for example:
    $variable = "var";
    $var = $_POST[$variable];

    You're also going to want to prepare the data for the database.

    So here is a revised PHP code:
    PHP Code:
    function prepareData($data) {
      
    $data htmlspecialchars($data);
      if (
    get_magic_quotes_gpc())
        
    $data stripslashes($data);
      return 
    mysql_real_escape_string($data);
    }
    if (isset(
    $_POST['submit'])){ 
        
    $title prepareData($_POST['title']);
        
    $short prepareData($_POST['short_text']);
        
    $long prepareData($_POST['long_text']);
        
    $insertNews "INSERT INTO news (title, short_text, long_text) 
        VALUES ('
    $title', '$short', '$long')"

    if (!
    mysql_query($insertNews$connect)) { 
      die(
    'Error: ' mysql_error()); 

    echo 
    "1 record added"
    Also, Your missing a ; in the PHP part of the action attribute.
    Code:
    <form name="news" method="post" action="<?php $_SERVER['php self']; ?>" enctype="multipart/form-data">
    Lastly, make sure your submit button has a name="submit" - for example:
    Code:
    <input type="submit" name="submit" value="Post">
    e39m5

  3. #3
    SitePoint Addict Braveheart4's Avatar
    Join Date
    Nov 2005
    Location
    Macedonia
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I used the code that you provided and it still showed "Error: Query was empty" Then I cut-paste the code after the closing </html> tag and it works.

    But there is still one thing I want to improve. On the left side I have all the news created and on the right side I have the form for creating new news. When I populate the form, hit submit, the page refreshed the news are created in the database, but then I need to click refresh again in the browser in order the new news to appear on the left site where all my created news are listed. Is it because my code for the insert statement is below the </html> ?
    Author at GraphicRiver

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you SELECT from the database, display the news, then INSERT the new entry, naturally the new entry wasn't in the database when you ran the SELECT.

    Anyway, another warning: You should use something other than the submit button to identify the form being submitted. Browsers treat sending the submit button of a form differently. Some older ones never send it as part of the form, newer ones do if you click the button but may not if you press enter while in a form field instead of using the submit button to submit.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •