SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help in adding a new record please.

    Hello all. I am new to PHP / MySQL but I am having fun learning.

    I have a simple form with an input (type text), a textarea and an input (type radio with a default value set). Of course I have the famous Submit button.

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p></p><label for "verseref">Type the reference<br />
    <input type="text" name="verseref" size="20" maxlength="20" />
    </label></p>
    <p></p><label for "versetext">Type your verse here<br />
    <textarea name="versetext" rows="10" cols="40"></textarea>
    </label></p>
    <p></p><label for "verseper">
    <input type="radio" name="verseper" value="OT" checked="checked" />Old Testament
    <input type="radio" name="verseper" value="NT" />New Testament
    </label></p>
    <input type="submit" name="submit" value="Submit" />
    </form>

    Now I have a bit of PHP code that is supposed to write the text in each control in a table. It used to work fine but after a few changes it stopped writing the data in a new record. There is the code:

    <?php // Default page display.

    // Connect to the database server.
    $dbcnn = @mysql_connect('localhost','root','mypassword');
    if (!$dbcnn) {
    exit('<p class="alarm">Unable to connect to the database
    server at this time.</p>');
    }

    // Select to the b_verses database.
    if (!@mysql_select_db('b_verses')) {
    exit('<p class="alarm">Unable to locate the <strong>Bible Verses</strong>
    database at this time.</p>');
    }

    // If a verse has been submitted, add it to the database.
    if (isset($_POST['submit'])) {
    if (!empty($_POST['verseref']) and !empty($_POST['versetxt'])) {
    $verseref = trim($_POST['verseref']);
    $versetext = trim($_POST['versetext']);

    $sql = "INSERT INTO tblverses SET
    verse_ref = '$verseref',
    verse_txt = '$versetext',
    verse_per = '$verseper',
    verse_date_added = CURDATE()";
    if (@mysql_query($sql)) {
    echo '<p>You verse has been added to the database.</p>';
    } else {
    echo '<p class="alarm">There was an error submitting the verse: '
    . mysql_error() . '.</p>';
    }
    }
    }

    /* When clicked, this link will load this page with the verse
    submission form displayed. */
    echo '<p><a href="' . $_SERVER['PHP_SELF'] . '">Add another verse?</a>
    <a href="verses.php">See complete list</a></p>';
    ?>

    It seems that the if:

    if (isset($_POST['submit']))

    always return False and thus never executes the line inside. I checked the name 'submit' and all is fine.

    Any ideas anyone? Can you help a bit bigalreturns?

  2. #2
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    California
    Posts
    267
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try adding a hidden field into your form and checking to see if that is set instead of the submit field.
    The Banana Stand - an Arrested Development fansite
    LC-3 Help - tutorials on the LC-3 educational assembly language

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello thirteenlisk

    Thanks for your reply. There was a hidden field before I cleaned the code and post it here. But in case I missed something I did it again. Here's the amended lines:

    ...
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    ...
    <input type="submit" name="submit" value="Submit" />
    <input type="hidden" name="submitted" />
    </form>
    ...

    ...
    // If a verse has been submitted, add it to the database.
    if (isset($_POST['submitted'])) {
    if (!empty($_POST['verseref']) and !empty($_POST['versetxt'])) {
    $verseref = trim($_POST['verseref']);
    $versetext = trim($_POST['versetext']);
    ...

    But unfortunately it still does NOT add the content to a record. I tried to add some echo flags and it does not even go into the first if.

    In the browser the form look perfect!!

    What to do???
    Last edited by dlamarche; Jun 26, 2007 at 14:02. Reason: Added some bold

  4. #4
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there is no value, the field won't be submitted. Just add 'value="true"' to your hidden field.
    PHP questions? RTFM
    MySQL questions? RTFM

  5. #5
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Arrghhhh! I found my silly mistake! Thanks btw kromey for replying. The value="true" used to be there before I remove the line completely. I appreciate your time.

    In my form I have:

    <p></p><label for "versetext">Type your verse here<br />
    <textarea name="versetext" rows="10" cols="40"></textarea>
    </label></p>

    and in the code I mistype the name of my textarea:

    if (!empty($_POST['verseref']) and !empty($_POST['versetxt'])) {

    So versetxt and versetext would not match! Now it is perfect.

    Thank you very much kromey and thirteenlisk for your time. One day it will be me that will be helpful.

    D. Lamarche


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
  •