I have created a table with NOT NULL
constraints on some columns in MySQL. Then in PHP I wrote a script to insert data, with an insert query. When I omit one of the NOT NULL
columns in this insert statement I would expect an error message from MySQL, and I would expect my script to fail. Instead, MySQL inserts empty strings in the NOT NULL
fields. In other omitted fields the data is NULL, which is fine. Could someone tell me what I did wrong here?
I'm using this table:
CREATE TABLE IF NOT EXISTS tblCustomers (
cust_id int(11) NOT NULL AUTO_INCREMENT,
custname varchar(50) NOT NULL,
company varchar(50),
phone varchar(50),
email varchar(50) NOT NULL,
country varchar(50) NOT NULL,
...
date_added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (cust_id)
) ;
And this insert statement:
$sql = "INSERT INTO tblCustomers (custname,company)
VALUES ('".$customerName."','".$_POST["CustomerCompany"]."')";
$res = mysqli_query($mysqli, $sql);
Or using bind variables:
$stmt = mysqli_prepare($mysqli, "INSERT INTO tblCustomers (custname,company, email, country) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($stmt, 'ssss', $customerName, $_POST["CustomerCompany"], $_POST["CustomerEmail"], $_POST["AddressCountry"]);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);