I’m not 100% positive that this is a PHP problem and not MySQL but I have gone back to different versions of this form and it was happening before the MySQL was introduced, I just did not notice it. I am making an HTML5 form that submits to a MySQL database (I am using 100webspace for my server and MyPhpAdmin) for a PHP/MySQL programming class.
My issue is the variables are not recording the correct data. I have no clue how or why this is happening. I have double checked my code to make sure that the name, and id from the HTML form matched what is passed into the variables in my validate function and they all match. I have made sure that the variables that are passed into each function are passed in the same order. What is stranger is that not all variables are affected. $firstname, $lastname, $email and $comments are ok. It is my $see (a radio button and gets the value of $look), $when(the date, which gets the $see value), $do (what they did, gets $many), $many (gets $length value) and $look (gets $do) variables. Has anyone else ever had this happen? It is affecting how data is entered into the database. I have attached my code for anyone to review too.
TIA!
[php]<?php
ini_set(‘display_errors’,1); error_reporting(E_STRICT);
include ‘header.html’;
$firstname="";
$lastname="";
$email="";
$when="";
$length="";
$many="";
$look="";
$do="";
$seen="";
$comments="";
$fnameError="";
$lnameError="";
$emailError="";
$seenError="";
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
validateForm();
} else {
showForm($firstname, $lastname, $email, $when, $length, $many, $look, $do, $seen, $comments, $fnameError, $lnameError, $emailError, $seenError);}
//clean and validate data
function validateForm() {
include ‘cleanData.php’;
$firstname = $_POST['fname'];
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
$firstname = null;
$fnameError = '<p>Only letters and white space allowed</p>'; }
$lastname = $_POST['lname'];
if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) {
$lastname = null;
$lnameError = '<p>Only letters and white space allowed</p>'; }
if (!empty($_POST['email'])) {
$email = $_POST['email'];
} else {
$email = NULL;
$emailError = '<p>Sorry, you must provide an email address!</p>';
}
if (empty($_POST['seen'])) {
$seen = NULL;
$seenError = '<p>Sorry you have not told us if you saw Fluffy or not!</p>';
} else $seen = $_POST['seen'];
//get variables from form post
$when = $_POST[‘when’];
$length = $_POST[‘length’];
$look = $_POST[‘look’];
$do = $_POST[‘do’];
$comments = $_POST[‘comments’];
$many = $_POST[‘many’];
if(!($firstname && $lastname && $email && $seen)) {
showform ($firstname,$lastname,$when,$length,$seen,$look,$do,$comments,$email,$many, $fnameError, $lnameError, $emailError, $seenError);
} else addData($firstname, $lastname, $email, $seen, $when, $length, $do, $many, $look, $comments);
}
//Connect to database,print confirmation
function addData($firstname, $lastname, $email, $when, $length, $many, $look, $do, $seen, $comments){
print <<< HERETEXT
The following has been added:
- First name: $firstname
- Last name: $lastname
- Email: $email
- Did you see fluffy: $seen
- Date of Abduction: $when
- How long you were gone: $length
- What did they do: $do
- How many did you see: $many
- What did they look like: $look
- Additional Comments: $comments
if (mysqli_query($db, $query)) {
echo "<h2>Thank you, a new record was created successfully.</h2>";
} else {
echo "Error: " . $query . "<br>" . $db->error;
}
}
// call function to connect to DB and confirm input
function confirm($firstname, $lastname, $email, $when, $length, $many, $look, $do, $seen, $comments) {
addData($firstname, $lastname, $email, $when, $length, $many, $look, $do, $seen, $comments);
}
//Show form include if/else statements to make radio button sticky
function showForm($firstname, $lastname, $email, $when, $length, $many, $look, $do, $seen, $comments, $fnameError, $lnameError, $emailError, $seenError)
{
if ($seen == “yes”) {
$fluffy = “<input class=“radio” type=“radio” name=“seen” value=“yes” checked=“checked”>
<label class=“radio2”>Yes
<input class=“radio” type=“radio” name=“seen” value=“no”>
<label class=“radio2”> No”;
} else if ($seen == “no”) {
$fluffy = “<input class=“radio” type=“radio” name=“seen” value=“yes”>
<label class=“radio2”>Yes
<input class=“radio” type=“radio” name=“seen” value=“no” checked=“checked”>
<label class=“radio2”> No”;
} else {
$fluffy = “<input class=“radio” type=“radio” name=“seen” value=“yes”>
<label class=“radio2”>Yes
<input class=“radio” type=“radio” name=“seen” value=“no”>
<label class=“radio2”> No”; }
print <<< SOMETEXT
Last Name:* $lnameError
What is your Email address?*$emailError
When did it happen?
How long were you gone?
How many did you see?
Describe them:
What did they do you to?
Have you seen my dog fluffy?*$seenError
$fluffy
Anything else you want to Add?
SOMETEXT; }
?>[/php]