Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>/** * Rule #0: never trust users input! */ //sanitize integer value $id = intval($_GET['id']); //sanitize string value; $name = mysql_real_escape_string($_POST['name']); //1. using `dbname`. is better than using mysql_select_db() //2. names of tables and columns should be quoted by "`" symbol //3. each variable should be sanitized (even in LIMIT clause) $q = mysql_query("UPDATE `dbname`.`tablename` SET `name`='".$name."' WHERE `id`='".$id."' LIMIT 0,1 "); if ($q===false) { trigger_error('Error in query: '.mysql_error(), E_USER_WARNING); } else { //be careful! $name contains user's data, remember Rule #0 //always use htmlspecialchars() to sanitize user's data in output print htmlspecialchars($name).' updated'; } ######################################################################## //Example, how easily is to use set_error_handler() and trigger_error() //to control error reporting in production and dev-code //Do NOT use error_reporting(0) or error_reporting(~E_ALL) - each error //should be fixed, not muted function err_handler($errno, $errstr, $errfile, $errline) { $hanle_errors_print = E_ALL &amp; ~E_NOTICE; //if we want to print this type of errors (other types we can just write in log-file) if ($errno &amp; $hanle_errors_print) { //$errstr can contain user's data, so... Rule #0 print PHP_EOL.'Error ['.$errno.'] in file '.$errfile.' in line '.$errline .': '.htmlspecialchars($errstr).PHP_EOL; } //here you can write error into log-file } set_error_handler('err_handler', E_ALL &amp; ~E_NOTICE &amp; E_USER_NOTICE &amp; ~E_STRICT &amp; ~E_DEPRECATED); </code></pre> <p><em><strong>And some explanation of comments:</em></strong> </p> <pre><code>//1. using `dbname`. is better than using mysql_select_db() </code></pre> <p>With using mysql_select_db you can create errors, and it will be not so easy to find and fix them.<br> For example, in some script you will set db1 as database, but in some function you need to set db2 as database.<br> After calling this function, database will be switched, and all following queries in script will be broken or will broke some data in wrong database (if names of tables and columns will coincide).</p> <pre><code>//2. names of tables and columns should be quoted by "`" symbol </code></pre> <p>Some names of columns can be also SQL-keywords, and using "<strong>`</strong>" symbol will help with that.<br> Also, all string-values, inserted to query, should be quoted by <strong>'</strong> symbol. </p> <p><code>//always use htmlspecialchars() to sanitize user's data in output</code><br> It will help you to prevent <a href="http://en.wikipedia.org/wiki/Cross-site_scripting" rel="nofollow">XSS-attacks</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload