Greetings,
This post is about adding / displaying / deleting records from MySQL using PHP scripting.
It doesn't use client side scripting language.
Database:
Create a database in the name of 'sample'.
Use the following code to create table:
PHP file: index.php
Configure your database username and password accordingly. It displays from latest insertion. 6 records can be viewed as such and the rest can be viewed using the scroll bar. For easy understanding i have not used any css.
This works fine in wamp server. Hope this helps..
This post is about adding / displaying / deleting records from MySQL using PHP scripting.
It doesn't use client side scripting language.
Database:
Create a database in the name of 'sample'.
Use the following code to create table:
CREATE TABLE IF NOT EXISTS `sample_data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`emailid` varchar(50) NOT NULL,
`address` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
PHP file: index.php
Configure your database username and password accordingly. It displays from latest insertion. 6 records can be viewed as such and the rest can be viewed using the scroll bar. For easy understanding i have not used any css.
<html>
<head>
<title>Add and Delete DB record from single page</title>
</head>
<body>
<?php
$dbcon = mysql_connect('localhost','root','admin') or die('Could not connect to database');
mysql_select_db('sample',$dbcon);
if(isset($_GET['q'])){
$delquery = "DELETE FROM sample_data WHERE id=".$_GET['q'];
mysql_query($delquery) or die('Cannot be deleted');
echo "Record Id: ".$_GET['q']." is deleted";
}
if(isset($_POST['save'])){
$value1 = $_POST['txtname'];
$value2 = $_POST['txtmail'];
$value3 = $_POST['txtaddress'];
$insertquery = "INSERT INTO sample_data(username,emailid,address) VALUES('". $value1 ."','". $value2 ."', '". $value3 ."')";
mysql_query($insertquery) or die('Incorrect Query');
}
echo "<h4> <u>Data Display </u></h4>";
$query = "SELECT * FROM sample_data ORDER BY id DESC";
$result = mysql_query($query) or die('Error in query');
$count = mysql_num_rows($result);
if($count==0){
echo "No Data Set";
}
else {
echo "<table>";
echo "<tr>";
echo "<th width='20' align='left'>Id</th>";
echo "<th width='150' align='left'>Name</th>";
echo "<th width='150' align='left'>Email</th>";
echo "<th width= '250' align='left'>Address</th>";
echo "<th width='50' align='left'>Delete</th>";
echo "</tr> </table><div style = 'height:150px; width:700px; overflow:auto'><table>";
}
while($record = mysql_fetch_array($result)){
echo "<tr>";
echo "<td width='20'>". $record[0]. "</td>";
echo "<td width='150'>". $record[1]. "</td>";
echo "<td width='150'>". $record[2]. "</td>";
echo "<td width='250'>". $record[3]. "</td>";
echo "<td width='50'><a href='index.php?q=".$record[0]."'>Click</a></td>";
echo "</tr>";
}
echo "</table></div>";
?>
<br><br><br>
<h4><u>Add Data</u></h4>
<form name="form1" method="post" action="index.php">
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<th width="123" scope="row">Name</th>
<td width="271"><input name="txtname" type="text" id="txtname" size="50"></td>
</tr>
<tr>
<th scope="row">Email</th>
<td><input name="txtmail" type="text" id="txtmail" size="50"></td>
</tr>
<tr>
<th scope="row">Address</th>
<td><textarea name="txtaddress" cols="40" rows="3" id="txtaddress"></textarea></td>
</tr>
<tr>
<th scope="row"> </th>
<td> </td>
</tr>
<tr>
<th scope="row"> </th>
<td><input type="submit" name="save" value="Save">
<input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
This works fine in wamp server. Hope this helps..
Comments
Post a Comment