Sunday, September 6, 2009

Simplest Way to save data in mysql database and text file using PHP

//==============================================

C:\wamp\www\saveD\  Used as project folder

This Code creates database and tables automatically when its load first time

I use wamp server to run program

//=============================================

index.php

This use to create database and tables and prompt user to add data

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
//Creatr Database Dinamically in run time
//Make Connection To Database in Local host
//Username=root and Password=""
//This is the basic way in Xamp & Wamp
$connect =mysql_connect("localhost","root","") or die(mysql_error());
//mysql_select_db("eee");
$quary="create database db1";
//Create Database named DB1
$quary1="use db1";
//Use db1
$quary2="create table table1(timeA int,timeB int,timeC int,timeU varchar(8))";
//Creates Table named table1
//Executes queries
$results = mysql_query($quary);
$results1 = mysql_query($quary1);
$results2 = mysql_query($quary2);
if($results2==1)
{
echo "create table succesfully";
}
else
{
echo "Already Exsists";
}
?>
<form name="form1" method="post" action="insert.php">
  <input type="submit" name="Submit" value="Insert Values">
</form>
<p>&nbsp;</p>
</body>
</html>

//========================================================

insert.php

This use to take input and post them to save page

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="save.php" method="post">

  <font color="#000000"><strong><font size="+2">value1</font></strong>:</font>
  <font color="#000000">
  <input type="text" name="val1">
    </font></strong></font></label>
    <font color="#000000"><strong><font size="+2">&nbsp;
</font></strong></font></p>
  <p><font color="#000000"><strong><font size="+2">value2:
    <input type="text" name="val2">
    </font></strong></font></p>
  <p> <font color="#000000"><strong><font size="+2">
    <label>value3:</label>
  </font></strong></font><font color="#0000FF"><strong><font
size="+2"><label></label>
  </font></strong></font><strong><font color="#8000FF"
size="+2"><label></label>
    </font> <font size="+2">
    <label></label>
    </font>
    <label></label>
    </strong>
    <label>
    <input type="text" name="val3">
    </label>
    &nbsp; </p>
  <p> <font color="#FF0080" size="+2">
    <input type="submit" name="Submit" value="Send Data">
    </font> </p>
</form>
</body>
</html>

 

//============================================================

save.php

This use to write data to the database and text file

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
echo $va11=$_POST['val1'];
echo $val2=$_POST['val2'];
echo $val3=$_POST['val3'];

$ourFileName = "C:\wamp\www\saveD\outtoc.txt";
$ourFileHandle = fopen($ourFileName,'w') or die("can't open file");
fwrite($ourFileHandle,$va11);
fwrite($ourFileHandle,$val2);
fwrite($ourFileHandle,$val3);
fclose($ourFileHandle);
$date_array = date(h);

$date_array1 = date(i);

$date_array3 = date(A);

$timeU ="$date_array:$date_array1$date_array3";
$dateU="$date_array2:$date_array2";
$connect =mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("db1");
$quary3="insert into table1 values('$va11','$val2','$val3','$timeU')";
$results = mysql_query($quary3) or die(mysql_error());
if ($results==1)
{
print "\n";
echo "\n Insert values Success";
}
?>
</body>
</html>

No comments:

Post a Comment