Thursday, September 13, 2012

SQL BETWEEN Operator


MySQL BETWEEN Operator 
BETWEEN operator is used to select a range of data between two values.Here in this example we select the data between two dates.
Create table product_issue in your my_database.

CREATE TABLE IF NOT EXISTS `product_issue` (
  `id` int(30) NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `name` varchar(40) NOT NULL,
  `quantity` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `product_issue`
--

INSERT INTO `product_issue` (`id`, `date`, `name`, `quantity`) VALUES
(1, '2012-09-01', 'Computers', '12'),
(2, '2012-09-02', 'Mouses', '20'),
(3, '2012-09-03', 'Key Boards', '23'),
(4, '2012-09-05', 'UPS', '12'),
(5, '2012-09-06', 'Monitors', '40'),
(6, '2012-09-07', 'CPU', '22');

Next create bellow PHP file and find the result between 2012-09-01-2012-09-05,For the posting of these two dates to php file create bellow HTML file also.

HTML file 




<table bgcolor=#cccccc align="center">
<form action="find.php" method="post">
<tr><td><input type="text" name="date1"></td></tr>
<tr><td><input type="text" name="date2"></td></tr>
<tr><td><input type="submit" name="Find"></td></tr>
</form>
</table>

find.php

<?php
$date1=$_POST['date1'];
$date2=$_POST['date2'];
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);

$result = mysql_query("SELECT*FROM product_issue WHERE date BETWEEN '$date1' AND '$date2'");

echo "<table border='1' align='center'>
<tr>
<th>Date</th>
<th>Item</th>
<th>Amount</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['quantity'] . "</td>";
 echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

Date Item Amount
2012-09-01Computers12
2012-09-02Mouses20
2012-09-03Key Boards23
2012-09-05UPS12

SQL UNION Operator


MySQL UNION Operator 

You can use union if you want to select rows one after the other from several tables, or several sets of rows from a single table all as a single result set.This is actually merging 2 or more tables having columns of same data type.
Create following 2 tables in my_database for this example of codes

