mercredi 15 avril 2015

PHP uploading images using filepath in MySql

I am trying to create a script that enter details from a book. The image variable 'image' and 'content' is supposed to be a jpg or PDF. These are selected in the HTML create form of type 'file'.


I've been looking online at other peoples code as I am really struggling to get my head around it.


Heres what I have so far:



require_once __DIR__.'/config.php';
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);

if (isset($_POST['submit'])) {
$title = $_POST["title"];
$authors = $_POST["authors"];
$description = $_POST["description"];
$price = $_POST["price"];
$image = $_POST["image"];
$content = $_POST["content"];
$uploadDir = 'path/to/where/i/can/save/';

if(isset($_POST['submit']))

$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];

$filePath = $uploadDir . $fileName;


$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$stmt = $dbh->prepare("INSERT INTO books (title, authors, description, price, image, content ) ".
"VALUES ('$title', '$authors', '$description', '$price', '$filepath', '$content')");
$stmt->execute();
}


Not surprisingly this does not do anything, the database remains empty and the page i'm presented with has nothing on it. Can anyone help me sort this out and also tell me how I could make it so that the content is also saved as a filepath.


Sidenote:


In the database i used VARCHAR to store the variables.


Aucun commentaire:

Enregistrer un commentaire