Just a couple of quick comments:
In some of the CSS/DOM mailing lists I'm on, people generally
refuse to help unless the HTML and CSS will validate. You
can check these at http://validator.w3.org/check/ and
http://jigsaw.w3.org/css-validator/check/ respectively, and it will
often sort out problems you are experiencing with layout/display if
your code validates properly.
This has led me to think of things that could be done in here, to reduce
the number of "easily fixed" posts. A lot of the problems that people in
this newsgroup tend to experience would have been easier to solve
if they did these things:
- put error_reporting(E_ALL); in all your code. If you have a globally
included/required file, stick it in the start there. Most of your
problems will become easier to identify.
You will probably find that you get problems with
Notice: undefined index in ....
Fix these by doing
if (array_key_exists('myvar', $_POST))
{
// do stuff with $_POST['myvar']
}
etc
- turn register_globals OFF. Use $_POST['variablename'] rather than
$variablename for form data. Similarly use $_GET, $_FILES, $_SERVER, $_SESSION, etc.
Your code will work on more servers, with less work, and you'll have a better handle
on where the data is coming from. Probably better to avoid $HTTP_POST_VARS if you
have a sufficiently current version of PHP; if you don't, better to use
$HTTP_POST_VARS['myvar'] than $myvar
- Check the value of mysql_error() if queries fail:
if (!($result = mysql_query($sql, $connection)))
{
print "Couldn't execute the query\n<pre>$sql</pre>\nMySQL error is \n".mysql_error();
}
- If you need to make a function call that could result in an error, you can always prefix
it with @ :
if (!@move_uploaded_file($somefile, $someotherfile))
{
// clean up here
}
- Check your query result has data in it, before fetching arrays/rows from it:
either do
if (mysql_num_rows($result))
{
// do stuff
}
or
while ($row = mysql_fetch_assoc($result))
{
// do stuff
}
- If necessary, make sure you use addslashes(0 on data before putting it into an SQL query
- Escape entities in HTML code (htmlentities(), urlencode() for URL query strings, etc)
- If your form post/file upload handlers aren't working, try doing
var_dump($_POST) or var_dump($_FILES) to see what input you're getting
Also, if you get a strange or incomprehensible error message, it's often quite
productive to try searching for the "standard" part of it in google, enclosed in quotes,
e.g. http://www.google.com/search?q=%22un...sql_connect%22
Anyone else think of any more?
I'm not trying to be nasty; it's just that following some basic checks will allow
you to find your errors more quickly and easily, and you'll start writing more
robust code as a result!
Matt