CREATE TABLE IF NOT EXISTS `emp_name` (
  `id` int(40) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `country` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `emp_name`
--

INSERT INTO `emp_name` (`id`, `name`, `country`) VALUES
(1, 'Oksana Bebiac', 'Ukrane'),
(2, 'Larisa Volk', 'Ukrane'),
(3, 'Ruth Allon', 'USA'),
(4, 'Kylee Nguyen', 'Japan');
CREATE TABLE IF NOT EXISTS `emp_name2` (
  `id` int(30) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `country` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `emp_name2`
--

INSERT INTO `emp_name2` (`id`, `name`, `country`) VALUES
(1, 'Oksana Bebiak', 'Ukrane'),
(2, 'Anlen Jozz', 'Ukrane'),
(3, 'Eshe Biabaku', 'USA'),
(4, 'Lizza Diction', 'Japan');

Then Create following PHP codes to union tables emp_name and emp_name2

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);

$result = mysql_query("SELECT name FROM emp_name UNION SELECT name FROM emp_name2");

echo "<table border='1'>
<tr>
<th>Name</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
 echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>


Name
Oksana Bebiak
Larisa Volk
Ruth Allon
Kylee Nguyen
Anlen Jozz
Eshe Biabaku
Lizza Diction

You may have noticed there are two Okasana Babiak in two tables but displays only one This will be resolved by using UNION ALL operator instead of UNION.

Sunday, September 9, 2012

Connecting to Two Databases PHP


Connecting to Two Databases PHP


This Tutorial is to view data from two  data base table. you have created table division in my_database in previous lesson create table division


CREATE TABLE IF NOT EXISTS `division` (
  `uid` int(40) NOT NULL AUTO_INCREMENT,
  `uname` varchar(45) NOT NULL,
  `division` varchar(45) NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Then create table employee in oops database

CREATE TABLE IF NOT EXISTS `employee` (
  `ID` int(20) NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) NOT NULL,
  `Surname` varchar(30) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Next creating bellow php file you can see the data in two table from two databases

ID Name Division
1DianaElectronic
2JackComputer

ID Name Surname
1LarisaVolk
2OksanaBabiak



<?php
$hostname="localhost";
$username="root";
$password="";
$con1 = mysql_connect("localhost","root","");
if (!$con1)
  {
  die('Could not connect: ' . mysql_error());
  }
$con2 = mysql_connect("localhost","root","");
if (!$con2)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db('my_database',$con1);
$query1=mysql_query('select * from divition');

echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Division</th>
</tr>";

while($row = mysql_fetch_array($query1))
  {
  echo "<tr>";
  echo "<td>" . $row['uid'] . "</td>";
  echo "<td>" . $row['uname'] . "</td>";
  echo "<td>" . $row['division'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
echo "</br>";
mysql_select_db('oops', $con2);
$query2=mysql_query('select * from employee');
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
</tr>";

while($row = mysql_fetch_array($query2))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['Name'] . "</td>";
  echo "<td>" . $row['Surname'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>

Insert Data into MySQL in Object Orient PHP


Insert Data into MySQL in Object Orient PHP 

Create table employee in oops database

CREATE TABLE IF NOT EXISTS `employee` (
  `ID` int(20) NOT NULL AUTO_INCREMENT,
  `Name` varchar(50) NOT NULL,
  `Surname` varchar(30) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Next create person php which we take as include file where contain class and variables that we are going to input as name and surname

<?php
class person
{
    var $surname;
    var $name;
    function set_surname($new_surname)
    {
        $this->surname=$new_surname;
    }

    function get_surname()
    {
        return $this->surname;
    }

    function set_name($new_name)
    {
        $this->name=$new_name;
    }

    function get_name()
    {
        return $this->name;
    }
}
?>


Then create the form to input data

NAME Surname<?php
 include 'person.php';
 ?>
 <html>
 <body>
 <form action="insert.php" method="post"/>
  NAME<input type="text" name="name" />
  Surname<input type="text" name="surname" />
  <input type="submit" value="submit">
  </form>
  </html>
    </body>

Next create insert.php which takes the information to MySQL table and write the data.
<?php
    include("person.php");
    $con = mysql_connect("localhost", "root", "");
    if (!$con)
    {
        die("couldn't connect ".mysql_error());
    }
    mysql_select_db("oops", $con);

    $person = new person;
    $person->set_name($_POST['name']);

    $person1 = new person;
    $person1->set_surname($_POST['surname']);


    $sql="INSERT INTO employee (ID,Name,Surname ) VALUES ('','".$person->get_name()."','".$person1->get_surname()."')";
    if (!mysql_query($sql, $con))
    {
    die('Error in inserting '.mysql_error());
    }

    ?>

Friday, September 7, 2012

Simple Video Gallery in php



Simple Video Gallery in php 


This tutorial is created to upload your videos in to a folder called "video" and also send ID,Date Added,Name of video and video description in to a MySQL table called "myvideo" in my_database Where you can select the video and view it.
Create following upload scripts file first

Video Upload


upload your videos
Title
Select file
Video Description

<html>
 <head>
 <title>Video Upload</title>
</head>
<body>
<center><h2>Video Upload </h2></center>
<form action="vd3.php" method="post" enctype="multipart/form-data" name="form1" id="form1" > <tr><td>
<table align="center">
<tr><td colspan=2><hr></hr></td></tr>
<tr> <td><strong>upload your videos </strong></td> </tr>
<tr><td>Title</td></tr>
<tr><td><input type="text" name="txtName"></td></tr>
 <tr> <td>Select file <input name="ufile" type="file" id="ufile" size="50" /></td> </tr>
<tr><td>Video Description</br><textarea cols="40" rows="5" name="details">
</textarea></td></tr>
<tr><td align="center"><input type="submit" name="Submit" value="Upload" /></td> </tr>
</form>
<tr><td colspan=2><hr></hr></td></tr>
</table>
</body>
</html>

Then create vd3.php which upload file in to upload folder and write details in to MySQL table

<?php
$path1= "video/".$_FILES['ufile']['name'];

$date = date("y/m/d");
$time = date("g:i a");
$file_name = $_FILES['ufile']['name'];
if($file !=none)
{
 if(copy($_FILES['ufile']['tmp_name'], $path1))
{
 mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("my_database");
$strSQL = "INSERT INTO myvideo";
 $strSQL .="(date,time,Name,file,details) VALUES ('$date','$time','".$_POST["txtName"]."','$file_name','".$_POST["details"]."')";

mysql_query($strSQL); echo "<center>Uploaded To videos</center> ";
  mysql_close();
 }
}
 else { echo "ERROR.....";
}
?>

Your uploaded files could be viewed by using the bellow php file

Video Gallery

ID
Date Added
Name Of Video
Open File
Video Description
1 12/09/07 Butter Production Butter Production steps 


<?php
 $dbHost = 'localhost';
$dbUser = 'root';
 $dbPass = '';
$dbName = 'my_database';
$dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());
?>
<html>
 <head>
<title></title>
</head>
<body>
 <h2><font color="#54164e"><center>Video Gallery</font></center></h2>
<table align="center" bgcolor="#9a7608" cellpadding="10" cellspacing="3">
<tr bgcolor="#ffffff">
<th> <center> ID </center>
</th> <th> <center>Date Added</center> </th>
<th> <center>Name Of Video</center> </th>
<th> <center> Open File </center> </th>
 <th> <center> Video Description</center> </th>
</tr>

<?php
 $query = "SELECT * FROM myvideo";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
 ?>
<form name="form" method="post">
<tr bgcolor="#ffffff">
<td><?php echo $row["ID"];?></td>
 <td><?php echo $row["date"];?></td>
<td><?php echo $row["Name"];?></td>
<th><a href="video/<?php echo $row['file'];?>"><img src="vd.png"></a></th>
<td><?php echo $row["details"];?> </td></tr>
 <?php
}
?>

</table>
</form>
</body>
</html>

Thursday, September 6, 2012

insert data into one table and update another


Insert data into one table and update another 

Create two table called "order1" and "order2" in my_database

CREATE TABLE IF NOT EXISTS `order1` (
  `id` int(30) NOT NULL AUTO_INCREMENT,
  `req_id` varchar(30) NOT NULL,
  `Fname` varchar(40) NOT NULL,
  `Type` varchar(40) NOT NULL,
  `subdate` varchar(40) NOT NULL,
  `startdate` varchar(40) NOT NULL,
  `status` varchar(40) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `req_id` (`req_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
CREATE TABLE IF NOT EXISTS `order2` (
  `id` int(30) NOT NULL AUTO_INCREMENT,
  `req_id` varchar(30) NOT NULL,
  `Fname` varchar(40) NOT NULL,
  `Type` varchar(40) NOT NULL,
  `subdate` varchar(40) NOT NULL,
  `startdate` varchar(40) NOT NULL,
  `status` varchar(40) NOT NULL,
  `res_officer` varchar(40) NOT NULL,
  `findate` varchar(40) NOT NULL,
  `dildate` varchar(40) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `req_id` (`req_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

Create table for entering data in to table order1

Reception Counter
Registration Number
Name
Request Type
Submitted Date
Started Date
<table bgcolor=#dec09e align="center">
<tr bgcolor=#f1ede8><th colspan=2>Reception Counter</th></tr>
<form action="enter.php" method="post">
<tr bgcolor=#f1ede8><td>Registration Number</td><td><input type="text" name="req_id"></td></tr>
<tr bgcolor=#f1ede8><td>Name</td><td><input type="text" name="Fname"></td></tr>
<tr bgcolor=#f1ede8><td>Request Type</td><td><input type="text" name="Type"></td></tr>
<tr bgcolor=#f1ede8><td>Submitted Date</td><td><input type="text" name="subdate"></td></tr>
<tr bgcolor=#f1ede8><td>Started  Date</td><td><input type="text" name="startdate"></td></tr>
<tr bgcolor=#f1ede8><td colspan=2 align="right"><input type="submit" value="ENTER"/></td></tr>
</form>
</table>

Then create data enter php called "enter.php"

<?php
$req_id=$_POST['req_id'];
$Fname=$_POST['Fname'];
$Type=$_POST['Type'];
$subdate=$_POST['subdate'];

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);

$sql="INSERT INTO order1 (id,req_id, Fname, Type,subdate)
VALUES
('','$req_id','$Fname','$Type','$subdate')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

Now create file to post Registration Number(req_id) into table order1 to get entered data from the Reception Counter Form

Enter Registration Number
<html>
<head>
<title></title>
</head>

<div align="center">
<table boder="0">
<tr><th colspan="2"BGCOLOR="#91a9db">
Enter Registration Number</th></tr>
<form action="edit.php" method="post">
 <tr><td BGCOLOR="#e3e7f0"> 
<center><input type="text" name="req_id" />
</center></td></tr>
  <tr><td BGCOLOR="#91a9db" colspan="2">  
<center>
<input type="submit" name="submit" value="Submit" />
</center></td></tr>
 </form>
</table> 
</div>

Next create edit.php
 
<?php
$dbHost = 'localhost';
$dbUser = '';
$dbPass = '';
$dbName = 'my_database';
$dbRoot='root';
$dbConn = mysql_connect ($dbHost,$dbRoot, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());
?>
<html>
<head>
<title></title>
</head>
<body>

<table align="center"  cellpadding=10 cellspacing=3 width="600" border="0" bgcolor=#91a9db>
<tr bgcolor=#91a9db><th colspan=6>VIC-Service Request External</th></tr>
<?php

$req_id=$_POST['req_id'];

$query = "SELECT * FROM order1 where req_id='$req_id'";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){
?>
<form action="enter2.php" method="post">



<tr  bgcolor="#ffffff"><th align="right"> Reg. Number <br> </th>
<td ><input readonly name="req_id" value="<?php echo $row["req_id"];?>" /></td>
<th align="right"> Name  </th>
<td><input readonly name="Fname" value="<?php echo $row["Fname"];?>" /></td>
<th align="right">Request Type  </th>
<td><input readonly name="Type" value="<?php echo $row["Type"];?>" /></td></tr>
<tr  bgcolor="#ffffff">
<th align="right">Request date  </th>
<td><input readonly name="subdate" value="<?php echo $row["subdate"];?>" /></td>
<th align="right">Date of Commence  </th>
<td><input  name="startdate" value="<?php echo $row["startdate"];?>" /></td>
<th align="right">Status </th>
<td><input  name="status" value="<?php echo $row["status"];?>" /></td>
</tr>
<tr  bgcolor="#ffffff">
<th align="right">Responsible Officer</th>
<td><input  name="res_officer"/></td>
<th align="right">Date of Finished </th>
<td><input  name="findate"/></td>
<th align="right">Date of Dilivery</th>
<td><input  name="dildate"/></td>
</tr>
<tr><td BGCOLOR="#91a9db" colspan="6">
<center><input type="submit" name="submit" value="Submit" /></center></td></tr>
<?php }?>
</table>
</body>
</html>

Then create enter2.php which update table order1 and data in to table order2

<?php
$req_id=$_POST['req_id'];
$Fname=$_POST['Fname'];
$Type=$_POST['Type'];
$subdate=$_POST['subdate'];
$startdate=$_POST['startdate'];
$status=$_POST['status'];
$res_officer=$_POST['res_officer'];
$findate=$_POST['findate'];
$dildate=$_POST['dildate'];

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);

$sql="INSERT INTO order2 (id,req_id, Fname,Type,subdate,startdate,status,res_officer,findate,dildate)
VALUES
('','$req_id','$Fname','$Type','$subdate','$startdate','$status','$res_officer','$findate','$dildate')";

$sql2="UPDATE order1 Set startdate='$startdate',status='$status' WHERE req_id='$req_id'";
mysql_query($sql);
mysql_query($sql2);
mysql_close($con);
echo "Records Added";
?>

Processing gif in MySQL


Processing gif in MySQL

IF MySQL result==processing gif file could be included to the resultant MySQL report page


Production Division
Number Sample Type Status
1 Milk
2 Meat finish
3 Blood finished
4 Tissue

Create table as bellow in my_database

CREATE TABLE IF NOT EXISTS `sample` (
  `id` int(40) NOT NULL AUTO_INCREMENT,
  `type` varchar(40) NOT NULL,
  `status` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `sample`
--

INSERT INTO `sample` (`id`, `type`, `status`) VALUES
(1, 'Milk', 'processing'),
(2, 'Meat', 'finish'),
(3, 'Blood', 'finished'),
(4, 'Tissue', 'processing');

PHP Display file is as bellow

<?php
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Production Division</td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>Number</font></th>
<th><font color=#ffffff>Sample Type</font></th>
<th><font color=#ffffff>Status</font></th>

</tr>";
$host="localhost";
$username="root";
$password="";
$db_name="my_database";
$tbl_name="sample";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT*FROM $tbl_name";

$result=mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
if ($row['status'] == "processing")
            {
                echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['type']."</td>
<td><img src='processing1.gif'></td>


</tr>";
            } else
            {
               echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['type']."</td>
<td>".$row['status']."</td>

</tr>";
}
}
echo "</table>";
  ?>

Wednesday, September 5, 2012

Data retrieving in object oriented php programming


Data retrieving in object oriented php programming 

The object model was rewritten to allow for better performance and more features. This was a major change from PHP 4. PHP 5 has a full object model.
This Tutorial start with the employee  table you created in my_database.create table employee.

CREATE TABLE IF NOT EXISTS `employee` (
  `id` int(40) NOT NULL AUTO_INCREMENT,
  `division` varchar(40) NOT NULL,
  `name` varchar(40) NOT NULL,
  `appointed` varchar(40) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `employee`
--

INSERT INTO `employee` (`id`, `division`, `name`, `appointed`) VALUES
(1, 'Production', 'Jhones', '1965'),
(2, 'Service', 'Taylor', '1970'),
(3, 'Marketting', 'Bayker', '1980'),
(4, 'Human resource', 'Steven', '1988'),
(5, 'Production', 'Jennifer', '1990'),
(6, 'Service', 'Lopez', '2000'),
(7, 'Marketting', 'Bayker', '2000'),
(8, 'Marketting', 'Bayker', '2010'),
(9, 'Human resource', 'Steven', '2008');

Then by using class and functions you can retrieve the data using below scripts

<table border=1 align="center"><tr>
<th>ID</th><th>Division</th><th>Name</th><th>Appointed Year</th></tr>
<?php
class MySQL {


  private $host;
  private $username;
  private $password;
  private $database;
  private $conn;

  public function __Construct($set_host, $set_username, $set_password){
    $this->host = $set_host;
    $this->username = $set_username;
    $this->password = $set_password;
  
    $this->conn = mysql_connect($this->host, $this->username, $this->password)
                  or die("Couldn't connect");
  }

  public function Database($set_database)
  { 
    $this->database=$set_database;
 
    mysql_select_db($this->database, $this->conn) or die("cannot select Dataabase");
  }

  public function Fetch($table_name){
 
    return mysql_query("SELECT * FROM ".$table_name, $this->conn);       
  }

}

$connect = new MySQL('localhost','root','');
$connect->Database('my_database');
$posts = $connect->Fetch('employee');

if ($posts && mysql_num_rows($posts) > 0) {
     echo "<center>Table Employee:</center><BR>";
     while ($record = mysql_fetch_array($posts)) {
         echo "<tr><td>".$record[0]."</td>";
        echo "<td>".$record[1]."</td>";
        echo "<td>".$record[2]."</td>";
        echo "<td>".$record[3]."</td>";
        echo "</tr>";
     }
} else {
     echo "No Records Found!";
}
echo "</table>";
?>

Retrieved table look like bellow


Table Employee:

IDDivisionNameAppointed Year
1ProductionJhones1965
2ServiceTaylor1970
3MarkettingBayker1980
4Human resourceSteven1988
5ProductionJennifer1990
6ServiceLopez2000
7MarkettingBayker2000
8MarkettingBayker2010
9Human resourceSteven2008

Multiple Selection in to a MySQL table single row


Multiple Selection in to a MySQL table single row 

Select all  following options

Create table called "test1" in my_database

 CREATE TABLE IF NOT EXISTS `test1` (
  `test` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Creating fallowing php file you can insert data into mysql table as follows





<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_database", $con);


 $test=$_POST['test'];

 $query="INSERT INTO test1 (test) VALUES ('".implode(',',$test)."')";
 mysql_query($query) or die(mysql_error());

 echo "Record is inserted";
?>
<form action="" method="post">
<select name="test[]" multiple="multiple">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
    <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>