mysql - Check empty fields on php -


i got insert action db , wanted check on php foreach loop.i don't understand wrong in code because there no successfull or fail result return.any appreciated.

<?php $placename = $_post['placename']; $placeaddress = $_post['addressarea']; $placephone = $_post['placephone']; $placewebsite = $_post['placewebsite']; $oonweekday = $_post['openweekday']; $oonweekend = $_post['openweekend']; $conweekday = $_post['closeweekday']; $conweekend = $_post['closeweekend']; $lati = $_post['latitude']; $longi = $_post['longitude']; $btnaddplace = $_post['addplacebut'];   $dbqueryi = $nesnepdo->prepare("insert places                        (place_name,place_address,place_phone,place_web,                         oonweek_day,oonweek_end,conweek_day,conweek_end,                         lati_p,longi_p)                  values (:p_n,:p_a,:p_p,:p_w,                         :oowd_p,:oowe_p,:cowd_p,:cowe_p,                         :l_p,:l2_p)");   $dbqueryi->bindparam(":p_n",$placename); $dbqueryi->bindparam(":p_a",$placeaddress); $dbqueryi->bindparam(":p_p",$placephone); $dbqueryi->bindparam(":p_w",$placewebsite); $dbqueryi->bindparam(":oowd_p",$oonweekday); $dbqueryi->bindparam(":oowe_p",$oonweekend); $dbqueryi->bindparam(":cowd_p",$conweekday); $dbqueryi->bindparam(":cowe_p",$conweekend); $dbqueryi->bindparam(":l_p",$lati); $dbqueryi->bindparam(":l2_p",$longi);   $fields = array($placename,$placeaddress,$placephone,$placewebsite,                 $oonweekday,$oonweekend,$conweekday,$conweekend,                 $lati,$longi); $errors = false; foreach ($fields $fieldname )  {     if(!isset($_post[$fieldname]) || empty($_post[$fieldname]))      {         $response['errors'] = 0;         $response['message'] = "some fields empty fill them..";         die(json_encode($response));         $errors = true; //yup there errors     }  }  if(!$errors) {         $dbqueryi->execute();         $response['errors'] = 1;         $response['message'] = "that's it..";         header('refresh: 3; url=addplaces.php');         die(json_encode($response));  } ?> 

sorry didn't explain problem because afk.i posted mobile. problem want control fields whether empty or not.i have form users , want collect them info.this not entire code these inputs i'm gonna check.i have no result record add.i tried different ways they're not work well.even records entirely empty inserted.just wanted check fields fill , insert.

when this:

if(!isset($_post[$fieldname]) || empty($_post[$fieldname]))  

i think true, because $fieldname set value of post parameter, not name of parameter. may not seeing error message because of json_encode() being inside die().

if want check of these have not been given, can move part of code top of script instead, needs changes.

$fields = array('placename', 'addressarea', 'placephone', 'placewebsite',                 'openweekday', 'openweekend', 'closeweekday', 'closeweekend',                  'latitude','longitude'); // use names strings here  $errors = false; foreach ($fields $fieldname )  {     // can use empty() here, because includes checking isset()     if(empty($_post[$fieldname])) {         $response['errors'] = 0; // not sure why errors = 0 here         $response['message'] = "some fields empty fill them..";         $response = json_encode($response); // separate json_encode die         die($response);          // never execute because script has died         $errors = true; //yup there errors     }  } 

then not need worry checking $errors afterward, because if there errors, script have died.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -