Hello everybody!
I'm a newbie with PHP and i'm trying to build my first application.
It's a multipart data form, with image upload functionality.
<form method="post" action="process.php" enctype="multipart/form-data">
<label>Bus Name: </label>
<input name="busname" id="name" type="text" maxlength="100" />
<br />
<label>Bus Description: </label>
<textarea name="description" id="description" cols="45" rows="5"></textarea>
<br />
<label>select an image:</label>
<input type="file" id="file" name="file" />
<input type="submit" id="add" value="add" />
</form>
The data is submitted to "process.php" file:
include 'config.php';
/* File Upload */
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("../upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " - file already exists. ";
echo "please go back and select another image file." ;
exit;
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../upload/" . $_FILES["file"]["name"]);
echo "image saved successfully</br>";
echo $_FILES["file"]["name"] . '</br>';
}
}
}
else
{
echo "Invalid file. Please go back and choose a valid image file!";
exit;
}
/* post variables */
if(isset($_POST['add']))
{
$username = $_POST['name'];
$password = $_POST['description'];
}
/* insert name and description */
$sql="INSERT INTO bus (name, description) VALUES ('$_POST[busname]','$_POST[description]')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
As you can see the values of "name" and "description", will be inserted to the database.
Also i want to get the image filename of the uploaded file into database. I'm gonna use it at a later time to link the uploaded image..
I'm trying to print the "$_FILES["file"]["name"]" but i'm completely stuck..
Is there any way to do this??
Thanks in advance!
DATABASE:
-- Database: `busdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `bus`
--
CREATE TABLE IF NOT EXISTS `bus` (
`busID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(220) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(220) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(220) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`busID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=29 ;