I know there are other posts on this topic but they don't answer the basic question. The previous answers all say to use PDO::PARAM_NULL but doesn't that remove the entire reason for using PDO anyway?
Here is my code:
$sql = "INSERT INTO users_address (uid,street1,street2,city,state,region,postalcode,country) VALUES (?,?,?,?,?,?,?,?)";
$stmt = $this->db->conn_id->prepare($sql);
$stmt->bindParam(1, $uid, PDO::PARAM_INT);
$stmt->bindParam(2, $street1, PDO::PARAM_STR);
$stmt->bindParam(3, $street2, PDO::PARAM_STR);
$stmt->bindParam(4, $city, PDO::PARAM_STR);
$stmt->bindParam(5, $state, PDO::PARAM_STR);
$stmt->bindParam(6, $region, PDO::PARAM_STR);
$stmt->bindParam(7, $postalcode, PDO::PARAM_STR);
$stmt->bindParam(8, $country, PDO::PARAM_STR);
$stmt->execute();
Not all of these values are required so sometimes they are null. Both varchar and int database fields have default=null. How can I prepare this so that if a variable happens to be null, the null gets inserted? All of the other answers I have seen pass NULL for the second parameter but how do I know at run time it is a null?
To be really clear. I do not want to implicitly insert a null. I want to insert a variable which MAY be a null if the user did not complete the form. Not all fields are required on the form. For example, in this example, $street2.