Monday, 18 February 2013

How to update/edit a data in database using Mysql

Step 1:  Make a file named edit.php

<?php
include("include/connection.php");
$id=$_GET['id'];
?>

Step 2:  Make a form according to insertion.

Step 3:  Above the form add a selection query using where condition. for e.g.:

$query="select * from register where id=". $id;

Step 4:  $result=mysql_query($query);

while($row=mysql_fetch_array($result))

{

$name=$row['name'];

$email=$row['email'];

}

if(isset($_POST['submit'])=='update')

{

$name=$_POST['name'];

$email=$_POST['email'];

$query_update="update register set name='$name',email='$email' where id=". $id;

Step 5:  Run the query 

$result_update=mysql_query($query_update);
if($result_update=='1')
{
header("location:selection.php");
}
else
{
echo"data not updated. Please try again";
}
}
?>
<html>
<head>
</head>
<body>
<form name="edit" action="" method="post" onsubmit="return validate()">
<table border="0" width="50%" align="center">
<tr>
<td>Name:</td><td><input type="text" name="name" id="name"value="<?php echo $name; ?>" size="20"></td>
</tr>
<tr>
<td>Email:</td><td><input type="text" name="email" id="email" value="<?php echo $email; ?>" size="20"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="update"></td>
</tr>
            </table>
</form>
</body>
</html>
Like this we can update the name and email of the person.

Sunday, 17 February 2013

How to insert data permanently in DB using Mysql

Step 1:   Create a HTML form using .php extension. for e.g.

<html>
<body>
<form name="Registration" action="" method="post">
<table border="1" width="50%" cell spacing="0" cell padding="0">
<tr>
<td>
<table>
<tr>
<td><h1 align="center">REGISTRATION</h1></td>
</tr>
<tr>
<td>NAME:</td>
<td><input type="text" name="name" size="20" id="name"></td>
</tr>
<tr>
<td>USERNAME:</td>
<td><input type="text" name="uname" size="20" id="uname"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="submit" ></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>

Step 2:  Create a database in PHPmyadmin.

Step 3:   Create a table in database.

Step 4:   Make a connection file in common file. for e.g.:
<?php
$conn=mysql_connect("localhost","root","");
if(!$conn)
{
die('couldnot connect:'.mysql_error());
}
mysql_select_db("online_property",$conn);
?>

Step 5:  

1.  Call a connection file on the top.

<?php include ("include/connection.php");

2.  if(isset($_POST['submit'])=='submit')

{

$name=$_POST['name'];

$uname=$_POST['uname'];

echo "You are".$name."".$uname."<br/>";

}

3. Add the insertion query for e.g.:

$query="insert into register(name,uname)value('$name','$uname')";

4. Run the query for e.g.: 

$result=mysql_query($query);

5. Then check the result if result is one then output is successfully register otherwise output is failed.

if($result=='1')

{

echo"Successfully Register";

}

else

{

echo"Registration failed. please try again";

}


?>