PHP

http://govindasuccess.blogspot.in

Govind Marriage website : http://govindtanu.blogspot.com 


Below code for php developers how have not concerted database with my sql

Hear your can insert update delete and view your database.

Database name :- test_mysql
Table Name :- tbl_test


CREATE TABLE `test_mysql`.`tbl_test` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 50 ) NOT NULL ,
`lastname` VARCHAR( 50 ) NOT NULL ,
`email` VARCHAR( 65 ) NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;


CREATE TABLE IF NOT EXISTS `tbl_login` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;

--
-- Dumping data for table `tbl_login`
--

INSERT INTO `tbl_login` (`id`, `username`, `password`) VALUES
(30, 'hello', 'hello'),
(34, '', ''),
(27, 'new', 'new'),
(29, 'yu', 'yu'),
(31, '', ''),
(32, 'hello', 'hello'),
(33, 'govind', 'govind');



config.php


<?php

/* Database Connection */

$sDbHost = 'localhost';
$sDbName = 'test_mysql';
$sDbUser = 'root';
$sDbPwd = '';

$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());

?>

insert.php


<?php
function valid($name, $lastname,$email, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Insert Records</title>
</head>
<body>
<?php

if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>

<form action="" method="post">
<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Insert Records </font></b></td>
</tr>
<tr>
<td width="179"><b><font color='#663300'>Name<em>*</em></font></b></td>
<td><label>
<input type="text" name="name" value="<?php echo $name; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>LastName<em>*</em></font></b></td>
<td><label>
<input type="text" name="lastname" value="<?php echo $lastname; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Email<em>*</em></font></b></td>
<td><label>
<input type="text" name="email" value="<?php echo $email; ?>" />
</label></td>
</tr>

<tr align="Right">
<td colspan="2"><label>
<input type="submit" name="submit" value="Insert Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

include('config.php');

if (isset($_POST['submit']))
{

$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$lastname =mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));

if ($name == '' || $lastname == '' || $email == '')
{

$error = 'Please enter the details!';

valid($name, $lastname, $email,$error);
}
else
{

mysql_query("INSERT tbl_test SET name='$name', lastname='$lastname', email='$email'")
or die(mysql_error());

header("Location: view.php");
}
}
else
{
valid('','','','');
}
?>

view.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php

include('config.php');

$result = mysql_query("SELECT * FROM tbl_test")
or die(mysql_error());

echo "<table border='1' cellpadding='10'>";
echo "<tr>
<th><font color='Red'>Id</font></th>
<th><font color='Red'>Name</font></th>
<th><font color='Red'>lastname</font></th>
<th><font color='Red'>email</font></th>
<th><font color='Red'>Edit</font></th>
<th><font color='Red'>Delete</font></th>
</tr>";

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

echo "<tr>";
echo '<td><b><font color="#663300">' . $row['id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['name'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['lastname'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['email'] . '</font></b></td>';
echo '<td><b><font color="#663300"><a href="edit.php?id=' . $row['id'] . '">Edit</a></font></b></td>';
echo '<td><b><font color="#663300"><a href="delete.php?id=' . $row['id'] . '">Delete</a></font></b></td>';
echo "</tr>";

}

echo "</table>";
?>
<p><a href="insert.php">Insert new record</a></p>
</body>
</html>

edit.php

<?php
function valid($id, $name, $lastname,$email, $error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Edit Records</title>
</head>
<body>
<?php

if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>

<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>

<table border="1">
<tr>
<td colspan="2"><b><font color='Red'>Edit Records </font></b></td>
</tr>
<tr>
<td width="179"><b><font color='#663300'>Name<em>*</em></font></b></td>
<td><label>
<input type="text" name="name" value="<?php echo $name; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>LastName<em>*</em></font></b></td>
<td><label>
<input type="text" name="lastname" value="<?php echo $lastname; ?>" />
</label></td>
</tr>

<tr>
<td width="179"><b><font color='#663300'>Email<em>*</em></font></b></td>
<td><label>
<input type="text" name="email" value="<?php echo $email; ?>" />
</label></td>
</tr>

<tr align="Right">
<td colspan="2"><label>
<input type="submit" name="submit" value="Edit Records">
</label></td>
</tr>
</table>
</form>
</body>
</html>
<?php
}

include('config.php');

if (isset($_POST['submit']))
{

if (is_numeric($_POST['id']))
{

$id = $_POST['id'];
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$lastname = mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));


if ($name == '' || $lastname == '' || $email == '')
{

$error = 'ERROR: Please fill in all required fields!';


valid($id, $name, $lastname,$email, $error);
}
else
{

mysql_query("UPDATE tbl_test SET name='$name', lastname='$lastname' ,email='$email' WHERE id='$id'")
or die(mysql_error());

header("Location: view.php");
}
}
else
{

echo 'Error!';
}
}
else

{

if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM tbl_test WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);

if($row)
{

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

valid($id, $name, $lastname,$email,'');
}
else
{
echo "No results!";
}
}
else

{
echo 'Error!';
}
}
?>

delete.php

<?php
include('config.php');

if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];

$result = mysql_query("DELETE FROM tbl_test WHERE id=$id")
or die(mysql_error());

header("Location: view.php");
}
else

{
header("Location: view.php");
}
?>

login.php


<?php$err=mysql_error();$con=mysql_connect("localhost","root","");if (!$con)  {  die('Could not connect: ' . mysql_error());  }  mysql_select_db("govinddb", $con);?><?php if(isset($_POST['submit']) && $_POST['submit']=='submit') { $uname=$_POST['uname']; $pass=$_POST['pass']; $sel="select * from tbl_login where username='$uname' and password='$pass'"; $res=mysql_query($sel)or die(mysql_error()); if($fet=mysql_fetch_array($res)) { echo "Successfull Loging..."; } else { echo "Invalid User Name or Password..."; } }?><!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><form method="post" enctype="multipart/form-data"><table border="1" width="250" cellpadding="2"><tr> <td align="center" colspan="3">Login: </td> </tr> <tr> <td>Username:</td> <td><input type="text" name="uname" value="" /></td> </tr> <tr>  <td>Password:</td>    <td><input type="password" name="pass" value="" />    </td> </tr> <tr><td align="center" colspan="3"><input type="submit" name="submit" value="submit" /></td> </tr></table></form></body></html>


IF You like this link and code then please response me so that i will give you more codes....








No comments: