Database insertion not working correctly

Not sure which forum to post this on, but as I am a beginner at PHP (& programming in general), I am stumped, do not understand what order to put things, or really how to troubleshoot.

I have a project that I created for a class, and the teacher said that everything looks like it should work right, but it doesn’t. He did not have time to tell me how to make it work right before the class ended.

I have this survey created with PHP & MySQL that is supposed to enter information into the database I created. However, there are multiple problems.

The main problem is that some of the survey questions do not insert anything into the database, while some do. The code looks the same, tho’, as far as I can tell. The site looks great, as I am a web designer, trying to learn programming. I do understand a bunch about PHP includes & files, but not much at all about coding PHP. I have looked on these forums & tutorials, and I’m still stumped. Most of this code was taken from previous projects where code was copied & pasted, & worked right in the earlier projects, especially in the teacher’s example database. I modified code for this project. The database exists & looks OK.

This code successfully inserts what i expect into the database:
[php]$sql = “INSERT INTO user
(FName, LName, occupation, ageRange, gender, username, salt, password, email)
VALUES (’$FName’, ‘$LName’, ‘$occupation’, ‘$ageRange’, ‘$gender’, ‘$username’,’$salt’,’$password’,’$email’)”;

@mysql_query($sql) or die("Query failed in save");[/php]

This code does not:
[php]$sql = “INSERT INTO websiteurl
(websiteID, websiteName, url, howFound, SurveyID)
VALUES (’$websiteID’, ‘$websiteName’, ‘$url’, ‘$howFound’, ‘SurveyID’)”;

mysql_query($sql) or die("Query failed:" .mysql_error());[/php]

‘SurveyID’ is a foreign key. i added it today to see if it would make a difference, but it doesn’t.

I also tried changing the last line to:

[php] @mysql_query($sql) or die(“Query failed in save”);[/php]

to see if it would make a difference. It doesn’t.

I am connecting to the database on both pages.
The first page is for registration, so some of the other code on the page has the user register as a new user & also saves the user name using session cookies. That part works.

As far as I can tell, I am calling all table fields correctly in my HTML forms & in my queries.

Please tell me if you need more of the code to see what’s going on.

What’s the error message?

You’re missing the semi-colon at the end of your INSERT query. It should be
[php]
$sql = “INSERT INTO user
(FName, LName, occupation, ageRange, gender, username, salt, password, email)
VALUES (’$FName’, ‘$LName’, ‘$occupation’, ‘$ageRange’, ‘$gender’, ‘$username’,’$salt’,’$password’,’$email’);”;
[/php]

A few things:

To richei,
There is no error message. Some things are not inserting in the database.

To RaythXC,
I tried adding an extra semi-colon, as you say. It does not change anything. Also, the part you commented on was the part that works fine.

Ok, wel if its just something Like that, then check the spelling and that the information is being passed

Thank you, but I did that like 50 times. I found a few errors in upper/lower case, fixed them, and still no results. I have checked & re-checked.

ok, well. Which variables aren’t being inserted? Post your form and php too, so we can see what’s going on.

Ok, Here’s one of the pages that the insertion is not working. Also, the error handling does not work, even tho’ the code for both is exactly the same as on another page where both work.

[php]<?php
/**

  • survey_p2.php
  • Registered users only, Secure page for User Testing Survey

*/

ob_start();
require_once “includes/secure.php”;
if (!session_id()) session_start();
include “includes/formverifier.php”;
include “includes/formlib.php”;

main(“User Testing Survey, Page 2”);

// Control the operation of the page
function main($title = “”) {
$f = new FormLib(“error”, HORIZONTAL);
if (isset($_REQUEST[“submitTest4”])) {
checkForm($f);
if (!$f->isError()) { // data is OK
processData($f);
redirect(“survey_p3.php”);
}
}
include(“includes/header.php”);
showContent($title, $f);
include(“includes/footer.php”);
}

// Check the input form for errors
function checkForm(&$f) {
// Add code to test form values and record errors

$f->isEmpty(‘navEase’, “Please check yes or no.”);
$f->isEmpty(‘understandEase’, “Please check yes or no.”);
$f->isEmpty(‘findEase’, “Please check yes or no.”);
$f->isEmpty(‘readEase’, “Please check yes or no.”);
$f->isEmpty(‘colorEase’, “Please check yes or no.”);

}

