Errors in my form, please help

Edit: found the issue. Table name was called “test-form” which I didnt know this untill I saw the error displayed but it doesnt like hyphens in database names.

Thanks SpikeZ, adding the debugging in helped find the error! :slight_smile: Cheers for your help!

You are most welcome.
The thing with the $conn variable, once it has been initialised you dont really need to call it at each time it is being used. So after the connection has been made you can pretty much ignore it.

Ok cool thanks :slight_smile:

Just two quick question. When for form page first loads it displays all of the words from the else part of the following statement. I can remove it leaving the else statment blank, however would be nice to display the error if the database goes down or whatever. How can this be done?

        $result = mysqli_query($conn, $query) or die(mysqli_error($conn) . $query);

        if ($result != FALSE) {
            // Form submitted successfully
            header("Location: thankyou.php");
            exit;
        }
    }
} else {
    echo "Sorry, your comment could not be saved at this time";

    //  DEBUGGING ONLY - DISABLE IN PRODUCTION SITE
    echo "<br/><br /> MySQLi Error: " . mysqli_error($conn);
}

Also what collation do you have set for each row in the database? phpMyAdmin defaults to latin1_Swedish_ci, should it change to latin1_generic_ci or utf8mb4_unicode_ci?

You need to move the comments up into the PREVIOUS else.
What the code says at the minute is:

if the form has been sent
– process it
– insert into the database
– redirect
otherwise
–display the message…

So you would end up with:



      header("Location: thankyou.php"); 
    exit; 
    }  
  } else { 
    echo "Sorry, your comment could not be saved at this time"; 
 
   //  DEBUGGING ONLY - DISABLE IN PRODUCTION SITE 
   echo "<br/><br /> MySQLi Error: " . mysqli_error($conn); 
   } 
  } // <-- closes the if(SERVER METHOD) line

I generally use latin1_Swedish_ci because it is the default setting and has generally worked for me. I’m sure if you ask over in the MySQL forum then you would get a much more comprehensive answer!!

Ah ok, makes sense. Thanks once again for your help!

My big questions would be where’s your FIELDSET, and what the devil makes that a TABLE in the first place? Semantic markup could go a LONG ways towards cleaning that up into something functional instead of all those extra tags for NOTHING. LABEL and INPUT already have semantic meanings, there is NO reason to be wrapping those with other tags that have meanings like tables or lists.

… as could an echo statement and axing all that annoying hard to follow <?php ?> nonsense – but that’s probably just me; I’d like to see <?php and ?> removed from PHP in it’s entirety… also on ‘email’ you’re opening a quote before value that shouldn’t be there.

NOT that you should be writing new code in transitional anyways – this isn’t 1997.

REGARDLESS of what you are doing for styling, it is VERY unlikely you should be using much more than:


} else {

	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
	xmlns="http://www.w3.org/1999/xhtml"
	lang="en"
	xml:lang="en"
><head>

<meta
	http-equiv="Content-Type"
	content="text/html; charset=utf-8"
/>

<meta
	http-equiv="Content-Language"
	content="en"
/>

<link
	type="text/css"
	rel="stylesheet"
	href="screen.css"
	media="screen,projection,tv"
/>

<title>
	Demo Form
</title>

</head><body>

<form action="',$_SERVER['PHP_SELF'],'" method="POST">
	<fieldset>
	
		<div class="',form_row_class('NAME',$errors),'" >
			<label for="NAME">Name</label>
			<input
				type="text"
				name="NAME"
				id="NAME"
				value="',(isset($_POST['NAME']) ? hsc($_POST['NAME']) : ''),'"
			/>
			',error_for('NAME',$errors),'
		</div>
		
		<div class="',form_row_class('TELEPHONE',$errors),'">
			<label for="TELEPHONE">Telephone</label>
			<input
				type="text"
				name="TELEPHONE"
				id="TELEPHONE"
				value="',(isset($_POST['TELEPHONE']) ? hsc($_POST['TELEPHONE']) : ''),'"
			/>
			',error_for('TELEPHONE',$errors),'
		</div>
		
		<div class="',form_row_class('EMAIL',$errors),'">
			<label for="EMAIL">Email Address</label>
			<input
				name="EMAIL"
				id="EMAIL"
				type="text"
				value="',(
					isset($_POST['EMAIL']) ? hsc($_POST['EMAIL']) : ''
				),'"
			/>
			',error_for('EMAIL',$errors),'
		</div>
		
		<div class="',form_row_class('COMMENTS',$errors),'">
			<label for="COMMENTS">Comments</label>
			<textarea
				name="COMMENTS"
				id="COMMENTS"
			>',(
				isset($_POST['COMMENTS']) ? hsc($_POST['COMMENTS']) : ''
			),'</textarea>
			',error_for('COMMENTS',$errors),'
		</div>
		
		<input type="submit" value="Go!" class="submit" />
		
	</fieldset>
</form>

</body></html>';

}

?>

for the markup. Basically drag it kicking and screaming into THIS century.

Thanks. Work fine, only trouble is if the data entered does not validate (according to my form validation) then the error message appears saying can not write to database which I guess it true, but would be better if it happened once all data is valid.

Hi deathshadow60. Yeah I realise there is a fair bit of cleaning up that is needed, but just wanted the basic strucutre to get right first. Thanks for pointing out thought and some very good points which I will fix.