What's new

Closed How to insert null value in database myphpadmin

Status
Not open for further replies.

Penpenqt69

Addict
Joined
Jul 31, 2018
Posts
36
Reaction
3
Points
83
Age
24
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.png
 

Attachments

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.
 
Last edited:
No po di po ako nag hahäçk im creating a website for our project.
yes di naman sya maghahäçk . ignore mo sasabihin nila patapos sila kung magsalita . nasagot na nung nasataas tama yon. actually the same to sa gusto mo mag happen like optional textbox same sa name kung saan optional lang ang middle name
 
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';
 
Depende yan sa database definition kung allow o hindi yung null value. Kung allow, eh di wala, ie don't put anything on it.
 
Status
Not open for further replies.

Similar threads

Back
Top