Page 1 of 1

storing records

PostPosted: 28. January 2008 17:08
by terry
Hi,
I'm doing a tutorial in Dreamweaver SC3, where the contents of a form are auto stored in a database. On the first trial the info I wrote in the form was passed to the database successfully. But on further runs I get an error message "The connection was reset during loading please try later"
Can anyone help and show me where I'm going wrong.
I'm using an internal browser, firefox, and apache.
Terry
Code: Select all
<?php virtual('/terry/Connections/connAdmin.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO feedback (name, email, comments, visited, subscribe) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['comments'], "text"),
                       GetSQLValueString($_POST['visited'], "text"),
                       GetSQLValueString($_POST['subscribe'], "text"));

  mysql_select_db($database_connAdmin, $connAdmin);
  $Result1 = mysql_query($insertSQL, $connAdmin) or die(mysql_error());
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Contact form</title>
<link href="../examples/styles/contact.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Contact Us</h1>
<p>We welcome feedback from visitors to our site. Please use the following form to let us know what you think about it.</p>
<form action="<?php echo $editFormAction; ?>" method="POST" name="form1" id="form1">
    <fieldset>
    <legend>Your details</legend>
    <p>
        <label for="name">Name: <?php
        if (isset($missing) && in_array('name', $missing)) { ?>
          <span class="warning">Please enter your name</span><?php } ?>
        </label>
        <input name="name" type="text" class="textInput" id="name"
        <?php if (isset($missing)) {
        echo 'value="'.htmlentities($_POST['name']).'"';
        } ?>
        />
    </p>
    <p>
        <label for="email">Email:</label>
      <input name="email" type="text" class="textInput" id="email"
      <?php if (isset($missing)) {
        echo 'value="'.htmlentities($_POST['email']).'"';
        } ?>
      />
    </p>
  </fieldset>
    <fieldset>
    <legend>Your views</legend>
    <p>
        <label for="comments">Comments: <?php
        if (isset($missing) && in_array('comments', $missing)) { ?>
          <span class="warning">Please enter your comments</span><?php } ?>
        </label>
        <textarea name="comments" id="comments" cols="45" rows="5"><?php if (isset($missing)) {
        echo htmlentities($_POST['comments']);
        } ?></textarea>
    </p>
    <p><strong>What aspects of London most interest you?</strong></p>
    <div class="chkRad">
        <p>
            <input type="checkbox" name="interests[]" id="interests-classical" value="Classical concerts"
            <?php
         $OK = isset($_POST['interests']) ? true : false;
         if ($OK && isset($missing) && in_array('Classical concerts', $_POST['interests'])) {
           echo 'checked="checked"';
           } ?>
            />
            <label for="interests-classical">Classical concerts</label>
        </p>
        <p>
            <input name="interests[]" type="checkbox" id="interests-rock" value="Rock/pop"
            <?php
         if ($OK && isset($missing) && in_array('Rock/pop', $_POST['interests'])) {
           echo 'checked="checked"';
           } ?>
            />
            <label for="interests-rock">Rock/pop events</label>
        </p>
        <p>
            <input name="interests[]" type="checkbox" id="interests-drama" value="Drama"
            <?php
         if ($OK && isset($missing) && in_array('Drama', $_POST['interests'])) {
           echo 'checked="checked"';
           } ?>
            />
            <label for="interests-drama">Drama</label>
        </p>
    </div>
  <div class="chkRad">
        <p>
            <input name="interests[]" type="checkbox" id="interests-walks" value="Guided walks"
            <?php
         if ($OK && isset($missing) && in_array('Guided walks', $_POST['interests'])) {
           echo 'checked="checked"';
           } ?>
            />
            <label for="interests-walks">Guided walks</label>
        </p>
        <p>
            <input name="interests[]" type="checkbox" id="interests-art" value="Art"
            <?php
         if ($OK && isset($missing) && in_array('Art', $_POST['interests'])) {
           echo 'checked="checked"';
           } ?>
            />
            <label for="interests-art">Art</label>
        </p>
    </div>
    <p class="clearIt">
        <label for="visited">How often have you been to London? <?php
        if (isset($missing) && in_array('visited', $missing)) { ?>
          <span class="warning">Please select a value</span><?php } ?></label>
        <select name="visited" id="visited">
          <option value="0"
          <?php
        if (!$_POST || $_POST['visited'] == '0') {
            echo 'selected="selected"';
         } ?>
          >-- Select one --</option>
          <option value="Never"
          <?php
        if (isset($missing) && $_POST['visited'] == 'Never') {
          echo 'selected="selected"';
         } ?>
          >Never been</option>
          <option value="1-2 times"
          <?php
        if (isset($missing) && $_POST['visited'] == '1-2 times') {
          echo 'selected="selected"';
         } ?>
          >Once or twice</option>
          <option value="Not yearly"
          <?php
        if (isset($missing) && $_POST['visited'] == 'Not yearly') {
          echo 'selected="selected"';
         } ?>
          >Less than once a year</option>
          <option value="Yearly"
          <?php
        if (isset($missing) && $_POST['visited'] == 'Yearly') {
          echo 'selected="selected"';
         } ?>
          >I go most years</option>
          <option value="Live/work"
          <?php
        if (isset($missing) && $_POST['visited'] == 'Live/work') {
          echo 'selected="selected"';
         } ?>
          >I live/work there</option>
        </select>
    </p>
    <p>
        <label for="views">What image do you have of London?</label>
        <select name="views[]" size="6" multiple="multiple" id="views">
            <option value="Vibrant/exciting"
            <?php
         $OK = isset($_POST['views']) ? true : false;
         if ($OK && isset($missing) && in_array('Vibrant/exciting', $_POST['views'])) {
           echo 'selected="selected"';
           } ?>
            >A vibrant, exciting city</option>
            <option value="Good food"
            <?php
         if ($OK && isset($missing) && in_array('Good food', $_POST['views'])) {
           echo 'selected="selected"';
           } ?>
            >A great place to eat</option>
            <option value="Good transport"
            <?php
         if ($OK && isset($missing) && in_array('Good transport', $_POST['views'])) {
           echo 'selected="selected"';
           } ?>
            >Convenient transport</option>
            <option value="Dull"
            <?php
         if ($OK && isset($missing) && in_array('Dull', $_POST['views'])) {
           echo 'selected="selected"';
           } ?>
            >Dull, dull, dull</option>
            <option value="Bad food"
            <?php
         if ($OK && isset($missing) && in_array('Bad food', $_POST['views'])) {
           echo 'selected="selected"';
           } ?>
            >The food's rotten</option>
            <option value="Transport nightmare"
            <?php
         if ($OK && isset($missing) && in_array('Transport nightmare', $_POST['views'])) {
           echo 'selected="selected"';
           } ?>
            >A transport nightmare</option>
                                        </select>
    </p>
    <p><strong>Would you like to receive regular details of events in London?</strong></p>
    <div class="chkRad">
        <p>
            <input type="radio" name="subscribe" id="subscribe-yes" value="y"
            <?php
          if (isset($missing) && $_POST['subscribe'] == 'y') {
            echo 'checked="checked"';
           } ?>
            />
            <label for="subscribe-yes">Yes</label>
            <input name="subscribe" type="radio" id="subscribe-no" value="n"
            <?php
          if (!isset($missing) || isset($missing) && $_POST['subscribe'] == 'n') {
            echo 'checked="checked"';
           } ?>
             />
            <label for="subscribe-no">No</label>
        </p>
    </div>
    <p>&nbsp;</p>
    <p class="clearIt">
        <input type="submit" name="send" id="send" value="Send comments" />
    </p>
    </fieldset>
    <input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>


