Tuesday, 25 February 2014

Save a form value to database and attached file to a particular folder using php and mysql

Here you can find the code please click below
DEMO



index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Iphone|Event|Sytem</title>
<link href="style.css" type="text/css" rel="stylesheet" media="screen" />
</head>
<body>
<div id="container">
<div id="form">
<form action="save.php" method="post" enctype="multipart/form-data">
  <table align="center" border="0.5">
    <tr>
      <td >Event Name :</td>
      <td><input type="text" name="ename"   /></td>
    </tr>
    <tr>
      <td >Event Location :</td>
      <td><input type="text" name="elocation"   /></td>
    </tr>
     <tr>
      <td >Event Date :</td>
      <td><input type="text" name="edate"   /></td>
    </tr>
     <tr>
      <td >Start Time :</td>
      <td><input type="text" name="stime"   /></td>
    </tr>
     <tr>
      <td >End Time :</td>
      <td><input type="text" name="etime"   /></td>
    </tr>
     <tr>
      <td >Organiser's Websites :</td>
      <td><input type="text" name="website"   /></td>
    </tr>
     <tr>
      <td >Event Image :</td>
      <td><input type="file" name="image"   /></td>
    </tr>
    <tr>
      <td >&nbsp;</td>
      <td><input type="submit"  value="Send" /><input type="reset"  value="Clear" /></td>
    </tr>
   </table></form>
</div>
</div>
</body>
</html>


Open localhost/phpmyadmin and make a database like iphone then select database made by you and run this sql query 

-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Feb 22, 2014 at 09:42 PM
-- Server version: 5.5.27
-- PHP Version: 5.4.7

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `iphone`
--

-- --------------------------------------------------------

--
-- Table structure for table `event`
--

CREATE TABLE IF NOT EXISTS `event` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ename` varchar(100) NOT NULL,
  `elocation` varchar(100) NOT NULL,
  `date` varchar(50) NOT NULL,
  `stime` varchar(50) NOT NULL,
  `etime` varchar(50) NOT NULL,
  `website` varchar(100) NOT NULL,
  `image` varchar(4000) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `event`
--

INSERT INTO `event` (`id`, `ename`, `elocation`, `date`, `stime`, `etime`, `website`, `image`) VALUES
(1, 'ritu', 'noida', '12 feb 2014', '', '12 feb 2014', 'www.zibrasoft.com', '403398_414247441964542_153541410_n copy.jpg'),
(2, 'singing', 'noida', '12 feb 2014', '10am', '1pm', 'www.zibraosft.com', 'Rituraj_-java.docx'),
(3, 'singing', 'noida', '12 feb 2014', '10am', '1pm', 'www.zibraosft.com', 'Rituraj-java.docx'),
(4, 'singing', 'noida', '12 feb 2014', '10am', '1pm', 'www.zibraosft.com', 'sample.sql'),
(5, 'singing', 'noida', '12 feb 2014', '10am', '1pm', 'www.zibraosft.com', 'sample.sql'),
(6, 'singing', 'noida', '12 feb 2014', '10am', '1pm', 'www.zibraosft.com', 'sample.sql');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Now You have to write php-script to run your form

save.php

<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("iphone",$db) or die("Error mysql connection");
$path="event-image/";
$path=$path.basename($_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'],$path))
{
$ename=$_POST['ename'];
$elocation=$_POST['elocation'];
$date=$_POST['edate'];
$stime=$_POST['stime'];
$etime=$_POST['etime'];
$website=$_POST['website'];
$image=basename($_FILES['image']['name']);
$sql="insert into event(ename,elocation,date,stime,etime,website,image) values('$ename','$elocation','$date','$stime','$etime','$website','$image')";
$result=mysql_query($sql,$db);

}
echo 'Message has been sent<br>';



?>


In the above code 
$path="event-image/";
event-image is the folder name where the attached file will be saved.
make a folder to your root with this name or according to you.

No comments:

Post a Comment

Printing first 50 Fibonacci numbers Using PHP Program

Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers. For example. 0 , 1 , 1 , 2 , 3 , 5...