Essential+SQL+Query+Markup

=Note=

The table name is **topics**


 * id** is an int, the primary key and set to auto increment
 * name** is a string - varchar(25)
 * visible** is a tiny int - value 0 or 1 - default value of 0

=Create=

$query = "INSERT INTO topics (name, visible) VALUES ('{$name}',{$visible})";

Example:

code require_once("includes/connection.php");

//capture data from form $name = $_REQUEST['name']; $visible = $_REQUEST['visible'];

//echo ($name . $visible);

function confirm_query($result_set) { if (!$result_set) { die("Database query failed: " . mysql_error); }   }

$query = "INSERT INTO topics (name, visible)               VALUES ('{$name}',{$visible})";

$topics_set = mysql_query($query, $connection); confirm_query($topics_set); code

=Read=

Get all records in a table:
$query = "SELECT * FROM topics ORDER BY name ASC";

Example:

code require_once("includes/connection.php");

function confirm_query($result_set) { if (!$result_set) { die("Database query failed: " . mysql_error); }   }

$query = "SELECT *               FROM topics                ORDER BY name ASC";

$topics_set = mysql_query($query, $connection); confirm_query($topics_set); code

Get one record by its id:
$queryRecord = "SELECT * FROM topics WHERE id = 4 LIMIT 1";

Example:

code $queryRecord = "SELECT *               FROM topics                WHERE id = 4                LIMIT 1";

$topicsRecord_set = mysql_query($queryRecord, $connection); // make sure confirm_query function is available // see code examples above confirm_query($topicsRecord_set);

$editTopic = mysql_fetch_array($topicsRecord_set); // retrieve name from the record code

=Update=

$query = "UPDATE topics SET name = '{$name}', visible = {$visible} WHERE id = {$id}";

Example:

code require_once("includes/connection.php"); // need the id $id = $_REQUEST['id']; $name = $_REQUEST['name']; $visible = $_REQUEST['visible'];

//echo ($id . $name . $visible);

function confirm_query($result_set) { if (!$result_set) { die("Database query failed: " . mysql_error); }   }

$query = "UPDATE topics SET name = '{$name}', visible = {$visible} WHERE id = {$id}";

$topics_set = mysql_query($query, $connection); confirm_query($topics_set); code

Delete

TBD

.