MySql NOT NULL Limit does not work

I am trying to implement a NOT NULL constraint in a client table that is created as:

CREATE TABLE CUSTOMER(
     cust_id INT(5) NOT NULL AUTO_INCREMENT, 
     PRIMARY KEY(cust_id),
     first_name VARCHAR(25) NOT NULL,
     last_name VARCHAR(25) NOT NULL,
     email VARCHAR(25) NOT NULL,
     password VARCHAR(25) NOT NULL,
     gender VARCHAR(1) NOT NULL,
     city VARCHAR(25) NOT NULL,
     dob DATE NOT NULL,
     pin INT NOT NULL);

After that, from the form, I pass the values ​​and insert them as:

$sql= "INSERT INTO customer(first_name,last_name,email,password,gender,city,dob,pin) VALUES('$first_name','$last_name','$email_add','$password','$gender','$city','$DOB','$pin')";

However, if I pass in empty field values, does Mysql seem to insert them? What could be the problem?

+3
source share
3 answers
$sql= "INSERT INTO customer(first_name,last_name,email,password,gender,city,dob,pin) VALUES('$first_name',".
($last_name?"'".$last_name."'":"NULL").
", '$email_add','$password','$gender','$city','$DOB','$pin')";

This will:

VALUES('ahm',NULL,'email@a.ddr' ...

And due to NOT NULL nothing will be inserted.

+3
source

I suspect that, since in a real DBMS, unlike Oracle :-), there is a difference between an empty value and a NULL value.

NOT NULL. , NOT NULL, - , ,... NULL. , . .: -)

, Oracle , VARCHAR NULL ( , , - ).

, PHP- Oracle ( NULL), , - ():

if $first_name == "":
    $first_name = "NULL"
else:
    $first_name = "'" + $first_name + "'"
: : :
$sql= "INSERT INTO customer(" +
    "first_name," +
    "last_name," +
    "emailpassword," +
    "gender," +
    "city," +
    "dob," +
    "pin" +
    ") VALUES (" + 
        $first_name + "," +
        "'$last_name'" + "," +
        "'$email_add'" + "," +
        "'$password'" + "," +
        "'$gender'" +
        ",'$city'" + "," +
        "'$DOB'" + "," +
        "'$pin'" +
    ")";

.. , NULL. , $first_name.

, first_name INSERT NULL, $first_name, .

, / SQL-.

+5

Additional information from the documentation:

http://dev.mysql.com/doc/refman/5.0/en/problems-with-null.html

0
source

Source: https://habr.com/ru/post/1708058/


All Articles