Saturday, August 29, 2015

sample tbluser in mysql


--
-- Table structure for table `tbluser`
--

CREATE TABLE IF NOT EXISTS `tbluser` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;




--
-- Dumping data for table `tbluser`
--

INSERT INTO `tbluser` (`id`, `username`, `email`, `password`) VALUES
(1, 'rath', 'ratha@yahoo.com', '123'),
(2, 'Monorom', 'monorom123@yahoo.com', '12345'),
(3, 'rathkeo', 'ratha@yahoo.com', '123'),
(7, 'sok', 'ratha@yahoo.com', '123'),
(15, 'soksophy', 'soksophy@gmail.com', '1234578'),
(18, 'der', 'ratha@yahoo.com', '202cb962ac59075b964b07152d234b70'),
(19, 'sdklfj', 'jksder', '08ab302adaa100bb3e3945cafe0ae6af'),
(20, 'wsaef', '', 'd41d8cd98f00b204e9800998ecf8427e'),
(21, '', 'ratha@yahoo.com', '202cb962ac59075b964b07152d234b70'),
(22, '', 'ratha@yahoo.com', '202cb962ac59075b964b07152d234b70'),
(23, 'sdf', '', 'd41d8cd98f00b204e9800998ecf8427e'),
(24, 'wse', 'wef', 'bb7946e7d85c81a9e69fee1cea4a087c'),
(25, 'keolakhean', 'lsadjfoiiefioie', '2ebd1fc50f90c2a695d4ed6869c95d7f'),
(26, 'wserf', 'wer', '057a538a0de0b5f8481bffa5321fe52f'),
(27, '', '', ''),
(28, 'qwwe', '', '2134'),
(29, '?', '?', '?'),
(30, '?', '?', '?'),
(31, 'ds', '', ''),
(32, 'sd', '', ''),
(33, 'jhgj', '', ''),
(34, 'asdfesd', '', ''),
(35, 'ws', '', ''),
(36, 'gh', '', ''),
(37, 's', '', ''),
(38, 'weew', '', ''),
(39, '', 'sdww', '');

1. create file config.php
<?php
//Step1
 $db = mysql_connect("localhost","root","");
 if (!$db) {
 die("Database connection failed miserably: " . mysql_error());
 }
//Step2
 $db_select = mysql_select_db("dbex1",$db);
 if (!$db_select) {
 die("Database selection also failed miserably: " . mysql_error());
 }
?>

select.php
<?php
include_once"config.php";
$query="select * from tbluser";
$queryID=$_GET['varID'];
$sql="delete from tbluser where id='$queryID'";
mysql_query($sql) or die("cannot delete "+ mysql_error());

$result=mysql_query($query,$db);
echo "<table border=1><tr><th>user ID<th>username<th>email<th>password</tr>";
while ($row=mysql_fetch_array($result)) {
$id= $row['id'];
$user=$row['username'];
$email=$row['email'];
$psw=$row['password'];

echo "<tr><td>$id
<td>$user
<td>$email
<td>$psw<td>";
?>
<a href=edit.php?varID=<?php echo $id ?>>Edit</a><td>
<a href='#' onClick="confirmDelete('<?php echo $id ?>')">Delete</a>
</tr>
<?php


}
echo "</table>";
mysql_close($db);
?>
<html>
<head>
<script language="javascript">
function confirmDelete(ID){
var answer=confirm("Do you want to delete this record?");
if(answer)
window.location="select.php?ID="+ID;
else
alert("Record was not deleted");

}

</script>

</head>
<body>

</body>
</html>

edit.php
<?php
include_once"config.php";
$ID=$_GET['varID'];
$query="select * from tbluser where id=$ID";
$result=mysql_query($query,$db);
while($row=mysql_fetch_array($result)){
$id= $row['id'];
$user=$row['username'];
$email=$row['email'];
$psw=$row['password'];
}

mysql_close($db);
echo "Hi ".$user;
?>
<html>
<body>
<?php
if(isset($_POST['update']))
{
include('config.php');
$id1=$_POST['txtid'];
$user1=$_POST['txtname'];
$email1=$_POST['txtmail'];
$pws1=$_POST['txtpsw'];
$sql = "UPDATE tbluser ".
       "SET username = '$user1',email='$email1',password='$pws1' ".
       "WHERE id = $id1" ;

mysql_select_db('dbex1');
$retval = mysql_query($sql,$db);
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n<a href='select.php'>view list</a>";
mysql_close($db);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table>
<tr><td>id<td><input type="text" name="txtid" value="<?php echo $id;?>"></tr>
<tr><td>username<td><input type="text" name="txtname" value="<?php echo $user;?>"></tr>
<tr><td>email<td><input type="text" name="txtmail"  value="<?php echo $email;?>"></tr>
<tr><td>Password<td><input type="text" name="txtpsw" value="<?php echo $psw; ?>"></tr>
<tr><td><input type="submit" name="update" value="Edit">

</table>
</form>
<?php
}
?>
</body>
</html>

How to insert data into table database insert.php
<?php
if(isset($_POST['add']))
{
include('config.php');
$user=$_POST['txtname'];
$email=$_POST['txtmail'];
$pws=$_POST['txtpsw'];
$sql = "INSERT INTO tbluser(username,email,password) VALUES('$user','$email','$pws')";
mysql_select_db('dbex1');
$retval = mysql_query($sql,$db);
if(! $retval )
{
  die('Could not add data: ' . mysql_error());
}
echo "add data successfully\n<a href='select.php'>view list</a>";
mysql_close($db);
}
else
{
?>
<html>
<body>
<form method="post" action="<?php $_PHP_SELF ?>">
<table>
<tr><td>username<td><input type="text" name="txtname"></tr>
<tr><td>email<td><input type="text" name="txtmail"></tr>
<tr><td>Password<td><input type="password" name="txtpsw"></tr>
<tr><td><input type="submit" name="add" value="Add User">

</table>
</form>
<?php
}
?>
</body>
</html>


No comments:

Post a Comment