how to store date to database

Answers were Sorted based on User's Feedback



how to store date to database..

Answer / nirav desai

You have to use Date datatype for the particular field where
you have to store date.

Is This Answer Correct ?    2 Yes 1 No

how to store date to database..

Answer / prabu

<link rel="stylesheet" href="<?php echo $this->baseurl
;?>/templates/<?php echo $this->template
;?>/css/default.css" type="text/css" />

Is This Answer Correct ?    1 Yes 1 No

how to store date to database..

Answer / dev

for database connectivity

<?php
$con = mysql_connect("localhost","your database username
here","your database password here") or die("could
not connect".mysql_error());
$db = mysql_select_db("your database name here") or die
("could not select db".mysql_error());
?>

insert into a table

$sql_query= "insert into your table name here(table fields
name here)
values(Fields values here)";
$result = mysql_query($sql_query);
?>

FOR EXAMPLE....

<?php
//for database connectivity
$con = mysql_connect("localhost","peter","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}


mysql_select_db("my_db", $con); // select a database

$firstname=$_POST['firstname'];
$lastname = $_POST['lastname'];

// insert into a table
mysql_query("INSERT INTO Persons (FirstName, LastName)
VALUES ('$firstname','lastname ')");

mysql_close($con);
?>

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
<input type="submit" />
</form>

</body>
</html>

Is This Answer Correct ?    2 Yes 3 No

how to store date to database..

Answer / sekar

<?php
include_once("config/dbcon.php");
admin_html();
if($_POST['submit']=='INSERT'){
admin_insert();
}
elseif($_GET['type']=='list'){
$id=0;
$cl_name=0;$cl_place=0;$cl_site=0;$cl_logo=0;$cl_category=0;
admin_list($id,$cl_name,$cl_place,$cl_site,$cl_logo,$cl_category);
}
elseif($_GET['type']=='del'){
admin_delete();
}
elseif($_GET['type']=='update'){
admin_update();
}
function admin_html(){
$display = "<form method='post' action=''
enctype='multipart/form-data'>
<table align='center' border='1' cellpadding='10'
cellspacing='5' width='700'>
<tr>
<td width='100%' align='center' colspan='3'><h3>College
information</h3></td>
</tr>
<tr>
<td width='40%' align='center'>College Name</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='text'
name='cl_name'></td>
</tr>
<tr>
<td width='40%' align='center'>College Place</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='text'
name='cl_place'></td>
</tr>
<tr>
<td width='40%' align='center'>College site</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='text'
name='cl_site'></td>
</tr>
<tr>
<td width='40%' align='center'>College Category</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'>
<select name='cl_cat'>
<option value='-1'>Please Select anyone</option>
<option value='mediacl'>Medical</option>
<option value='art'>Art</option>
<option value='engg'>Engg</option>
</select>
</td>
</tr>
<tr>
<td width='40%' align='center'>College Logo</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='file'
name='cl_logo'></td>
</tr>
<tr>
<td width='50%' align='center' colspan='2'><input
type='submit' value='INSERT' name='submit'></td>
<td width='50%' align='center'><a
href='admin.php?type=list'>EDIT</a></td>
</tr>";
echo $display;
}

function admin_insert(){
$cn = $_POST["cl_name"];
$cp = $_POST["cl_place"];
$cs = $_POST["cl_site"];
$cc = $_POST["cl_cat"];
$cl = admin_upload();

$select = "select cl_name, cl_place, cl_site from
collegeinformation";
$row = mysql_query($select);
$count = mysql_num_rows($row);

$insert = "insert into collegeinformation(cl_name,
cl_place, cl_site,
cl_logo,cl_category)values('$cn','$cp','$cs','$cc','$cl')";
$result = mysql_query($insert);

if($insert){
echo "1 record added";
}
else{
echo "No record added";
}


}

function admin_upload(){
$name = $_FILES['cl_logo']['name'];
$type = $_FILES['cl_logo']['type'];
$size = $_FILES['cl_logo']['size'];
$tmp= $_FILES['cl_logo']['tmp_name'];
$err = $_FILES['cl_logo']['error'];

if(($type="image/jpeg")||($type="image/gif")){

if($err>0){
echo "Error in file";
}
else{

$target_path = "images/";
$target_path = $target_path.$name;

if(move_uploaded_file($tmp,$target_path)){

echo "success";
}
else{

echo "failure";
}
}

return $target_path;

}
else{
echo "invalid File";
}
}

function
admin_list($id,$cl_name,$cl_place,$cl_site,$cl_logo,$cl_category){
$pagenum= $_GET['pagenum'];
$display_list = "";
$display_list .= "<form method='post'><table align='center'
border='1' cellpadding='8' cellspacing='2' width='700'>
<tr>
<td width='100%' align='center' colspan='8'>
<h3>list College information</h3>
</td>
</tr>
<tr>
<td width='5%' align='center'>ID</td><td width='10%'
align='center'>Name</td><td width='10%'
align='center'>Place</td><td width='10%'
align='center'>Site</td><td width='25%'
align='center'>Logo</td><td width='15%'
align='center'>category</td><td width='15%'
align='center'>DELETE</td><td width='10%'
align='center'>UPDATE</td>
</tr>
";
$pagenum = $_GET['pagenum']>0?$_GET['pagenum']:0;
if($id!=0){
echo "sekar";
$update = "update collegeinformation set
cl_name='$cl_name', cl_place='$cl_place',cl_site='$cl_site'
where id='$id'";
$up = mysql_query($update);
}
$select = "select
id,cl_name,cl_place,cl_site,cl_logo,cl_category from
collegeinformation";
$result = mysql_query($select);
while($res = mysql_fetch_array($result)){
$display_list .="<tr>
<td width='5%' align='center'>$res[0]</td><td width='10%'
align='center'>$res[1]</td><td width='10%'
align='center'>$res[2]</td><td width='10%'
align='center'>$res[3]</td><td width='25%'
align='center'>$res[4]</td><td width='15%'
align='center'>$res[5]</td><td width='15%' align='center'><a
href='admin.php?type=del&id=$res[0]&url=$res[5]'>DELETE</a></td><td
width='10%' align='center'><a
href='admin.php?type=update&id=$res[0]'>UPDATE</a></td>
</tr>";
}

$display_list .= "</table></form>";

echo $display_list;

}

function admin_delete(){

$id = $_GET['id'];
$url = unlink($_GET['url']);

$delete = "delete from collegeinformation where id ='$id'";

$result = mysql_query($delete);

if($result){

echo "1 record deleted";
}
else{

echo "Not deleted";
}

}

function admin_update(){

$id = $_GET['id'];

$select = "select id, cl_name, cl_place, cl_site from
collegeinformation where id='$id'";

$result = mysql_query($select);

while($res = mysql_fetch_array($result)){

$display_update .= "<form method='post' action=''
enctype='multipart/form-data'>
<table align='center' border='1' cellpadding='10'
cellspacing='5' width='700'>
<tr>
<td width='100%' align='center' colspan='3'><h3>College
information</h3></td>
</tr>
<tr>
<td width='40%' align='center'>College Name</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='text'
name='cl_name' value='$res[1]'></td>
</tr>
<tr>
<td width='40%' align='center'>College Place</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='text'
name='cl_place' value='$res[2]'></td>
</tr>
<tr>
<td width='40%' align='center'>College site</td>
<td width='10%' align='center'>:</td>
<td width='50%' align='center'><input type='text'
name='cl_site' value='$res[3]'></td>
</tr>
<tr>
<td width='100%' align='center' colspan='3'><input
type='submit' name='submit' value='UPDATE'></td>
</tr>
</table></form>";
echo $display_update;

if($_POST['submit']=='UPDATE'){


admin_list($res[0],$_POST['cl_name'],$_POST['cl_place'],$_POST['cl_site'],$res[4],$res[5]);
}

}



}
?>

Is This Answer Correct ?    1 Yes 2 No

how to store date to database..

Answer / kannan

Step for store data to database
1.insert.php
2.insert_ac.php
3.edit.php
4.delete.php
5.update.php
5.update_ac.php

1insert.php
<?php
$con = mysql_connect("localhost","root","") or die("could
not connect".mysql_error());
$db = mysql_select_db("sekar") or die("could not select
db".mysql_error());
?>
<!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>Untitled Document</title>
</head>
<body>
<div align="center" style="float:left;width:400px;">
<form method="post" action="insert_ac.php">
<table border="1" cellpadding="10" cellspacing="4" width="400">
<tr>
<td colspan="3" align="center" width="100%">Enter Customer
Infomation</td>
</tr>
<tr>
<td align="left" width="40%">Name</td>
<td align="center" width="10%">:</td>
<td align="left" width="50%"><input type="text" name="name"
/></td>
</tr>
<tr>
<td align="left" width="40%">Last Name</td>
<td align="center" width="10%">:</td>
<td align="left" width="50%"><input type="text"
name="lastname" /></td>
</tr>
<tr>
<td align="left" width="40%">Email</td>
<td align="center" width="10%">:</td>
<td align="left" width="50%"><input type="text" name="email"
/></td>
</tr>
<tr>
<td align="left" width="40%"><input type="submit"
value="SUBMIT" name="submit" /></td>
<td align="center" width="10%">&nbsp;</td>
<td align="left" width="50%"><a href="edit.php"
target="_blank">Edit</a></td>
</tr>
</table>
</form><br /><br />
<ul>
<li><a href="insert.php">Add New record</a></li>
<li><a href="edit.php">View record</a></li>
</ul>

</div>

</body>
</html>

2.insert_ac.php
<?php
$con = mysql_connect("localhost","root","") or die("could
not connect".mysql_error());
$db = mysql_select_db("sekar") or die("could not select
dn".mysql_error());

$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];

$sql = "insert into test_mysql(name,lastname,email)
values('$name','$lastname','$email')";
$result = mysql_query($sql);
if($result!=""){
echo "Record successfuly added"." "."<a
href='insert.php'>Back</a>";
}
else{
echo "failed";
echo "<br>";
}
?>
<ul>
<li><a href="insert.php">Add New record</a></li>
<li><a href="edit.php">View record</a></li>
</ul>
3.edit.php
<?php
$con = mysql_connect("localhost","root","") or die("could
not connect".mysql_error());
$db = mysql_select_db("sekar") or die("could not select
db".mysql_error());

$sql = "select * from test_mysql";
$result = mysql_query($sql);
echo "<table border='1' cellpadding='10' cellspacing='4'
width='400'>";
while($res = mysql_fetch_array($result)){
$id = $res['id'];
$name = $res['name'];
$lastname = $res['lastname'];
$email = $res['email'];
echo
"<tr><td>$id</td><td>$name</td><td>$lastname</td><td>$email</td><td><a
href='delete.php?id=$id'>Delete</a></td><td><a
href='update.php?id=$id'>Update</a></td></tr>";
}
echo "</table>";
echo "<br>";
?>
<ul>
<li><a href="insert.php">Add New record</a></li>
<li><a href="edit.php">View record</a></li>
</ul>
4.delete.php
<?php
$con = mysql_connect("localhost","root","") or die("could
not connect".mysql_error());
$db = mysql_select_db("sekar") or die("could not select
dn".mysql_error());

$id = $_GET['id'];

$sql = "delete from test_mysql where id='$id'";
$result = mysql_query($sql);
if($result!=""){
echo "Record successfuly delete"." "."<a
href='edit.php'>Back</a>";
}
else{
echo "failed";
echo "<br>";
}
?>
<ul>
<li><a href="insert.php">Add New record</a></li>
<li><a href="edit.php">View record</a></li>
</ul>
5.update.php
<?php
$con = mysql_connect("localhost","root","") or die("could
not connect".mysql_error());
$db = mysql_select_db("sekar") or die("could not select
dn".mysql_error());

$id = $_GET['id'];
$sql = "select * from test_mysql where id='$id'";
$result = mysql_query($sql);
echo "<form method='post' action='update_ac.php?id=$id'>";
echo "<table border='1' cellpadding='10' cellspacing='4'
width='400'>";
while($res = mysql_fetch_array($result)){
$id = $res['id'];
$name = $res['name'];
$lastname = $res['lastname'];
$email = $res['email'];

echo "<tr><td><input type='text' name='id'
value='$id'></td><td><input type='text' name='name'
value='$name'></td><td><input type='text' name='lastname'
value='$lastname'></td><td><input type='text' name='email'
value='$email'></td><td><input type='submit' name='submit'
value='submit'></td></tr>";
}
echo "</table>";
echo "</form>";
echo "<br>";
?>
<ul>
<li><a href="insert.php">Add New record</a></li>
<li><a href="edit.php">View record</a></li>
</ul>
6.update_ac.php
<?php
$con = mysql_connect("localhost","root","") or die("could
not connect".mysql_error());
$db = mysql_select_db("sekar") or die("could not select
dn".mysql_error());

$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];

$sql = "update test_mysql set id='$id', name='$name',
lastname='$lastname', email='$email' where id='$id'";
$result = mysql_query($sql);
if($result!=""){
echo "Record successfuly updated"." "."<a
href='edit.php'>Back</a>";
}
else{
echo "failed";
echo "<br>";
}
?>
<ul>
<li><a href="insert.php">Add New record</a></li>
<li><a href="edit.php">View record</a></li>
</ul>

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More PHP Interview Questions

How to create a table to store files?

0 Answers  


Does php support multithreading?

0 Answers  


Can you extend a final defined class?

0 Answers  


Tell me how to find the length of a string?

0 Answers  


Name some of the functions in php.

0 Answers  






What the difference between WAMP5 1.7.3 and WampServer 2?

3 Answers  


What is difference between core php and framework?

0 Answers  


How can we determine whether a variable is set?

0 Answers  


What is the use of trim function in php?

0 Answers  


What is php array function?

0 Answers  


Define urlencode() and urldecode() used in php?

0 Answers  


When should you use a stored procedure?

0 Answers  


Categories