<?php
    require_once '../library/config.php';
    require_once '../library/functions.php';


    if(isset($_POST['txt_book']))
    {
    $book_name = $_POST['txt_book'];
    $book_desc = $_POST['txt_desc'];
    $img_name   = $_FILES['image_file']['name'];
    $loaded_image   = $_FILES['image_file']['tmp_name'];

    $ext = strrchr($img_name, ".");
    $new_one = md5(rand()) . $ext;
    $path_to_image = BOOKS_DIR . $new_one;
    $result = create_icon($loaded_image, $path_to_image, icon_WIDTH);

    if (!$result) {
    echo "Error uploading file";
    exit;
    }

    if (!get_magic_quotes_gpc()) {
    $book_name  = addslashes($book_name);
    $book_desc  = addslashes($book_desc);
    }  

    $query = "INSERT INTO sc_books (book_name, book_description, book_image, book_date) 
    VALUES ('$book_name', '$book_desc', '$new_one', NOW())";

    mysql_query($query) or die('Error, add book failed : ' . mysql_error());                    


    echo "<script>window.location.href='indexoo.php?page=display_books';</script>";
    exit;
    }        
    ?>

    <form action="" method="post" enctype="multipart/form-data" name="add_form" id="add_form">
    <table width="65%" border="0" cellpadding="2" cellspacing="1" class="small_table">
    <tr> 
    <th width="150">Book name: </th>
    <td width="150" bgcolor="#AFEEEE"> 
    <input name="txt_book" type="text" size = "50" id="txt_book"></td>
    </tr>
    <tr> 
    <th width="150">Description: </th>
    <td bgcolor="#AFEEEE"> <textarea name="txt_desc" cols="70" rows="4" id="txt_desc"></textarea> 
    </td>
    </tr>
    <tr> 
    <th width="150">Book image: </th>
    <td bgcolor="#AFEEEE"> <input  name="image_file" type="file" size = "70" id="image_file"></td>
    </tr>
    <br />
    <tr> 
    <td width="150" bgcolor="#FFFFFF">&nbsp;</td>
    <td bgcolor="#AFEEEE"> <input backgroud-color = "#FCF4F4" name="button_add" type="submit" id="button_add" value="Add book"> &nbsp;
    <input name="cancel_button" type="button" id="cancel_button" value="Cancel" onClick="window.history.back();"></td>
    </tr>
    </table>
    </form>