php - MySQL - PDO - Allow if POSTS are NULL -
if fill every input in form, data go database, when don't fill input telephone, data not go database , give error :
error sqlstate 23000 integrity constraint violation 1048 column 'telephone' cannot null
so means, if field telephone empty, , click submit data not go database.
so question is: how can allow if textinput 'telephone' not filled, data still go database?
this .php
<?php include("dbconfig.php"); if( isset($_post['name']) ) { try { $date= date('d.m.y'); $tijd= date("h:i a"); $ip = $_server['remote_addr']; $conn = new pdo('mysql:host=localhost;dbname=*****', $username, $password); $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $mhbt = $conn->prepare("insert orders (name, email, telephone, city) values (?, ?, ?, ?)"); $mhbt->bindparam(1, $_post["name"]); $mhbt->bindparam(2, $_post["email"]); $mhbt->bindparam(3, $_post["telephone"]); $mhbt->bindparam(4, $_post["city"]); $mhbt->bindparam(5, $date); $mhbt->bindparam(6, $tijd); $mhbt->bindparam(7, $ip); $mhbt->execute(); } catch(pdoexception $e) { echo 'error: ' . $e->getmessage(); } } ?>
be careful when ever creating columns not null
attribute. need pass value when want save data table.
change in table. change default value of telephone field null
. if no value assigned automatically null
added telephone field
.
just query this:- alter table orders modify column telephone varchar(15) null
note:- more @ db level. give relaxation future problems.thanks.
Comments
Post a Comment