collecting data from forms

I’m having trouble collecting data from a guestbook form and loading it to a database. Here is the code I’m using in the guestbook form:

<form action="addguestbook.php" method="post" name="guestbook" id="guestbook">
      <table width="75%" border="0" align="center" cellpadding="2" cellspacing="1" summary="Table used for form layout.">
        <tr>
          <th colspan="2" scope="col"><p class="bodytext"><strong>Post your comment</strong></p>
            <p class="bodytext"><a href="viewguestbook.php">View Guestbook </a></p></th>
          </tr>
        <tr>
          <td class="bodytext">Name:</td>
          <td><input name="name" type="text" id="name" size="40" maxlength="100"></td>
        </tr>
        <tr>
          <td class="bodytext">Email:</td>
          <td><input name="email" type="text" id="email" size="40" maxlength="100"></td>
        </tr>
        <tr>
          <td width="30%" class="bodytext">Comment:</td>
          <td width="70%"><textarea name="comment" cols="50" rows="10" id="comment"></textarea></td>
        </tr>
        <tr>
          <td colspan="2" align="center" class="bodytext"><input type="submit" name="Submit" value="Submit">
            <input name="Reset" type="reset" id="Reset" value="Reset"></td>
          </tr>
      </table>
            </form>

Here is the code I’m using to process the data:

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO $tbl_name(name, email, datetime, comment)VALUES('$name', '$email', '$datetime', '$comment')";
$result=mysql_query($sql);

For some reason, the data for name, email, and comment is not getting stored on my database but the datetime field gets stored just fine. Am I missing something here?

Thanks for your help.

ADMIN EDIT: Added [code] tags for readability. Please see http://phphelp.com/guidelines.php

What’s the state of your “register_globals” in the php.ini?

I suspect that it’s OFF (Which is default).

If this is the case then you need to use the $_POST array to get your data.

Try adding the following before your insert.

[php]

<?php $email = !empty($_POST['email']) ? $_POST['email'] : 'error' $name = !empty($_POST['name']) ? $_POST['name'] : 'error' $comment = !empty($_POST['comment']) ? $_POST['comment'] : 'error' if ($comment == 'error' || $name== 'error' || $email=='error') { die("There was a problem with one of the entered fields"); } ?>

[/php]

Thank you so much! That piece of code has corrected my problem!!

Sponsor our Newsletter | Privacy Policy | Terms of Service