Monday, March 10, 2008

Insert Data into MySQL from PHP

dbCon.html


<html>
<head>
<title>Student Registration</title>
</head>

<body>
<div align="center">
<h2><font color="#000099" face="Verdana, Arial, Helvetica,
sans-serif">Student Registration</font></h2>
<form name="form1" method="get" action="dbCon.php">
<p>&nbsp;</p>
<table width="52%" height="200" border="1"
align="center" cellpadding="5" cellspacing="5"
bordercolor="#000000">
<tr>
<td width="31%"><font color="#000099">Roll</font></td>
<td width="69%"><input name="roll" type="text"
size="20"></td>
</tr>
<tr>
<td><font color="#000099">Name</font></td>
<td><input name="nm" type="text" size="50"></td>
</tr>
<tr>
<td><font color="#000099">Address</font></td>
<td><textarea name="address" cols="30"></textarea></td>
</tr>
<tr>
<td><font color="#000099">Batch</font></td>
<td><input name="batch" type="text" size="20"></td>
</tr>
<tr bordercolor="#000000">
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Submit">
<input name="Reset" type="submit" id="Reset"
value="Reset">
</div></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</div>
</body>
</html>



dbCon.php


<?php
$server='localhost';
$user='root';
$pass='1234';
$dbName='Student_Admin';
$con = mysql_connect("$server", "$user", "$pass");

echo "<h2 align=center>
PHP-MySQL Connectivity<br></h2><hr>";

if($con)
{
print "Connected successfully<br>";
}
else
{
die("Unable to connect");
}

$selectdb= mysql_select_db("$dbName")
or die("Could not select database");

$roll=$_GET['roll'];
$nm=$_GET['nm'];
$address=$_GET['address'];
$batch=$_GET['batch'];

$sql_insert="insert into Student values('$roll','$nm','$address','$batch');";

$result=mysql_query($sql_insert);

if($result)
{
echo "The record is inserted";
}
else
{
echo "unable to insert record";
mysql_error();
}

echo "<br><hr><h3 align=center><br>Display Data from Database <br></h3>";

$sqlquery= ("select * from Student;");
$result=mysql_query($sqlquery);

if(mysql_num_rows($result)<1)
{
echo "No result";
}
else
{
echo "<br><table align=center width=70% border=1 bordercolor=#0000FF
cellpadding=5 cellspacing=5>";
echo "<th>Roll</th><th>Name</th><th>Address</th><th>Batch</th>";
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[Roll]</td>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Address]</td>";
echo "<td>$row[Batch]</td>";
echo "</tr>";
}
echo "</table>";
}

mysql_close($con);
?>




1 comments:

DataDiary.com said...

it's really helpful for me and other like me thanks for it...