Tried searching the other threads but found nothing that helps:
The code below is part of a form, this passes the data from the form to the DB, the connection has been tested and works.
When the submit button is pressed, nothing happens, no echo for success or failure, and no new record in the database. I can't seem to find what the issue is:
<?php
if(isset($_POST['submit'])){
require '/connectDB.php';
try {
$stmt = $db->prepare("INSERT INTO tbl_matchOfficials
(MO_FN, MO_LN, MO_Gender, MO_DOB, MO_DOD,
Nationality, twitterHandle, Active, TMO)
VALUES ('$_POST[MO_FN]', '$_POST[MO_LN]',
'$_POST[MO_Gender]', '$_POST[MO_DOB]',
'$_POST[MO_DOD]', '$_POST[Nationality]',
'$_POST[twitterHandle]', '$_POST[Active]',
'$_POST[TMO]')");
$stmt->bindParam('MO_FN', $MO_FN);
$stmt->bindParam('MO_LN', $MO_LN);
$stmt->bindParam('MO_Gender', $MO_Gender);
$stmt->bindParam('MO_DOB', $MO_DOB);
$stmt->bindParam('MO_DOD', $MO_DOD);
$stmt->bindParam('Nationality', $Nationality);
$stmt->bindParam('twitterHandle', $twitterHandle);
$stmt->bindParam('Active', $TMO);
$stmt->bindParam('TMO', $TMO);
$stmt->execute();
echo "New record created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$db = null;
}
?>
Appreciate your help.
EDIT:
It's clear from the awesome assistance below that there is something else wrong, here is the whole code for the form. The form displays and holds the data as it should, it just won't insert and echo back (good or bad).
<!DOCTYPE HTML>
<html>
<head>
<title>New Match Official</title>
<style>
.error {color: #FF0000;}
</style>
<!-- Load jQuery from Google's CDN -->
<!-- Load jQuery UI CSS -->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<!-- Load jQuery JS -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<!-- Load jQuery UI Main JS -->
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<!-- Load SCRIPT.JS which will create datepicker for input field -->
<script src="script.js"></script>
<script src="script1.js"></script>
<link rel="stylesheet" href="runnable.css" />
</head>
<h1>
New Match Official
</h1>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['submit'])){
require '/connectDB.php';
try {
$stmt = $db->prepare("INSERT INTO tbl_matchOfficials
(MO_FN, MO_LN, MO_Gender, MO_DOB, MO_DOD,
Nationality, twitterHandle, Active, TMO)
VALUES (:MOFN, :MOLN,
:MOGender, :MODOB,
:MODOD, :Nationality,
:twitterHandle, :Active,
:TMO)");
$stmt->bindParam(':MOFN', $_POST['MO_FN']);
$stmt->bindParam(':MOLN', $_POST['MO_LN']);
$stmt->bindParam(':MOGender', $_POST['MO_Gender']);
$stmt->bindParam(':MODOB', $_POST['MO_DOB']);
$stmt->bindParam(':MODOD', $_POST['MO_DOD']);
$stmt->bindParam(':Nationality', $_POST['Nationality']);
$stmt->bindParam(':twitterHandle', $_POST['twitterHandle']);
$stmt->bindParam(':Active', $_POST['Active']);
$stmt->bindParam(':TMO', $_POST['TMO']);
$stmt->execute();
echo "New record created successfully";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$db = null;
}
?>
<?php
// define variables and set to empty values
$MO_FN = $MO_LN = $MO_Gender = $MO_DOB = $MO_DOD = $Nationality = $twitterHandle = $Active = $TMO = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$MO_FN = test_input($_POST["MO_FN"]);
$MO_LN = test_input($_POST["MO_LN"]);
$MO_Gender = test_input($_POST["MO_Gender"]);
$MO_DOB = test_input($_POST["MO_DOB"]);
$MO_DOD = test_input($_POST["MO_DOD"]);
$Nationality = test_input($_POST["Nationality"]);
$twitterHandle = test_input($_POST["twitterHandle"]);
$Active = test_input($_POST["Active"]);
$TMO = test_input($_POST["TMO"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// define variables and set to empty values
$FNErr = $LNErr = $GenderErr = $NationalityErr = $ActiveErr = $TMOErr = "";
$MO_FN = $MO_LN = $MO_Gender = $Nationality = $Active = $TMO = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["MO_FN"])) {
$FNErr = "First Name is required";
} else {
$MO_FN = test_input($_POST["MO_FN"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$MO_FN)) {
$FNErr = "Only letters and white space allowed";
}
}
if (empty($_POST["MO_LN"])) {
$LNErr = "Last Name is required";
} else {
$MO_LN = test_input($_POST["MO_LN"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$MO_LN)) {
$LNErr = "Only letters and white space allowed";
}
}
if (empty($_POST["MO_Gender"])) {
$GenderErr = "Gender is required";
} else {
$MO_Gender = test_input($_POST["MO_Gender"]);
}
if (empty($_POST["Nationality"])) {
$NationalityErr = "Nationality is Required (i.e. AUS for Australia)";
} else {
$Nationality = test_input($_POST["Nationality"]);
}
if (empty($_POST["Active"])) {
$ActiveErr = "Please state if Match Official is still active";
} else {
$Active = test_input($_POST["Active"]);
}
if (empty($_POST["TMO"])) {
$TMOErr = "Please state if Match Official performs the role of a TMO";
} else {
$TMO = test_input($_POST["TMO"]);
}
}
?>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="MO_FN" value="<?php echo $MO_FN;?>"><span class="error">* <?php echo $FNErr;?></span></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="MO_LN" value="<?php echo $MO_LN;?>"><span class="error">* <?php echo $LNErr;?></span></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="MO_Gender" <?php if (isset($MO_Gender) && $MO_Gender=="Male") echo "checked";?> value="Male">Male
<input type="radio" name="MO_Gender" <?php if (isset($MO_Gender) && $MO_Gender=="Female") echo "checked";?> value="Female">Female
<span class="error">* <?php echo $GenderErr;?></span></td>
</tr>
<tr>
<td>Date of Birth:</td>
<td><input type="text" id="datepicker" name="MO_DOB" value="<?php echo $MO_DOB;?>"></td>
</tr>
<tr>
<td>Date of Death:</td>
<td><input type="text" id="datepicker1" name="MO_DOD" value="<?php echo $MO_DOD;?>"></td>
</tr>
<tr>
<td>Nationality (TLA):</td>
<td><input type="text" maxlength="3" name="Nationality" value="<?php echo $Nationality;?>"><span class="error">* <?php echo $NationalityErr;?></span></td>
</tr>
<tr>
<td>Twitter Handle:</td>
<td><input type="text" name="twitterHandle" value="<?php echo $twitterHandle;?>"></td>
</tr>
<tr>
<td>Active Referee:</td>
<td><input type="radio" name="Active" <?php if (isset($Active) && $Active=="Yes") echo "checked";?> value="Yes">Yes
<input type="radio" name="Active" <?php if (isset($Active) && $Active=="No") echo "checked";?> value="No">No
<span class="error">* <?php echo $ActiveErr;?></span></td>
</tr>
<tr>
<td>TMO:</td>
<td><input type="radio" name="TMO" <?php if (isset($TMO) && $TMO=="Yes") echo "checked";?> value="Yes">Yes
<input type="radio" name="TMO" <?php if (isset($TMO) && $TMO=="No") echo "checked";?> value="No">No
<span class="error">* <?php echo $TMOErr;?></span></td>
</tr>
<tr>
<td><br><br><input type="submit"></td>
</tr>
</table>
</form>
<?php
echo "<h2>Your Input:</h2>";
echo "<table>";
echo "<tr><td>First Name: </td><td>$MO_FN</td></tr>";
echo "<tr><td>Last Name: </td><td>$MO_LN</td></tr>";
echo "<tr><td>Gender: </td><td>$MO_Gender</td></tr>";
echo "<tr><td>Date of Birth: </td><td>$MO_DOB</td></tr>";
echo "<tr><td>Date of Death: </td><td>$MO_DOD</td></tr>";
echo "<tr><td>Nationality: </td><td>$Nationality</td></tr>";
echo "<tr><td>Twitter Handle: </td><td>$twitterHandle</td></tr>";
echo "<tr><td>Active: </td><td>$Active</td></tr>";
echo "<tr><td>TMO: </td><td>$TMO</td></tr>";
echo "</table>"
?>
</body>
</html>