www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] Inserting info into database not working.

  1. #1
    Join Date
    Jan 2012
    Posts
    14

    resolved [RESOLVED] Inserting info into database not working.

    I received the "undefined index" error on the following code, but it would update the database correctly.

    Code:
    <?php
    $TITLE = $_POST['TITLE'];
    
    mysql_connect ("localhost", "admin", "pass") or die ('Error: ' .mysql_error());
    mysql_select_db ("database");
    $query="INSERT INTO db_table (ID, TITLE)VALUES ('NULL', ' ".$TITLE." ')";
    mysql_query($query) or die ('Error updating database');
    echo "Database updated with: ".$TITLE;
    ?>
    
    <form method="post" action="update.php">
    Title <input type="text" name="TITLE"><br />
    <input type="submit" value="Submit">
    To fix the "undefined index" error I attempted to use isset.

    Code:
    <?php
    if (isset ($_POST['submit']) ) 
    {
    
    $TITLE = $_POST['TITLE'];
    
    mysql_connect ("localhost", "admin", "pass") or die ('Error: ' .mysql_error());
    mysql_select_db ("database");
    $query="INSERT INTO db_table (ID, TITLE)VALUES ('NULL', ' ".$TITLE." ')";
    mysql_query($query) or die ('Error updating database');
    echo "Database updated with: ".$TITLE;
    }
    ?>
    
    <form method="post" action="update.php">
    Title <input type="text" name="TITLE"><br />
    <input type="submit" value="Submit">
    This removed the error, but prevented the database from being updated. What am I missing here?

  2. #2
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    If setting a "check for submit" sort of thing bypassed the error, then the error is in the code to update the database and the "if" isn't likely processing at all... Try this to further check:

    PHP Code:
    <?php
    if (isset ($_POST['submit']) ) 
    {
    echo(
    '<h1 style="color:#F00;">Amanda was wrong.</h1>');

    $TITLE $_POST['TITLE'];

    ... 
    etc etc etc

    }else{

    echo(
    '<h1 style="color:#F00;">Amanda was right.</h1>');
    }
    If your page says "Amanda was right" when you process the form, then you know that your "if" part isn't processing, which means it's not getting the "submit" order otherwise I don't know what to tell you. Also, the page will probably say "Amanda was right" before you process the form too, so ignore it until you actually hit submit.

    Also, just a note: the "title" variable will enter into the database with a space on either side when you submit this form... it's ok to have the apostrophe up against the quotation ( '".$TITLE."' rather than ' ".$TITLE." ' )

  3. #3
    Join Date
    Jan 2012
    Posts
    14
    Thanks for the tip, but no dice. All it drops is an error code now.

    After adding your debug code it now looks like this.

    Code:
    <?php
    if (isset ($_POST['submit']) )
    {
    echo('<h1 style="color:#F00;">Amanda was wrong.</h1>');
    
    $TITLE = $_POST['TITLE'];
    
    mysql_connect ("localhost", "admin", "pass") or die ('Error: ' .mysql_error());
    mysql_select_db ("database");
    $query="INSERT INTO db_table (ID, TITLE)VALUES ('NULL', ' ".$TITLE." ')";
    mysql_query($query) or die ('Error updating database');
    echo "Database updated with: ".$TITLE;
    }
    
    else
    {
    echo('<h1 style="color:#F00;">Amanda was right.</h1>');
    } 
    
    
    <form method="post" action="update.php">
    Title <input type="text" name="TITLE"><br />
    <input type="submit" value="Submit">
    I am now receiving the following error. and nothing else shows up on the screen.
    Code:
    Parse error: syntax error, unexpected T_STRING in update.php on line 6

  4. #4
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    So then the error isn't in the function here, or else it would've displayed one of the two messages. If it doesn't show either one, then it doesn't even get this far; the error is somewhere else in your file. It has to be in something before this stuff even happens...

  5. #5
    Join Date
    Jan 2012
    Posts
    14
    Thanks for the help Amanda. I posted the entire file (as it's a stand alone), so this is going to be interesting.

  6. #6
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265
    Well then unfortunately I don't know what to tell you.

    I would think that it might work better if there was a doctype and regular page framework for the form (that's what mine all do) so I'm not sure where yours is misbehaving.

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    I tried this using my own database and login credentials and it successfully inserted new entries:

    update.php
    PHP Code:
    <?php

        
    if (isset($_POST['TITLE'])) {

            
    $TITLE $_POST['TITLE'];

            
    mysql_connect('localhost''admin''pass') or die ('Error: ' mysql_error());
            
    mysql_select_db('database');

            
    $query 'INSERT INTO db_table (ID, TITLE) VALUES (NULL, "' mysql_real_escape_string($TITLE) . '")';
            
    mysql_query($query) or die ('Error updating database');
            echo 
    'Database updated with: ' $TITLE;
        }

    ?>

    <form method="post" action="update.php">
        Title <input type="text" name="TITLE" /><br />
        <input type="submit" value="Submit" />
    </form>

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    PHP Code:
    <?php
    if (isset ($_POST['submit']) )
    {
       echo(
    '<h1 style="color:#F00;">Amanda was wrong.</h1>');
       
       
    $TITLE $_POST['TITLE'];
       
       
    mysql_connect ("localhost""admin""pass") or die ('Connect error: ' .mysql_error());
       
    mysql_select_db ("database") or die ('Select DB error: ' mysql_error());
       
    // NULL should not be quoted, as I presume you want to use the SQL keyword, not the string literal?
       
    $query="INSERT INTO db_table (ID, TITLE)VALUES (NULL, ' ".$TITLE." ')";
       
    mysql_query($query) or die ("Error updating database:<pre>$query</pre>");
       echo 
    "Database updated with: ".$TITLE;
    }

    else
    {
       echo(
    '<h1 style="color:#F00;">Amanda was right.</h1>');
    }
    ?>

    <form method="post" action="update.php">
    Title <input type="text" name="TITLE"><br />
    <input type="submit" value="Submit">
    Note, once you're done debugging, most of that "or die()" stuff should be converted into tests of return values and logging any debug info instead of exposing it to users, using a more user-friendly error message.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jan 2012
    Posts
    14
    Thank you for the help everyone, It's up and working now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles