Tuzki Tuzki - 3 months ago 12
MySQL Question

PDOException error PHP SQL "Firstname Cannot be Null"

I currently have a problem with the code I have below..

It keeps saying

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]:
Integrity constraint violation: 1048 Column 'Firstname' cannot be null'
in C:\xampp\htdocs\clubresults\checkregister.php:30 Stack trace: #0
C:\xampp\htdocs\clubresults\checkregister.php(30): PDOStatement->execute()
#1 {main} thrown in C:\xampp\htdocs\clubresults\checkregister.php on line 30


I've tried everything I know but I just cannot remove that error... any kind of help is really appreciated :D
This php file is called checkregister.php

$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
#Set Error Mode to ERRMODE_EXCEPTION.
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('INSERT INTO members
(firstname, surname, DD, MM, YYYY,
email, Player1, Player2, Player3, Player4, Player5)
VALUES
(:firstname, :surname, :DD, :MM, :YYYY,
:email, :Player1, :Player2, :Player3, :Player4, :Player5)');

$stmt->bindParam(':firstname', $_GET['firstname']);
$stmt->bindParam(':surname', $_GET['surname']);
$stmt->bindParam(':DD', $_GET['DD']);
$stmt->bindParam(':MM', $_GET['MM']);
$stmt->bindParam(':YYYY', $_GET['YYYY']);
$stmt->bindParam(':email', $_GET['email']);
$stmt->bindParam(':Player1', $_GET['Player1']);
$stmt->bindParam(':Player2', $_GET['Player2']);
$stmt->bindParam(':Player3', $_GET['Player3']);
$stmt->bindParam(':Player4', $_GET['Player4']);
$stmt->bindParam(':Player5', $_GET['Player5']);
$stmt->execute();


This is my form in another php file:

<form name="register" method="post" action="checkregister.php">

<!--Text, using <p> tag for new line-->
Main Account Registration <p>
* All Fields Mandatory <P>


<div class="required_field">
<label for="leadername">Team Leader's First Name</label>
<input name="firstname" type="text" value="hihi"><p>
<span id="firstnameerror" class="error"></span>
</div>

the same code repeats for each of the other ones such as Surname, DD MM YY, etc

<input type="submit" name="submitbutton" id="submitbutton" value="Register">
<input type="reset" name="resetbutton" id="resetbutton" value="Clear all Fields">
</form>

Answer

Working backwards:

  • MySQL complains about a null value; so why is null received?
  • PDO passes null to MySQL if a null value is passed in bindParam(); why are we passing a null value?
  • When a variable in PHP is not defined and you attempt to use it, it will a) issue a warning and b) implicitly set it to null; why are we using an undefined variable?
  • The $_GET['firstname'] is not defined (amongst others); why is is not defined?
  • You're using method="post" in your form, so the form entries are passed via $_POST instead of $_GET.