PHP Registration Form-Part 5


Hi guys

Here is the continuation of the last part of the Registration form in php.More Importantly this is the fifth and last part of the tutorial okay will proceed to our tutorial, In the final part i discussed about the admin section for the registration form where you can able to view the datas available in the database.Here i’m going to explain About the edit and delete function of the data from the admin section.

In the last part the edit and delete links are created in the view table

echo “<td><a href=’edit.php?em=”.$row[‘Email’].”‘>” . edit . “</a></td>”;
echo “<td><a href=’delete.php?em=”.$row[‘Email’].”‘>” . Delete . “</a></td>”;

As you can see in the above code so we require two more php pages for that so create edit and delete php pages.Here i’m using mail field to select the record so mail field is must be filled.suppose if your doing without validation better to use id field. where em is simply a name or variable to get the mail record.

Now going to the edit.php Here we are going to modify the record so the first step is to display the data from the record and then values are modified finally it should be saved with the modified values.

<?php
//variables
$fname=$_POST[‘fname’];
$lname=$_POST[‘lname’];
$email=$_POST[’email’];
$password=$_POST[‘password’];
$contactno=$_POST[‘contactno’];
if (isset($_POST[‘fname’]))

{

mysql_connect(“localhost”,”root”,””)or die(‘Error:’.mysql_error());
mysql_select_db(“login”);
$query=”update user_details SET first_name = ‘”.$fname.”‘,last_name = ‘”.$lname.”‘,Email='”.$email.”‘,Password = ‘”.$password.”‘,Contact_no='”.$contactno.”‘ where Email='”.$email.”‘”;
mysql_query($query) or die(“Error updating database”);
echo “updated”;
}
?>
<?php
mysql_connect(“localhost”,”root”,””)or die(‘Error:’.mysql_error());
mysql_select_db(“login”);
$res=”select * from user_details where email='”.$_GET[’em’].”‘”;
$row1=mysql_query($res);
while($result = mysql_fetch_array($row1)){
$re= $result[‘first_name’] ;
$wr= $result[‘last_name’];
$mail= $result[‘Email’];
$pass= $result[‘Password’];

$cont= $result[‘Contact_no’];

}

?>

<html><head>
<script language=”JavaScript” type=”text/javascript”>
function checkform (form1)
{
var fname = form1.fname.value;
var email = form1.email.value;
var pass = form1.password.value;
var lname = form1.lname.value;
var contact = form1.contactno.value;
if (email == “”) {
alert( “Please enter your email address.” );
form.email.focus();
return false ;
}
if (pass == “”){
alert(“Please Enter Your Password.”);
return false;
}
if (fname == “”){
alert(“Please Enter Your First Name”);
return false;
}
if (lname == “”)
{
alert(“Please enter Your Last Name”);
return false;
}
if (contact == “”)
{
alert(“Please enter the contact no”);
return false;
}
return true ;
}
</script>
<title>Edit this Page</title>
</head>
<body>

<form name=”form1″ method=”post” action=”edit.php?em=namah” onsubmit=” return checkform (form1);”>
<div>
<table cellspacing=”5″ cellpadding=”3″ width=”400″>
<tr>
<td> First Name</td>
<td><input type=”text” name=”fname” value=”<?php echo $re;?>”/></td>
</tr>
<tr>
<td> Last Name</td>
<td><input type=”text” name=”lname” value=”<?php echo $wr; ?>” /></td>
</tr>
<tr>
<td> EMAIL</td>
<td><input type=”text” name=”email” value=”<?php echo $mail; ?>” /></td>
</tr>
<tr>
<td>Password</td>
<td><input type=”password” name=”password” value=”<?php echo $pass;?>”  /></td>
</tr>
<tr>
<td> Contact No</td>
<td><input type=”text” name=”contactno” maxlength=”11″ size=”20″ value=”<?php echo $cont;?>”  /></td>
</tr>
<tr>
<td></td>
<td><input type=”Submit” name=”Ok” value=”okay” /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

You can see the above code  where we have to use select query first  and then use the update query. The lower section of the code contains full html where we used in the html page so thats pretty easy to do that. After that we are using variables for query Syntax for update query is

update table name  set  field= values;

Here in the select query we used to get the particular field by using where clauseyou can see in the below code

$res=”select * from user_details where email='”.$_GET[’em’].”‘”; where em is the name passed in the href tag in the view.php

Now i’m getting the modified values of field from the above select query so that  im getting that values in variables by the while loop thats it now i’m going to use that variable declared in the input field values between the php delimiters.

so that the values will be present when you click the edit link in the view.php after that you modify values and click submit it executes the update query so now the record will be updated with the modified values

DELETE:

Now i’m going to explain about the delete link in view.php where it also using to get the record by the mail field from the database.

<?php
mysql_connect(“localhost”,”root”,””)or die(‘Error:’.mysql_error());
mysql_select_db(“login”);
$result=”DELETE FROM user_details WHERE email='”.$_GET[’em’].”‘”;
$row1=mysql_query($result);
echo “successfully the record deleted”;
?>

Delete query Syntax is: Delete from table name where field=value;

Finally an echo statement will be executed to indicate the record is deleted .you can use the connection statement to the database in a separate file and include that in every file so that will reduce your time by coding those two lines in every file.It will be very useful when you are doing big projects in php.

Syntax : include “filename”;

you can check that in view.php so that the particular record will be deleted from the database thats it guys you created a registration page now.Will See you in Next PHP Tutorial.

Advertisements
About

Working For Cause Not For Applause

Posted in PHP, web

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: