Hello All,
I am a newbie to PHP and I'm working on a marathon sign up form for my final project and could really use the help. The trouble i'm having is sending and retrieving database from the database. Below is the code i am using. Can someone please review and tell me where I went wrong.
(This is the code that is sitting at the top of my document)
<?php
session_start();
require_once('inc/dbuser.php');
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['submit_registration'])) {
$username = test_input($_POST['username']);
$password = test_input($_POST['password']);
$cpassword = test_input($_POST['cpassword']);
$fname = test_input($_POST['fname']);
$lname = test_input($_POST['lname']);
$email = test_input($_POST['email']);
$phone = test_input($_POST['phone']);
$address = test_input($_POST['address']);
$city = test_input($_POST['city']);
$state = test_input($_POST['state']);
$zip = test_input($_POST['zip']);
$distance = test_input($_POST['distance']);
if (!empty($username) || !empty($password) || !empty($fname) || !empty($lname)|| !empty($email) || !empty($phone)|| !empty($address)|| !empty($city)|| !empty($state)|| !empty($zip)|| !empty($distance)) {
}
else{
$sql = "SELECT email From runners Where email = ? Limit 1";
//Prepare statement
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: ../signup?error=sqlerror");
exit();
}
else{
mysqli_stmt_bind_param($stmt,'s', $email);
mysqli_stmt_execute($stmt);
$stmt->bind_result($email);
$stmt->store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if ($resultsCheck > 0) {
header ("Location: ../signup.php?error=usertaken&email=".$email);
exit();
}
else {
$sql = "INSERT Into runners (username, password, fname, lname, email, phone, address, city, state, zip, distance) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
header ("Location: ../signup.php?error=sqlerror");
exit();
}
else{
$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt,"ssssssssssss", $username, $hashedPwd, $fname, $lname, $email, $phone, $address,$city, $state, $zip, $distance);
mysqli_stmt_execute($stmt);
header ("Location: ../signup.php?signup=success");
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_stmt_close($conn);
}
?>
(This is my form [submit_registration] )
<div class="contact">
<?php
if(isset($_GET['error'])) {
if($_GET['error']=="emptyfields"){
echo '<p>Fill in all fields!</p>';
}
elseif($_GET['error']== "invalidemail") {
echo '<p>Provide a valid email!</p>';
}
elseif($_GET['error']== "usertaken") {
echo '<p>Username is already taken!</p>';
}
elseif($_GET['error']== "passwordCheck") {
echo '<p>Your passwords do not match!</p>';
}
elseif($_GET['signup']== "success") {
echo '<p>Signup Successful!</p>';
}
}
?>
<form name="form1" action="signup.php" method="post">
<p><label>Create a username:<br></label>
<input type="text" name="username" placeholder="username" autofocus required><br>
<span class="err" id="usrErr"></span>
</p>
<p><label>Create a Password:<br></label>
<input type="password" name="password" placeholder="password" autofocus required><br>
<span class="err" id="pwdErr"></span>
</p>
<p><label>Confirm Password:<br></label>
<input type="password" name="cpassword" placeholder="confirm password" autofocus required><br>
<span class="err" id="cpwdErr"></span>
</p>
<p><label>First Name:<br></label>
<input type="text" name="fname" placeholder="first name" autofocus required><br>
<span class="err" id="fnErr"></span>
</p>
<p><label>Last Name:<br></label>
<input type="text" name="lname" placeholder="last name" autofocus required><br>
<span class="err" id="lnErr"></span>
</p>
<p><label>Email:<br></label>
<input type="text" name="email" placeholder="
[email protected]" required><br>
<span class="err" id="emErr"></span>
</p>
<p><label>Phone: <br></label>
<input type="tel" name="phone1" placeholder="XXX-XXX-XXXX" maxlength="13" required>
<span class="err" id="phErr"></span>
</p>
<p><label>Address: <br></label>
<input type="text" name="address" placeholder="415 W. 15th Street" required>
<span class="err" id="addErr"></span>
</p>
<p><label>City: <br></label>
<input type="text" name="city" placeholder="Chicago" required>
<span class="err" id="cityErr"></span>
</p>
<p><label>State:<br></label>
<input type="text" name="state" placeholder="IL" maxlength="2" required>
<span class="err" id="stateErr"></span>
</p>
<p><label>Zip: <br></label>
<input type="text" name="zip" placeholder="60621" maxlength="5" required>
<span class="err" id="zipErr"></span>
</p>
<p>I am interested in:<br>
<span id="inErr" class="err"></span>
<select id="interest" class="adj">
<option disabled>--5k--</option>
<option> 1 Mile Run</option>
<option> 5K Run</option>
<option>10K run</option>
</select>
</p>
<button type="submit" name="submit_registration"onclick="validForm()">Signup</button>
</p>
</form>