<?php
require_once('templates/layout.php');
page_header('Add bookmark');
?>
<h1>Add a new bookmark</h1>
<?php
//Include constants file; this file simply defines two constants,
//DB_USERNAME and DB_PASSWORD. We haven't written them here for
//obvious reasons :-)
require_once('/home/uchiyama/public_html/includes/constants.php');

if (isset(
$_POST['submitted'])) {
    
//The form was submitted; let's add a new contact to the database
    
$uid        $_POST['uid'];
    
$url        $_POST['url'];
    
    
//Let's check to see if the details submitted were valid
    
if (!is_numeric($uid) || empty($url)) {
        echo 
"<p>Error: You must specify all fields.</p>";
    } else {
        
//Connects to the database server
        
if (!mysql_connect('localhost'DB_USERNAMEDB_PASSWORD)) {
            exit(
'No connection');
        }
        
        
//Selects the appropriate database (uchiyama_section3 is the name of our database)
        
if (!mysql_select_db('uchiyama_section3')) {
            exit(
'Cannot select database');
        }
        
        
//Build the insertion query
        
$query "INSERT INTO bookmarks ("
        
."bookmark_uid, bookmark_url) VALUES ("
        
."'{$uid}',"
        
."'".mysql_real_escape_string($url) ."'"
        
.")";
        
        echo 
"<p>The query was:</p><pre>$query</pre>";
        
        
//Actually execute the insertion query
        
if (!mysql_query($query)) {
            exit(
'Insert failed');
        }
        
        echo 
"Added bookmark successfully!";
    }

} else {
    
//The form wasn't submitted; the user came directly to this page.
    //Let's just display the form.
?>
    <form method="post" action="newbookmark.php">
    <div style="line-height: 180%">
    <input type="hidden" name="submitted" value="yes" />
    <input type="hidden" name="uid" value="<?php echo @$_GET['uid']; ?>" />
    URL: <input type="text" name="url" size="30" />
    <br /><input type="submit" value="Add contact" />
    </div>
    </form>
<?php
}

page_footer();