SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist j0n's Avatar
    Join Date
    Apr 2003
    Location
    uk
    Posts
    546
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with submitting data to a mysql database using php forms.

    I have been playing around with php for the past few hours but I have hit a snag. I am trying to use an form to submit information to a mysql database but the code I have used doesn’t seem to be working, it just displays a blank page.

    PHP Code:
    <?php

    if( isset( $_POST['title'], $_POST['content'] ) ){

        
    $newstitle $_POST['title'];
        
    $newscontent $_POST['post'];
        
    $sql "INSERT INTO news SET
                title='
    $title',
                content='
    $content',
                date=CURDATE()"
    ;
        if (@
    mysql_query($sql)) {
            echo(
    '<p>Your news post has been added to the database!</p>');
            } else {


    ?>
    <form method="index.php?act=addnews" method="post">
    <fieldset>

        <legend>New Entry</legend>
        
        <label for="title">
            <input type="text" maxlength="255" name="title" id="title" />
            Title
        </label>
        
        <label for="content">
            <textarea rows="9" cols="60" name="content" id="content"></textarea>
            Article Content
        </label>
        
        <input type="submit" value="Add News" />

    </fieldset>
    </form>
    <?php
        
    }
    ?>
    Any help much appreciated!
    Thanks,
    Jon

  2. #2
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $newstitle $_POST['title']; 
    $newscontent $_POST['post']; 
    $sql "INSERT INTO news SET 
                title='
    $title', 
                content='
    $content', 
                date=CURDATE()"

    in your query your variables need to be $newstitle and $newscontent not what they are now.

  3. #3
    SitePoint Evangelist ikeo's Avatar
    Join Date
    Oct 2004
    Location
    Austin Texas
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your code is riddled with errors.
    First of all your submit button has no name.
    so
    HTML Code:
    <input type="submit" value="Add News" />
    should be
    HTML Code:
    <input type="submit" name="submit" value="Add News" />
    next
    this line
    PHP Code:
     if( isset( $_POST['title'], $_POST['content'] ) ){ 
    looks weird, I've never seen an if statement with a comma in it.
    use this instead
    PHP Code:
     if( isset( $_POST[submit]) ){
    //since your submit button has a name now this will work 
    then this
    PHP Code:
          $newstitle $_POST['title'];
          
    $newscontent $_POST['post'];
          
    $sql "INSERT INTO news SET
                  title='
    $_POST[title]',
                  content='
    $content',
                  date=CURDATE()"

    should be this
    PHP Code:
         $sql "INSERT INTO news 
                  (title, content, date) 
                 values ('
    $_POST[title]','$_POST[$content]', NOW()"


    Quote Originally Posted by j0n
    I have been playing around with php for the past few hours but I have hit a snag. I am trying to use an form to submit information to a mysql database but the code I have used doesn’t seem to be working, it just displays a blank page.

    PHP Code:
      <?php
      
      
    if( isset( $_POST['title'], $_POST['content'] ) ){
      
          
    $newstitle $_POST['title'];
          
    $newscontent $_POST['post'];
          
    $sql "INSERT INTO news SET
                  title='
    $title',
                  content='
    $content',
                  date=CURDATE()"
    ;
          if (@
    mysql_query($sql)) {
              echo(
    '<p>Your news post has been added to the database!</p>');
              } else {
      
      
      
    ?>
      <form method="index.php?act=addnews" method="post">
      <fieldset>
      
          <legend>New Entry</legend>
          
          <label for="title">
              <input type="text" maxlength="255" name="title" id="title" />
              Title
          </label>
          
          <label for="content">
              <textarea rows="9" cols="60" name="content" id="content"></textarea>
              Article Content
          </label>
          
          <input type="submit" value="Add News" />
      
      </fieldset>
      </form>
      <?php
          
    }
      
    ?>
    Any help much appreciated!
    Thanks,
    Jon


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
  •