Infolinks

Breaking

Adsense

Thursday, April 28, 2016

VALIDATING AND SAVING NEW MEMBER USING PHP AND MYSQLI

Validating or checking if a certain person is already save in the database is important to avoid duplication of data. Let’s say you want to add a new member in the database and you don’t want to add a member that is already in the system. By avoiding duplication of data, you maintain integrity of your data and save memory in your disk since every character, even space consumes memory.

So I created this simple program to validate each entry and save it in the database. There should be no duplication based on the first name and last name of a member. I modified a previous project I worked on by just adding validation on the page.

PHP VALIDATE NEW MEMBER

<?php 

include('../dist/includes/dbcon.php');

$salut = $_POST['tsalut'];
$last = $_POST['tlast'];
$first = $_POST['tfirst'];
$pass = $_POST['tpass'];
$user = $_POST['tuser'];

$query=mysqli_query($con,"select * from member where t_first='$first' and t_last='$last'")or die(mysqli_error());
$count=mysqli_num_rows($query);

if ($count>0)
{
echo "<script type='text/javascript'>alert('Member already exist!');</script>";
echo "<script>document.location='../index.php'</script>";
}
?>

The above code will check whether the first name and last name entered is already in the database. The code $salut = $_POST['tsalut']; until $user = $_POST['tuser']; accesses the values from the input controls found in index.php and stores it the php variables.

The query checks for records in the member table with a first name and last name the same as what the user enters in the form. Using the mysqli_num_rows($query); function, it will count for the returned rows. If there is a match, then it will return value greater than 1 or more and will display a dialog box that says “Member already exists!”

PHP SAVING NEW MEMBER

If there is no match in the member table, it will return 0 and will execute the code below. The code below will move the image into the folder specified in the path and will insert/save the new record in the member table and will pop up a dialog box and will return to index.php.

else 
{
$name = $_FILES["timage"]["name"];
if ($name=="")
{
$name="default.gif";
}
else
{
$name = $_FILES["timage"]["name"];
$type = $_FILES["timage"]["type"];
$size = $_FILES["timage"]["size"];
$temp = $_FILES["timage"]["tmp_name"];
$error = $_FILES["timage"]["error"];

if ($error > 0){
die("Error uploading file! Code $error.");
}
else{
if($size > 100000000000) //conditions for the file
{
die("Format is not allowed or file size is too big!");
}
else
{
move_uploaded_file($temp, "../dist/img/".$name);
}
}
}
mysqli_query($con,"INSERT INTO member(t_last,t_first,t_pass,t_salut,t_pic,t_user) VALUES('$last','$first','$pass','$salut','$name','$user')")or die(mysqli_error());
echo "<script type='text/javascript'>alert('Successfully added new teacher!');</script>";
echo "<script>document.location='../index.php'</script>";

}

VALIDATING AND SAVING NEW MEMBER PHP AND MYSQLI

No comments:

Post a Comment

Adbox