Source of: example.6-2.php

<?
/*
Source code example for Web Database Applications

Unless otherwise stated, the source code distributed with this book can be
redistributed in source or binary form so long as an acknowledgment appears
in derived source files.
The citation should list that the code comes from Hugh E.
Williams and David Lane, "Web Database Application with PHP and MySQL"
published by O'Reilly & Associates.
This code is under copyright and cannot be included in any other book,
publication, or educational product without permission from O'Reilly &
Associates.
No warranty is attached; we cannot take responsibility for errors or fitness
for use.
*/
?>
<?php
  
include 'db.inc';
  include 
'error.inc';

  if (empty(
$regionName) || empty($description))
  {
?>
    <!DOCTYPE HTML PUBLIC 
               "-//W3C//DTD HTML 4.0 Transitional//EN"
               "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
      <title>Insert a Region</title>
    </head>
    <body>
    <form enctype="multipart/form-data" 
     action="example.6-2.php" method="post">
    Region_name:
    <br><input type="text" name="regionName" size=80>
    <br>Description:
    <br><textarea name="description" rows=4 cols=80>
    </textarea>
    <input type="hidden" name="MAX_FILE_SIZE" value="30000">
    <br>Region map (GIF format): 
    <input name="userfile" type="file">
    <br><input type="submit">
    </form>
    </body>
    </html>
<?php    
  
}
  else 
  {
     
$regionName clean($regionName50);
     
$description clean($description2048);

     if (!(
$connection = @ mysql_connect($hostName
                                       
$username
                                       
$password)))
        die(
"Could not connect to database");

     if (!
mysql_select_db($databaseName$connection))
        
showerror();

     
// Was an image file uploaded?
     
if (is_uploaded_file($userfile))
     {
       
// Open the uploaded file
        
$file fopen($userfile"r");
    
       
// Read in the uploaded file
        
$fileContents 
          
fread($filefilesize($userfile)); 

       
// Escape special charcters in the file
        
$fileContents AddSlashes($fileContents);
     }  
     else
        
$fileContents NULL;

       
$insertQuery "INSERT INTO region VALUES (NULL, " .
                    
"\"" $regionName "\", " .
                    
"\"" $description "\", " .
                    
"\"" $fileContents "\")";

     if ((@ 
mysql_query ($insertQuery,
                         
$connection))
        && @ 
mysql_affected_rows() == 1)
           
header("Location: example.6-3.php?" .
                  
"regionId="mysql_insert_id($connection) .
                  
"&status=T");  
        else
           
header("Location: example.6-3.php?" .
                  
"status=F");  
  } 
// if else empty()
?>


© 2000--2001 Hugh E. Williams and David Lane.
Valid HTML 4.01!