php - Insert array values into a database -
i have system store checkbox data array. if checked put value in array. let's there 6 checkboxes, , check last 3, array values [3][4] , [5] have values, correct? ok.
now if array values [0][1] , [2] 0 haven’t been checked, value?
the question is, when mysql insert database , use code?
mysqli_query($con,"insert accounts (chk1, chk2, chk3, chk4, chk5, chk6) values ('$checkbox[0]', '$checkbox[1]', '$checkbox[2]', '$checkbox[3]', '$checkbox[4]', '$checkbox[5]'");
now, when query executes, if first arrays nothing, skip them , pretend not there. there way can make them put 0 if haven’t been checked.
put code before query
$checkbox[0]=(isset($checkbox[0]))?$checkbox[0]:0; $checkbox[1]=(isset($checkbox[1]))?$checkbox[1]:0; $checkbox[2]=(isset($checkbox[2]))?$checkbox[2]:0; $checkbox[3]=(isset($checkbox[3]))?$checkbox[3]:0; $checkbox[4]=(isset($checkbox[4]))?$checkbox[4]:0; $checkbox[5]=(isset($checkbox[5]))?$checkbox[5]:0;
or
// $_post value function initpostvalue($elementvar, $defval=null) { if(!isset($_post[$elementvar])){ $_post[$elementvar] = $defval; } return $_post[$elementvar]; } // $_request value function initrequestvalue($elementvar, $defval=null) { if(!isset($_request[$elementvar])){ $_request[$elementvar] = $defval; } return $_request[$elementvar]; } // $_get value function initgetvalue($elementvar, $defval=null) { if(!isset($_get[$elementvar])){ $_get[$elementvar] = $defval; } return $_get[$elementvar]; } example : $checkbox[0] = initrequestvalue('checkbox[0]',0);
use above function value take $_get,$_post,$_request there no need check empty
Comments
Post a Comment