A
Anonymous
Guest
Hello. Have tested this code without success:
After submitting the form, only the data from the header page shows up. Header.inc contains ->
But when I remove this "check if user exists"-code, the data from the form goes in to the database. What am I missing here?
Code:
<!doctype html>
<html>
<head>
<?php include 'inc/css-links.php'; ?>
<meta charset="utf-8">
<title><?php include 'inc/title.php'; ?></title>
</head>
<?php
include 'inc/header.php';
include 'inc/mysql-connection.php';
if(isset($_POST['create'])) {
$username = mysql_real_escape_string($_POST['username']);
//check if user exists
$query = "SELECT FROM users WHERE username='$username'";
$result = mysql_query ($query);
$num_rows = mysql_num_rows ($result);
if ($num_rows > 0) { // i.e if matches were found
echo "User details already exist!";
} else {
//check if user exists end
$p = $_POST['password'];
$hashed_p = password_hash($p, PASSWORD_DEFAULT);
//code for image uploading
if($_FILES['ProfileImg']['name']){
move_uploaded_file($_FILES['ProfileImg']['tmp_name'], "image/".$_FILES['ProfileImg']['name']);
$img="image/".$_FILES['ProfileImg']['name'];
}
$sql = "INSERT INTO users (username, password, firstname, lastname, email, telephone, image)
VALUES ('".$_POST["username"]."','".$hashed_p."','".$_POST["firstname"]."','".$_POST["lastname"]."','".$_POST["email"]."','".$_POST["telephone"]."','".$img."')";
if ($connect->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>
alert('New record created successfully');
</script>";
}
else
{
echo "<script type= 'text/javascript'>
alert('Error: " . $sql . "<br>" . $connect->error."');
</script>";
}
}
}
?>
<center>
<table width="1300" border="1">
<tbody>
<tr valign="top">
<td width="200" rowspan="2" class="left-td"><?php include 'inc/left_td.php'; ?></td>
<td width="900" class="profile-usernamefield">
<?php
include 'inc/profile-usernamefield.php';
?>
</td>
<td width="200" rowspan="2" class="right-td">
<?php
include 'inc/login.php';
?>
</td>
</tr>
<tr>
<td class="main-td">
<form action="register.php" method="post" enctype="multipart/form-data">
<label for="username" class="form__field">Username:</label><br>
<input type="text" name="username" required class="form__field"><br><br>
<label for="password" class="form__field">Password:</label><br>
<input type="password" name="password" required class="form__field" id="pwd"><br><br>
<label for="password-repeat" class="form__field">Password repeat:</label><br>
<input type="password" name="password" required class="form__field" id="pwd-r"><br><br>
<label for="firstname" class="form__field">First name:</label><br>
<input type="text" name="firstname" required class="form__field"><br><br>
<label for="lastname"class="form__field">Lastname:</label><br>
<input type="text" name="lastname" required class="form__field"><br><br>
<label for="email" class="form__field">Email:</label><br>
<input type="email" name="email" required class="form__field"><br><br>
<label for="telephone" class="form__field">Telephone:</label><br>
<input type="text" name="telephone" required class="form__field"><br><br>
<label for="image" class="form__field">Image:</label><br>
<input type="file" name="ProfileImg" required><br><br>
<input type="submit" name="create" value="Create account" onclick="return Validate()"><br><br>
</form>
</td>
</tr>
</tbody>
</table>
</center>
<script type="text/javascript">
function Validate() {
var password = document.getElementById("pwd").value;
var confirmPassword = document.getElementById("pwd-r").value;
if (password != confirmPassword) {
alert("Passwords do not match.");
return false;
}
return true;
}
</script>
<center>
<?php
include 'inc/footer.php';
After submitting the form, only the data from the header page shows up. Header.inc contains ->
Code:
<body>
<br>
<center>
<table width="1290" class="header">
<tbody>
<tr>
<td valign="top"><a href="index.php"><img src="img/IMG_1081.jpg" border="0" width="1290" height="380"></a></td>
</tr>
</tbody>
</table>
</center><br>