Emin Eryilmaz Emin Eryilmaz - 4 years ago 191
PHP Question

Registering system takes wrong infos from wrong locations

I have this code.

PHP

<?php
ob_start();
session_start();
if( isset($_SESSION['user'])!="" ){
header("Location: home.php");
}
include_once 'db.php';

$error = false;

if ( isset($_POST['btn-signup']) ) {

// clean user inputs to prevent sql injections
$name = trim($_POST['name']);
$name = strip_tags($name);
$name = htmlspecialchars($name);

$email = trim($_POST['email']);
$email = strip_tags($email);
$email = htmlspecialchars($email);

$pass = trim($_POST['pass']);
$pass = strip_tags($pass);
$pass = htmlspecialchars($pass);

$Nume = trim($_POST['Nume']);
$Nume = strip_tags($Nume);
$Nume = htmlspecialchars($Nume);

$Prenume = trim($_POST['Prenume']);
$Prenume = strip_tags($Prenume);
$Prenume = htmlspecialchars($Prenume);

$NumePrenume = trim($_POST['NumePrenume']);
$NumePrenume = strip_tags($NumePrenume);
$NumePrenume = htmlspecialchars($NumePrenume);

$CNP = trim($_POST['CNP']);
$CNP = strip_tags($CNP);
$CNP = htmlspecialchars($CNP);

$NumarTelefon = trim($_POST['NumarTelefon']);
$NumarTelefon = strip_tags($NumarTelefon);
$NumarTelefon = htmlspecialchars($NumarTelefon);

$ContBancar = trim($_POST['ContBancar']);
$ContBancar = strip_tags($ContBancar);
$ContBancar = htmlspecialchars($ContBancar);

$Poza = trim($_POST['Poza']);
$Poza = strip_tags($Poza);
$Poza = htmlspecialchars($Poza);

$Locatie = trim($_POST['Locatie']);
$Locatie = strip_tags($Locatie);
$Locatie = htmlspecialchars($Locatie);

$NumarPunctaj = trim($_POST['NumarPunctaj']);
$NumarPunctaj = strip_tags($NumarPunctaj);
$NumarPunctaj = htmlspecialchars($NumarPunctaj);

$Referal = trim($_POST['Referal']);
$Referal = strip_tags($Referal);
$Referal = htmlspecialchars($Referal);

$Varsta = trim($_POST['Varsta']);
$Varsta = strip_tags($Varsta);
$Varsta = htmlspecialchars($Varsta);

$IP = trim($_POST['IP']);
$IP = strip_tags($IP);
$IP = htmlspecialchars($IP);

$Cont = trim($_POST['Cont']);
$Cont = strip_tags($Cont);
$Cont = htmlspecialchars($Cont);


// basic name validation
if (empty($name)) {
$error = true;
$nameError = "Please enter your full name.";
} else if (strlen($name) < 3) {
$error = true;
$nameError = "Name must have atleat 3 characters.";
} else if (!preg_match("/^[a-zA-Z ]+$/",$name)) {
$error = true;
$nameError = "Name must contain alphabets and space.";
}

//basic email validation
if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$error = true;
$emailError = "Please enter valid email address.";
} else {
// check email exist or not
$query = "SELECT userEmail FROM users WHERE userEmail='$email'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count!=0){
$error = true;
$emailError = "Provided Email is already in use.";
}
}


// password validation
if (empty($pass)){
$error = true;
$passError = "Please enter password.";
} else if(strlen($pass) < 6) {
$error = true;
$passError = "Password must have atleast 6 characters.";
}

// password encrypt using SHA256();
$password = hash('sha256', $pass);

// if there's no error, continue to signup
if( !$error ) {

$query = "INSERT INTO users(userName,userEmail,userPass, Nume, Prenume, NumePrenume, CNP, NumarTelefon, ContBancar, Poza, Locatie, NumarPunctaj, Referal, IP, Cont) VALUES('$name','$email','$password','$Nume','$Prenume','$NumePrenume','$CNP','$NumarTelefon','$ContBancar','$Poza','$Locatie','$NumarPunctaj','$Referal','$IP','$Cont')";
$res = mysql_query($query);

if ($res) {
$errTyp = "success";
$errMSG = "Successfully registered, you may login now";
unset($name);
unset($email);
unset($pass);
unset($Nume);
unset($Prenume);
unset($NumePrenume);
unset($CNP);
unset($NumarTelefon);
unset($ContBancar);
unset($Poza);
unset($Locatie);
unset($NumarPunctaj);
unset($Referal);
unset($Varsta);
unset($IP);
unset($Cont);
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}

}


}
?>


HTML

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Nickname" maxlength="50" value="<?php echo $name ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Nume" maxlength="50" value="<?php echo $Nume ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Prenume" maxlength="50" value="<?php echo $Prenume ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="NumePrenume" maxlength="50" value="<?php echo $NumePrenume ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="CNP" maxlength="50" value="<?php echo $CNP ?>" />
</div>
<span class="text-danger"><?php echo $CNPERROR; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="NumarTelefon" maxlength="50" value="<?php echo $NumarTelefon ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="ContBancar" maxlength="50" value="<?php echo $ContBancar ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Poza" maxlength="50" value="<?php echo $Poza ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Locatie" maxlength="50" value="<?php echo $Locatie ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="NumarPunctaj" maxlength="50" value="<?php echo $NumarPunctaj ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Referal" maxlength="50" value="<?php echo $Referal ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Varsta" maxlength="50" value="<?php echo $Varsta ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="IP" maxlength="50" value="<?php echo $IP ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="name" class="form-control" placeholder="Cont" maxlength="50" value="<?php echo $Cont ?>" />
</div>
<span class="text-danger"><?php echo $nameError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input type="email" name="email" class="form-control" placeholder="Enter Your Email" maxlength="40" value="<?php echo $email ?>" />
</div>
<span class="text-danger"><?php echo $emailError; ?></span>
</div>

<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
<input type="password" name="pass" class="form-control" placeholder="Enter Password" maxlength="15" />
</div>
<span class="text-danger"><?php echo $passError; ?></span>
</div>


But, when I register the new account, this happens:

1) "Cont" section gets registered into Nickname section.
2) Sections after Nume won't get updated into the database. Sections shows blank.

I don't know why. I tried to follow some tutorials given by Fred ii and I can't solve this error.

Answer Source

All of your inputs have the same name:

 name="name"

Each input should have a unique name which can then be identified in the $_POST array.

You have another problem, the following syntax is not correct:

if( isset($_SESSION['user'])!="" ){

Tests on this must be performed separately:

if( isset($_SESSION['user']) && $_SESSION['user'] != "" ){

Reading from left to right, "if session user is set and session user is not blank, then perform the following".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download