function processData($f) {
// Process the verified data here.
// This is where you save data in the database.

require “includes/dbconvars.php”;
$dbCnx = mysql_connect($dbhost, $dbuser, $dbpwd)
or die(mysql_error());
mysql_select_db($dbname, $dbCnx)
or die(mysql_error());

// Process the first query

$navEase = trim($_REQUEST[“navEase”]);
$understandEase = trim($_REQUEST[“understandEase”]);
$findEase = trim($_REQUEST[“findEase”]);
$readEase = trim($_REQUEST[“readEase”]);
$colorEase = trim($_REQUEST[“colorEase”]);

$sql = "
INSERT INTO survey (navEase, understandEase, findEase, readEase, colorEase)
VALUES (’$navEase’, ‘$understandEase’, ‘$findEase’, ‘$readEase’, ‘$colorEase’);
";

// For debugging (remove when finished)
/*print_r($_REQUEST);
echo “

SQL: $sql

\n”;
die(“Stopped for testing”); // stop the redirect
*/

mysql_query($sql)
or die("Query failed: ".mysql_error());

// Process the next query
$navComments = trim($_REQUEST[“navComments”]);
$understandComments = trim($_REQUEST[“understandComments”]);
$findComments = trim($_REQUEST[“findComments”]);
$readComments = trim($_REQUEST[“readComments”]);
$colorComments = trim($_REQUEST[“colorComments”]);
$genComments = trim($_REQUEST[“genComments”]);

$sql = "
INSERT INTO surveycomments (navComments, understandComments, findComments, readComments, colorComments, genComments)
VALUES (’$navComments’, ‘$understandComments’, ‘$findComments’, ‘$readComments’, ‘$colorComments’, ‘$genComments’);
";

// For debugging (remove when finished)
/*print_r($_REQUEST);
echo “

SQL: $sql

\n”;
die(“Stopped for testing”); // stop the redirect
*/

mysql_query($sql)
or die("Query failed: ".mysql_error());

mysql_close($dbCnx);

}

// Display the content of the page
function showContent($title, $f) {
$username = “”;
if (isset($_SESSION[“username”])) {
$username = " ".$_SESSION[“username”];
}
echo<<<HTML

$title

Hello $username,

Please continue the Survey.

HTML;

?>

Answer a few questions about your experience visiting the Website.

<?php echo $f->formatOnError('navEase', 'Did you find the site easy to navigate?  ') ?> <?php $list = array("Yes"=>"Y", "No"=>"N"); echo $f->makeRadioGroup('navEase', $list); ?> <?php echo $f->showMessageOnError('navEase') ?>
<?php echo $f->formatOnError('navComments', 'If not, please tell us why.  ') ?> <?php echo $f->makeTextInput('navComments', 75) ?> <?php echo $f->showMessageOnError('navComments') ?>

<?php echo $f->formatOnError('understandEase', 'Was the information easy to understand?  ') ?> <?php $list = array("Yes"=>"Y", "No"=>"N"); echo $f->makeRadioGroup('understandEase', $list); ?> <?php echo $f->showMessageOnError('understandEase') ?>
<?php echo $f->formatOnError('understandComments', 'If not, please tell us why.  ') ?> <?php echo $f->makeTextInput('understandComments', 75) ?> <?php echo $f->showMessageOnError('understandComments') ?>

<?php echo $f->formatOnError('findEase', 'Was it easy to find what you were looking for?  ') ?> <?php $list = array("Yes"=>"Y", "No"=>"N"); echo $f->makeRadioGroup('findEase', $list); ?> <?php echo $f->showMessageOnError('findEase') ?>
<?php echo $f->formatOnError('findComments', 'If not, please tell us why.  ') ?> <?php echo $f->makeTextInput('findComments', 75) ?> <?php echo $f->showMessageOnError('findComments') ?>

<?php echo $f->formatOnError('readEase', 'Was it easy to read the information?  ') ?> <?php $list = array("Yes"=>"Y", "No"=>"N"); echo $f->makeRadioGroup('readEase', $list); ?> <?php echo $f->showMessageOnError('readEase') ?>
<?php echo $f->formatOnError('readComments', 'If not, please tell us why.  ') ?> <?php echo $f->makeTextInput('readComments', 75) ?> <?php echo $f->showMessageOnError('readComments') ?>

<?php echo $f->formatOnError('colorEase', 'Did you find the colors pleasing?  ') ?> <?php $list = array("Yes"=>"Y", "No"=>"N"); echo $f->makeRadioGroup('colorEase', $list); ?> <?php echo $f->showMessageOnError('colorEase') ?>
<?php echo $f->formatOnError('colorComments', 'If not, please tell us why.  ') ?> <?php echo $f->makeTextInput('colorComments', 75) ?> <?php echo $f->showMessageOnError('colorComments') ?>

<?php echo $f->formatOnError('genComments', 'Do you have any other comments about the site?  ') ?> <?php echo $f->makeTextArea('genComments', 3, 75) ?> <?php echo $f->showMessageOnError('genComments') ?>

<?php } ?>

[/php]

None of the insertion code inserts anything from this page, but the form looks great on the web, and it looks like it would work.

Go through all the queries and compare the following to the database:

In each field name, is the type of data an integer? If so, make sure the corresponding VALUE in the query does NOT have apostrophe’s around it. These should only be used in strings. I noticed this on this query:
[php]
$sql = “INSERT INTO websiteurl
(websiteID, websiteName, url, howFound, SurveyID)
VALUES (’$websiteID’, ‘$websiteName’, ‘$url’, ‘$howFound’, ‘SurveyID’)”;
[/php]
websiteID and SurveyID are msot likely integers. Also SurveyID you’re missing the $ at the start here.

Also try turning error reporting on and report any errors you get.

Sponsor our Newsletter | Privacy Policy | Terms of Service