View Full Version : Help with form handling and nesting of conditionals

04-23-2012, 09:50 PM
I have a form called submit.php wherein the user is asked to submit an entity name, location (country) of the entity, and a brief description of the entity.

Here is the code for the submit.php page:

require_once ('includes/config.inc.php');
$page_title = 'Dataconia | Submit New Entity';
$page_description = 'Dataconia | Submit New Entity; submission by user of new organization within the Dataconia database';
$page_keywords = 'submit new entity, submission, company, organization, Dataconia, quantifying perception, brands, products, rating, reviews';
$page_author = 'Kylan Hurt';
// Welcome the user (by name if they are logged in):
echo '<h2>New Entity Submission ';
if (isset($_SESSION['first_name'])) {
echo "by {$_SESSION['first_name']}!";}
echo '</h2>';

if (isset($_POST['submitted']))
{// Start of main $_POST conditional +1 = 1
require_once (MYSQL);
//Trim all field of the post / form
$trimmed = array_map('trim', $_POST);
//Assume invalid values
$uid = $en = $c = $ed = FALSE;
$errors = array(); // Initialize an error array.
//Validate User
if (!isset($_SESSION['user_id'])) // if post is submitted and user_id is not set
echo '<p class="error">You must be logged in to submit a new entity</p>';
} else { $uid = $_SESSION['user_id']; //if post is submitted and user_id IS set
if (isset($_POST['entity_name'])) { //if post is submitted, user_id is set, and entity_name is set
$en = mysqli_real_escape_string ($dbc, $trimmed['entity_name']);
} else { // if post is submitted, user_id is set, but entity_name not set
echo '<p class="error">Please enter a name for the entity you are trying to submit.</p>';
} // end of else clause for no entity name
if (isset($_POST['country']))
{ // if post is submitted, user_id is set, and country isset
if ($_POST['country'] = "...") {
echo '<p class="error">You did not enter a valid country.</p>';
else {$c = mysqli_real_escape_string ($dbc, $trimmed['country']);}
else {
echo '<p class="error">Please enter the country in which the entity is based.</p>'; //+1 = 2
} // end of else clause for entity country presence // -1 = 1
if (isset($_POST['description'])) { // start of entity description conditional +1 = 2
$ed = mysqli_real_escape_string ($dbc, $trimmed['description']);
} else {
echo '<p class="error">Please enter a description for the entity being submitted:</p>'; // +1 = 2
}// end of else clause for entity description field -1 = 1
if ($uid && $en && $c && $ed) { // If everything's OK... //+1 = 2
$q = "SELECT entity_id FROM entities WHERE entity_name = '$en'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_num_rows($r) == 0 ) { // then the space is available //+1 = 3, Add the user to the database:
$q = "INSERT INTO entities (entity_name, entity_location, entity_description, date_of_creation, created_by) VALUES ('$en', '$c', '$ed', 'NOW()', '$uid' )";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); // Close of mysqli_num_rows positive-1 = 2
if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. +1 = 3
echo '<p>Thank you for your submission.</p>';
include ('includes/footer.html');
} else {//if mysqli_affected_rows is not 1
echo '<p class="error">Your submission could not be processed at this time due to a system error. We apologize for any inconvenience.</p>';
} else { // the entity name is not available +1-1 = 2
echo '<p class="error">The entity name that you entered has already been submitted. Please try submitting a different entity.</p>';
} else { //
echo '<p class="error">Please fill out the form again.</p>';}


I will leave the HTML out of it for now unless someone wants me to include it but for some reason when I try filling out the form I always get my own coded error messages of "You did not enter a valid country" and "Please fill out the form again." Even when the form seems to be filled out correctly.

Nesting conditionals is something that is new territory to me as I would put myself as a "beginner" when it comes to PHP and MySQL. Can anyone here help me to identify my mistake? Any help would be greatly appreciated!

Edit: I figure it might help to include the rest of the code for the form as well. Here it is:

<div class="form" style="width:90%; margin: 0 auto 0 0;">
<form action="submit.php" method="post" style="padding:0 0 0 20px;">
<p>Please fill out the following form in order to submit a new entry to the Dataconia database:</p>
<tr><td class="form_left" style="text-align:right;">Entity Name:</td><td class="form_right" style="text-alight:left;"><input type="text" name="entity_name" size="30" maxlength="100" value="<?php if (isset($_POST['entity_name'])) echo $_POST['entity_name']; ?>" />
* </td></tr>
<tr><td class="form_left"> Country: </td><td class="form_right"> <select name="country">
<option value="...">...</option>
<option value="United States">United States</option>
<option value="Afganistan">Afghanistan</option>

<option value="Zimbabwe">Zimbabwe</option>
<tr><td class="form_left" style="text-align:right;">Please write a description for the entity:</td><td><textarea rows="15" cols="60" name="description"></textarea></td></tr>
<p><input style="text-align:center;" type="submit" name="submit" value="Submit New Entry" /></p>
<input type="hidden" name="submitted" value="TRUE" />


04-23-2012, 10:03 PM
LOL I just found out my problem. For the conditional checking if $_POST['country'] is equal to the string ".." I forgot to put two equals signs instead of one.

Easy mistake but a bit amateurish =P oh well I'm glad I figured it out.

04-24-2012, 06:52 AM
tip: swap variable and value like if ("..." == $_POST['country']) {. if you should accidentally write = instead of ==, you’ll get a syntax error (wherein the other case it becomes an assignment).

04-24-2012, 01:45 PM
Got it, Dormilich. I remember someone mentioning that tip before and maybe it's about time I started using it.