PostPosted: 29. January 2008 04:25
by Izzy
I take it you are using the Apache instance included in XAMPP?

Just checking if this file and path
<?php virtual('/terry/Connections/connAdmin.php'); ?>
is in the server's DocumentRoot?
.\xampp\htdocs\terry\Connections\connAdmin.php
and is \terry the root directory of your other test files including the file that contains the pasted code?

I am also assuming that the database credentials are stored in connAdmin.php and include localhost, user, user password, database name and this database is set up in phpMyAdmin for the XAMPP instance of MySQL?

I am not sure if this is the cause of your issue but worth checking into.


http://us2.php.net/function.virtual
php manual wrote:This function may be used on PHP files. However, it is typically better to use include() or require() for PHP files.


Other php gurus reading this thread may spot the cause for you.



Not related to your php issue but needs attention all the same is your HTML Document Type declaration:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

http://validator.w3.org/

Use the Validate by Direct Input tab and copy/paste your HTML code starting from and including the declaration.
This page is not Valid XHTML 1.0 Transitional!
Result: Failed validation, 55 Errors

PostPosted: 29. January 2008 04:53
by KallistaAEnvarou
For validation, if you're using Opera, you can also right-click the page and click "validate". You will validate as if you uploaded a file, so you won't be able to see the direct source code, but you'll have an idea of what's wrong.

storing records

PostPosted: 30. January 2008 12:46
by terry
Hi,
I can confirm that I am using Apache (Xampp) and that all my workfiles are located in .\xampp\htdocs\terry\ and that my database was constructed in PhpMyAdmin. However weird things are going on. I now find that when I try and record the form info in the database I now get a new message
Column 'name' cannot be null
I have not changed the database in any way since my first successful trial.I've also found that sending a simple form (just one name) to a .php page in order to read the form, the .php page opens but the php code does not operate so except for the title, the page is empty. Yet the php test page phpinfo() does work. I seem to be going backwards
Terry

PostPosted: 30. January 2008 13:55
by sari42
Maybe you have an error in your php code. Lately I accidently forgot a "(" behind "if" and got a blank page in spite of error_reporting(E_ALL) and everything. Normally I'd expected a Parse-error.

PostPosted: 30. January 2008 21:03
by KallistaAEnvarou
Column 'name' cannot be null

As far as I can remember, this error means that 'name' is a primary key and therefore requires a datum to be in it. It needs to not be set as one for you to not put a value into it. When you tried to insert the form data directly into the database, did you put in a value for 'name'?

storing records

PostPosted: 11. February 2008 11:59
by terry
I have tried everything, rebuilt the table, rebuilt the form but with the same result. Am now getting desperate. Most of my hair is gone
Terry

PostPosted: 11. February 2008 12:01
by KallistaAEnvarou
Could you give us the table layout?

storing records

PostPosted: 13. February 2008 18:43
by terry
Hi.
Sorry I took so long in replying.her is the table info..

Server: localhost Database: egdwcs3. feedback

Field Type Null Default Comments
message_id int(10) No
name varchar(50) No
email varchar(100) No
comments text No
interests set('Classical concerts', 'Rock/pop', 'Drama', 'Guilded walks', 'Art') Yes NULL
visited varchar(50) No
views set('Vibrant/exciting', 'Good food', 'Good transport', 'Dull', 'Bad food', 'Transport nightmare') Yes NULL
subscribe enum('y', 'n') No
submitted timestamp No CURRENT_TIMESTAMP


It looks pretty messy but it does check out okay in PhpMyAdmin. If there is a cleaner way of sending it let me know.
I have now rebuilt the table and form three times now.
Terry

storing records

PostPosted: 11. March 2008 11:44
by terry
Okay, Ive sorted my problem. I uninstalled Xampp and reinstalled a fresh downloaded version.and all is now working fine. many thanks for your help.
terry