I am attempting to create a form for the uploading of images. I want the 'userfile' name to be stored in the MYSQL database and the image file itself to be copied into a folder named 'uploads'. I'm not going very well with it as I can't even manage to get the data to be inserted into the database... 🙁
Once I get this part working, including the file saved into the folder, I will also be adding steps to limit file type to JPEG and file size to <1000. Plus, I also want to rename the uploaded file to prevent overriding an existing file if users upload a file with the same name.
If anyone can tell me why I am unable to insert the data into the database that will be my first step in moving closer to my goal. Thanks!
This is what I have so far:
<?php
session_start();
require("../admin/config.php");
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$target = "../uploads/";
$name = (isset($_POST['name']) ? strip_tags($_POST['name']) : NULL);
$displayname = (isset($_POST['displayname']) ? strip_tags($_POST['displayname']) : NULL);
$email = (isset($_POST['email']) ? strip_tags($_POST['email']) : NULL);
$phone = (isset($_POST['phone']) ? (int)($_POST['phone']) : NULL);
$country = (isset($_POST['country']) ? strip_tags($_POST['country']) : NULL);
$phototitle = (isset($_POST['phototitle']) ? strip_tags($_POST['phototitle']) : NULL);
$phototext = (isset($_POST['phototext']) ? nl2br($_POST['phototext']) : NULL);
$userfile = (isset($_POST['userfile']) ? strip_tags($_POST['userfile']) : NULL);
$gallerycheckbox = (isset($_POST['gallerycheckbox']) ? (int)($_POST['gallerycheckbox']) : NULL);
if(isset($_POST['submit'])){
$sql = sprintf(
"INSERT INTO gallery (name, display_name, email, phone, country, phototitle, phototext, userfile, gallerycheckbox)
VALUES(%s,%s, %s, %d, %s, %s, %s, %s, %d)",
"'" . mysql_real_escape_string($name) . "'",
"'" . mysql_real_escape_string($displayname) . "'",
"'" . mysql_real_escape_string($email) . "'",
"'" . $phone . "'",
"'" . mysql_real_escape_string($country) . "'",
"'" . mysql_real_escape_string($phototitle) . "'",
"'" . mysql_real_escape_string($phototext) . "'",
"'" . mysql_real_escape_string($userfile) . "'",
"'" . $gallerycheckbox . "'"
);
$result = mysql_query($sql);
move_uploaded_file($_FILES['userfile']['tmp_name'], $target . basename($_FILES['userfile']['name']));
header("Location: pages/gallery-sent.php");
}
else{
require("../pages/header.php");
?>