SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Form problems

  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form problems

    Hi Guys,

    I am trying to insert data via a form into a table in a mysql database called cms_stories.

    What I want it to do is:
    1) check all the fields have been filled in
    2) If that condition is true post the information entered into the database

    Then when those conditions are true
    3) Output text stating that the information has been entered into the database
    4) Show what has been entered in each field

    But am having trouble - Could someone please cast their eye over this and advise if this format of the statements seems correct. I keep getting a print error on:

    Print "You forgot something. Please use your back buton!";


    Thanks

    Chris

    PHP Code:
    <?php

    session_start
    ();

    //Connect to DB 
    include("******************");

    // POST variables 
    $section=$_POST['section'];
    $added_by=$_POST['added_by'];
    $headline=$_POST['headline'];
    $opening=$_POST['opening'];
    $body_text=$_POST['body_text'];

    {

    if ((!
    $section) || (!$added_by) || (!$headline) ||  (!$opening) || (!$body_text)

       Print 
    "You forgot something.  Please use your back buton!";
    }

    } else {

    (isset(
    $_POST['Submit'])){ 

      
    $query "INSERT INTO cms_stories(section,added_by,headline,opening,body_text) VALUES ('$section','$added_by','$headline','$opening','$body_text')";
      
    $msg "A new Story has been added to the database - Please Click <a href=\"http://index\">Here</a> to return to the main menu";
      
      
    $link mysql_connect
      
    mysql_select_db($db); 
      
    $result mysql_query($query) or die('Query failed: ' mysql_error()); 
      
    mysql_close(); 

    // GET variables
    $section=$_GET['section'];
    $added_by=$_GET['added_by'];
    $opening=$_GET['opening'];
    $body_text=$_GET['body_text'];


    ?>
    <html>
    <head>
    <title>Add to DB</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF">
    <form action="add_blank.php" method="post" name="frmAdd">
      <table width="737" border="0" cellspacing="1" cellpadding="1">
      <tr> 
        <td>Section</td>
        <td> 
          <select name="section">
            <option value="news">News</option>
            <option value="sport">Sport</option>
            <option value="business">Business</option>
            <option value="viator">Viator</option>
          </select>
        </td>
      </tr>
      <tr> 
        <td>Added By</td>
        <td> 
          <input name="added_by" type="text" size="35" maxlength="128" value="">
        </td>
      </tr>
      <tr> 
        <td height="35">Headline</td>
        <td height="35"> 
          <input type="text" size="48" maxlength="255" name="headline">
        </td>
      </tr>
      <tr> 
        <td>Opening Paragraph</td>
        <td> 
          <textarea name="opening" wrap="virtual" rows="4" cols="80"></textarea>
        </td>
      </tr>
      <tr> 
        <td>Body Text:</td>
        <td> 
          <textarea name="body_text" wrap="virtual" rows="25" cols="80"></textarea>
        </td>
      </tr>
    </table>
    <input type="Submit" name="Submit" value="Submit">
    <input type="reset" name="Reset" value="Clear Story">
    </form>
    </body>
    </html>

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That can't be your code... must be several syntax errors that should prevent it from running at all... floating curly braces that don't go with any container, missing parentheses, conditions not part of any if block? And why the "GET variables" when the form is submitted by POST?

  3. #3
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    That's the problem I'm trying to get at. My code for adding data to the DB:

    Code:
    <?php
    
    session_start();
    
    //Connect to DB 
    include("../include/check.ini");
    
    // POST variables 
    $section=$_POST['section'];
    $added_by=$_POST['added_by'];
    $headline=$_POST['headline'];
    $opening=$_POST['opening'];
    $body_text=$_POST['body_text'];
    
    if (isset($_POST['Submit'])) {
    
      $query = "INSERT INTO cms_stories(section,added_by,headline,opening,body_text) VALUES ('$section','$added_by','$headline','$opening','$body_text')";
      $msg = "A new Story has been added to the database - Please Click <a href=\"http://index\">Here</a> to return to the main menu";
      
      $link = mysql_connect; 
      mysql_select_db($db); 
      $result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
      mysql_close(); 
    }
    
    
    ?>
    <html>
    <head>
    <title>Add to DB</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    
    <body bgcolor="#FFFFFF">
    <form action="add_blank.php" method="post" name="frmAdd">
      <p><?php echo $msg?></p>
      <table width="737" border="0" cellspacing="1" cellpadding="1">
      <tr> 
        <td>Section</td>
        <td> 
          <select name="section">
            <option value="news">News</option>
            <option value="sport">Sport</option>
            <option value="business">Business</option>
            <option value="viator">Viator</option>
          </select>
        </td>
      </tr>
      <tr> 
        <td>Added By</td>
        <td> 
          <input name="added_by" type="text" size="35" maxlength="128" value="">
        </td>
      </tr>
      <tr> 
        <td height="35">Headline</td>
        <td height="35"> 
          <input type="text" size="48" maxlength="255" name="headline">
        </td>
      </tr>
      <tr> 
        <td>Opening Paragraph</td>
        <td> 
          <textarea name="opening" wrap="virtual" rows="4" cols="80"></textarea>
        </td>
      </tr>
      <tr> 
        <td>Body Text:</td>
        <td> 
          <textarea name="body_text" wrap="virtual" rows="25" cols="80"></textarea>
        </td>
      </tr>
    </table>
    <input type="Submit" name="Submit" value="Submit">
    <input type="reset" name="Reset" value="Clear Story">
    </form>
    </body>
    </html>
    Works fine, but when I try and add a rule in for don't submit the data if the fields are not filled in (as I tried above) I can't get it to work, which is why I'm asking for help.

    So any ideas?

    Thanks

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use empty() to check for zero-length strings (submitted but not filled in form items).


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
  •