mrjoeblack;10996238 wrote:
Ok actually this is what I altered the code to. It now works but I'm sure it's not quite right. Can anyone tidy it up for me please ??
As previously suggested by brad, indent your code. There are a few common ways of doing this. Wether you follow one of those or choose your own style doesn't matter. But actually indenting code and always staying consistent is a must.
Looking at an existing way of doing it may also make it easier.
Personally I prefer Allman style for a few reasons. Whenever you have a conditional expression (used with if (...), while (...) etc) that spans several lines, it's still easy to see where the block begins, and you can simply place the cursor over this curly bracer and scroll down to find its match. 1TBS is another common way of doing it.
# higher level of indentation than on the following lines is not good.
# choose the same for php opening tags so that code always can start at the very left edge
/* <?php */
<?php
# Sinc I prefer Allman, I moved the { down one line
# The dual tab indentation is bad though. If you wish to change the amount of space used in
# indentation, change the setting in your editor (and get an editor with this cabability if
# you want it.
# Also, include is not a function, it's a language construct and using parenthesis around
# what you believe is the included file will only fool you, since it does NOT say anything
# about what file is included.
/*
if (isset($_POST['submitted'] )) {
include('connect-mysql.php' ) ;
$area = $_POST['area'] ;
*/
if (isset($_POST['submitted'] ))
{
include 'connect-mysql.php';
$area = $_POST['area'];
# Since SQL code is rather likely to end up in error logs sooner or later, or will need to
# be printed to screen for inspection while developing, I prefer keeping SQL code properly
# formatted. You can pretty much go two ways. One is by always starting them to the very left,
# i.e. with 0 indentation. Keeping closing " on its own line means you won't forget adding
# line breaks between statements where needed or when concatenating parts into a statement.
# Especially useful for longer statements.
$sqlinsert =
"INSERT INTO members
(
area, firstname, lastname, sex, age,
email, postcode, promo, word
)
VALUES
(
'$area' , '$fname' , '$lname' , '$sex', '$age',
'$email', '$pcode', '$promo', '$word'
),
(
'$area' , '$fname' , '$lname' , '$sex', '$age',
'$email', '$pcode', '$promo', '$word'
)
";
# Another way would be to use string contentation to stay within the current indentation
# while formatting SQL code to also be indented
$sqlinsert = "INSERT INTO members " .
" (".
" area, firstname, lastname, sex, age,".
" email, postcode, promo, word".
" )".
" VALUES".
" (".
" '$area' , '$fname' , '$lname' , '$sex', '$age',".
" '$email', '$pcode', '$promo', '$word'".
" )";
# Once again, outdenting { and then not indenting the following code at all...
/*
if(!$email == "" && (!strstr($email,"@") || !strstr($email,".")))
{
echo "<p>Please Go Back and enter a valid e-mail address</p>\n";
$badinput = "<p>we need your valid email in order to keep you informed and send out your members manual</p>\n";
echo $badinput;
die ("Use the BACK button ! ");
}
*/
# and so on...
Just be aware that Allman style will break javascript when used with returning values
// This
return
{
name: value;
}
// is actually interpreted as
return;
{
name: value;
}