I've tried creating a basic registration form (I'm pretty new to PHP). The form works sometimes, but most of the times it's just sending blank entries into the MySQL database. Below is the code:
I have the following form:
<form action="#" method="post">
<h2 class="sub-heading-agileits">Participant 1</h2>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text1">
<input type="text" name="name1" placeholder="Full Name" required="">
</div>
<div class="field-agileinfo-spc form-w3-agile-text1">
<select class="form-control" name="year1">
<option>Year</option>
<option value="1st Year">1st Year</option>
<option value="2nd Year">2nd Year</option>
<option value="3rd Year">3rd Year</option>
</select>
</div>
</div>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="phone1" placeholder="Phone Number" required="">
</div>
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="college1" placeholder="College" required="">
</div>
</div>
<div class="field-agileinfo-spc form-w3-agile-text">
<input type="email" name="email1" placeholder="Email" required="">
</div>
<h2 class="sub-heading-agileits">Participant 2</h2>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text1">
<input type="text" name="name2" placeholder="Full Name">
</div>
<div class="field-agileinfo-spc form-w3-agile-text1">
<select class="form-control" name="year2">
<option>Year</option>
<option value="1st Year">1st Year</option>
<option value="2nd Year">2nd Year</option>
<option value="3rd Year">3rd Year</option>
</select>
</div>
</div>
<div class="main-flex-w3ls-sectns">
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="phone2" placeholder="Phone Number">
</div>
<div class="field-agileinfo-spc form-w3-agile-text2">
<input type="text" name="college2" placeholder="College">
</div>
</div>
<div class="field-agileinfo-spc form-w3-agile-text">
<input type="email" name="email2" placeholder="Email">
</div>
<div class="clear"></div>
<input type="submit" value="Submit">
<input type="reset" value="Clear Form">
<div class="clear"></div>
</form>
I'm sorry for the long form code.
This is the PHP code to post the data to the database:
$servername = "localhost";
$username = "fic";
$password = "fic201718";
$dbname = "fic201718";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name1 = $_POST['name1'];
$year1 = $_POST['year1'];
$phone1 = $_POST['phone1'];
$college1 = $_POST['college1'];
$email1 = $_POST['email1'];
$name2 = $_POST['name2'];
$year2 = $_POST['year2'];
$phone2 = $_POST['phone2'];
$college2 = $_POST['college2'];
$email2 = $_POST['email2'];
$sql = "INSERT INTO identitytheft (Participant1Name,Participant1Year,Participant1Phone,Participant1College,Participant1eMail,Participant2Name,Participant2Year,Participant2Phone,Participant2College,Participant2eMail) VALUES ('$name1','$year1','$phone1','$college1','$email1','$name2','$year2','$phone2','$college2','$email2')";
$conn->query($sql);
if (!empty($_POST['name1'])) {
echo ("<script type=\"text/javascript\"> alert('Successfully Registered'); </script>");
}
However, the form sometimes inserts absolutely blank data into the database. It sometimes works though.
One thing that I have noticed is, I do not get blank rows if there are no special characters in the responses. My columns are set to utf8_unicode_ci (all of the columns). Could there be something wrong here? Please help?