php - Inserting table data into a DB ignores !empty check and still inserts -
i have multiple tables user can enter data into. if user chooses don't have enter data every table , have checks see if fields empty before iterating through data , begin insert db.
here 1 of tables looks like:
<table id="budget_table"> <thead> <tr> <th>roofs</th> <th>area</th> <th>recommendation</th> <th>budget</th> </tr> </thead> <tbody id="budget_tb"> <tr id="budget_row0"> <td><input id="budget-roofs" name="budget-roofs[]" placeholder="entire roof area"></td> <td><input id="budget-area" name="budget-roof-area[]" placeholder="approx. 1,000 sq. ft."></td> <td><input id="budget-recommendation" name="budget-recommendation[]" placeholder="roof repairs & maintenance"></td> <td><input id="budget-amount" name="budget-amount[]" placeholder="$1,500.00 $2,500.00"></td> </tr> </tbody> </table> <input type="submit" value="create" name="submit"> when user clicks submit check see if first input field empty, if empty want nothing. if has within input fields want dump data db.
here php looks like:
var_dump($_post['budget-roofs']); //i added see going on if(!empty($_post['budget-roofs'])){ //foreach budget row insert data foreach ($_post['budget-roofs'] $index => $roofs) { $area = $_post['budget-roof-area'][$index]; $recommendations = $_post['budget-recommendation'][$index]; $amount = $_post['budget-amount'][$index]; //insert queries } } if enter nothing within input fields submits empty string db.
i used var_dump check see if empty , perhaps why ignoring check. being shown:
array (size=1) 0 => string '' (length=0) so shows have size of 1 (so not empty why check being ignored).
however unsure why has empty string?
i tried taking out placeholder attribute because thought maybe causing not issue either.
is there better way check see if see if input field empty , ensure empty strings don't dumped db?
where empty string coming from?
however unsure why has empty string?
the input fields in form submitted , if contain no data (entered user) submitted empty.
<?php var_dump($_post); ?> <form method="post" action="example.php"> <input type="text" name="arr" value=""> <input type="submit" name="submit" value="submit"> </form> if submit form above empty:
array(2) { ["arr"]=> string(0) "" ["submit"]=> string(6) "submit" } you have @ least 1 array element of budget-roofs since it's present in form. <input id="budget-roofs" name="budget-roofs[]" placeholder="entire roof area"> if not enter input still being submitted empty.
is there better way check see if see if input field empty , ensure empty strings don't dumped db?
you have check each element individually.
foreach ($_post['budget-roofs'] $index => $roofs) { if (! empty($roofs) // insert here. remember check other fields, may empty well.
Comments
Post a Comment