Results 1 to 4 of 4

Thread: SQL Hidden Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    12

    Question SQL Hidden Problem

    Right. This script just adds some information into a database. The information comes from text-boxes. My problem is that, when everything works until I hit the Submit button. The entries in the text-boxes disappear and nothing happens. I wrote a viewing script to check and see if the values were actually entered into the database, but no such luck. Any idea what I'm doing wrong?

    PHP Code:
    <?php

    if ($_POST['submit'])
    {
      
    //connect to mysql
      
    $connect mysql_connect('localhost','username','password');
      
    //verify connection
      
    if (!$connect)
      {
        echo 
    'Unable to connect to MySQL<br>' mysql_error() . '';
      }

      
    //select a database
      
    $select mysql_select_db('database');
      
    //verify db selection
      
    if (!$select)
      {
        echo 
    'Unable to select a database<br>' mysql_error() . '';
      }

      
    //get values
      //to be inserted into database
      
    $title $_POST["title"];
      
    $author $_POST["author"];
      
    $message $_POST["message"];
      
    $tdate $_POST['tdate'];

      
    //add values into database
      //add them in the corresponding fields
      
    $query "INSERT INTO news (id,title,author,message,tdate)" .  "VALUES ('NULL','$title','$author','$message','$tdate')";
      
    $r_query mysql_query($query);
      
    //veryify query
      
    if ($r_query)
      {
        echo 
    'Query has been completed.';
      }
    }
    else
    {
    ?>

    <form method="post" action="sql_add.php">
    <table>
     <tr>
      <td>Title:</td>
      <td><input type="text" name="title"></td>
     </tr>
     <tr>
      <td>Author:</td>
      <td><input type="text" name="author"></td>
     </tr>
     <tr>
      <td>Date:</td>
      <td><input type="text" name="tdate"></td>
     </tr>
     <tr>
      <td>Message:</td>
      <td><input type="text" name="message"></td>
     </tr>
     <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit"></td>
     </tr>
    </table>
    </form>

    <?php
    }
    ?>
    Thanks ahead of time,
    --Brendan

  2. #2
    Junior Member
    Join Date
    Apr 2004
    Location
    Stockholm, Sweden
    Posts
    29

    Re: SQL Hidden Problem

    Is the id-field in the database a varchar type or an integer type? Why are you including it there if you're just going to plonk 'NULL' into it?
    If there is a way to solve your problems, there is no need to worry; if there is no way to solve your problems, there is no point to worry.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: SQL Hidden Problem

    KTotte is right. You can omit the ID from your field list if the database is gernetating it automatically.

    The reason your form isn't working is because you are not checking the submit variable properly. If the user submits the form then the variable $_POST['name'] is created and set to a null string ''. If the form is not submitted then the $_POST['name'] variable is not created at all. In both cases testing for the variable with an IF statement will return false, as a null string is evaluated as a false.

    Instead use the isset() function to test whether or not the variable was actually created:
    PHP Code:
    if (isset($_POST['submit'])) 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    12

    Re: SQL Hidden Problem

    Thanks for the help you guys.

    As for the SQL thingy, I'm just starting to learn SQL (or relearn it rather) and I based that code of off a tutorial I read. The tutorial used NULL for the ID field - but if it's not needed, that's still fine.

    Thanks again!
    --Brendan

Posting Permissions

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



Click Here to Expand Forum to Full Width