🔒 Closed How to insert null value in database myphpadmin

Status
Not open for further replies.

Penpenqt69

Journeyman
pano po mag insert ng null value sa database kapag kunwari walang laman yung sa form pero kapag nilagyan nung input yung iba mag lalagay sya sa database 1580516617286.webp
 
Maglagay ka ng input validation sa form mo kung empty ba yung optional field mo bago mo isama sa sql query mo. If I am not mistaken by default optional naman lahat ng fields sa sql unless naka define sa schema ang isang field NOT NULL.
 
The first requirement is to configure your table to store nulls. But you know, letting null to store into your database table is not advisable. It's not that because it's bad or something, they are actually built for a reason but handling null requires a bit of special handling hence it's a more complex to handle at some point and situations that's why some devs or db admins avoid them in a well designed and normalized schema. By the way, in .Net we store null using the literal 'null' keyword. I dont know how to do that in php. And your code? wow! I miss those coding scheme back in college. That's the most vulnerable way of dealing with database(You didnt even use parameterized sql command strings). Avoid using sql text commands. It's prone in injection tricks. Use Stored Procedures, Triggers instead or you might want to learn using O/RM like Entity framwork or NHibernate. It doesnt bother you now because it's just for academic purposes but in production? man! you might be fired by your project head. haha! Using Stored proc or O/RM promotes maintainability, security, cleaner code, etc. utilize it's advantage, learn using it and thank me later.
 
Easy way to insert NULL values via php is this, first make sure that the column is allowed to have NULL values:
PHP:
$email = isset($_POST['EmailAddress']) ? $_POST['EmailAddress] : 'NULL';

But better to use trim() function to invalidate space/s input like this:
PHP:
$email = (isset($_POST['EmailAddress']) && !empty(trim($_POST['EmailAddress'])) ? $_POST['EmailAddress] : 'NULL';
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 10
    Replies
  • 960
    Views
  • 9
    Participants
Last reply from:
homer_simpson

Trending Topics

Online now

Members online
803
Guests online
1,612
Total visitors
2,415

Forum statistics

Threads
2,291,025
Posts
29,068,220
Members
1,211,656
Latest member
hazey
Back
Top