Bambookit Home    Middleware

Chapter 19. Middleware




Creating a table in MySQL

create_table.php
<?php

//Connect to the database
$connection = mysql_connect ( "localhost", "myusername", "mypassword" );
if ($connection) 
{
$create_table_string = "CREATE TABLE events (".
             "id INTEGER NOT NULL AUTO_INCREMENT, ".
             "field1 INTEGER, ".
             "title VARCHAR(50), ".
             "desc VARCHAR(255), ".
             "PRIMARY KEY (id))";

$message="Table created successfully.";

mysql_db_query ( "mysql", $create_table_string ) or $message="Cannot create the table!";

} else 
{ $message="Error connecting to database!"; }
?>

<Widget setName="status" setLabel="<?php echo $message; ?>"/>


Inserting into a table from a web form

insert_row.php
<?php

// Retrieve any data from GET, POST and COOKIE (gpc)
extract();

// Connect to database.
$connection = mysql_connect ( "localhost", "myusername", "mypassword" );

if ($connection)
{
   $insert_statement =
      "insert into events ".
      "(field1, title, desc)".
      " values ".
      "(".$field1.", ".$title.", ".$desc.")";

   if (mysql_db_query ( "mysql", $insert_statement)) 
   {
     $message="row added.";
   } else 
   {
     $message="unable to add row.";
   }
} else
{    
   $message="Error connecting to database!"; 
}
?>

<Widget setName="status" setLabel="<?php echo $message; ?>"/>