admin管理员组文章数量:1326134
I'm new in php and just have a quick question, I'm developing a plugin, in metaboxes I have a function to save data into database and this is the code I use to save:
$models_to_insert = array();
for ($i = 1; $i <= 3; $i++) {
if (isset($_POST["car_model_$i"]) != "") {
$car_model = sanitize_text_field($_POST["car_model_$i"]);
}
if (isset($_POST["car_price_$i"]) != "") {
$car_price = sanitize_text_field($_POST["car_price_$i"]);
}
if (isset($_POST["car_brand_$i"]) != "") {
$car_brand = sanitize_text_field($_POST["car_brand_$i"]);
}
if (isset($_POST["car_year_$i"]) != "") {
$car_year = sanitize_text_field($_POST["car_year_$i"]);
}
if (isset($_POST["car_type_$i"]) != "") {
$car_type = sanitize_text_field($_POST["car_type_$i"]);
}
if (isset($_POST["car_basefee_$i"]) != "") {
$car_basefee = sanitize_text_field($_POST["car_basefee_$i"]);
}
if (isset($_POST["car_basemodel_$i"]) != "") {
$car_basemodel = sanitize_text_field($_POST["car_basemodel_$i"]);
}
if (isset($_POST["car_rcusa_$i"]) != "") {
$car_rcusa = sanitize_text_field($_POST["car_rcusa_$i"]);
}
if (isset($_POST["car_bono_$i"]) != "") {
$car_bono = 1;
} else {
$car_bono = 0;
}
$single_model = array(
'post_id' => $post_id,
'car_model' => $car_model,
'car_price' => $car_price,
'car_brand' => $car_brand,
'car_year' => $car_year,
'car_type' => $car_type,
'car_basefee' => $car_basefee,
'car_basemodel' => $car_basemodel,
'car_rcusa' => $car_rcusa,
'car_bono' => $car_bono
);
array_push($models_to_insert, $single_model);
}
if (cotizador_query_select($post_id) == null) {
foreach($models_to_insert as $model){
$wpdb->insert($table_name, $model);
}
} else {
$wpdb->delete($table_name, array('post_id' => $post_id));
foreach($models_to_insert as $model){
$wpdb->insert($table_name, $model);
}
}
The for counts until 3 because I have 3 times the inputs for 3 car models:
But if I just fill the first model and leave the others inputs empty, I got 3 records inside database with empty fields instead just one because only one model is filled:
How can I just ignore the rest of inputs that are not filled??
I'm new in php and just have a quick question, I'm developing a plugin, in metaboxes I have a function to save data into database and this is the code I use to save:
$models_to_insert = array();
for ($i = 1; $i <= 3; $i++) {
if (isset($_POST["car_model_$i"]) != "") {
$car_model = sanitize_text_field($_POST["car_model_$i"]);
}
if (isset($_POST["car_price_$i"]) != "") {
$car_price = sanitize_text_field($_POST["car_price_$i"]);
}
if (isset($_POST["car_brand_$i"]) != "") {
$car_brand = sanitize_text_field($_POST["car_brand_$i"]);
}
if (isset($_POST["car_year_$i"]) != "") {
$car_year = sanitize_text_field($_POST["car_year_$i"]);
}
if (isset($_POST["car_type_$i"]) != "") {
$car_type = sanitize_text_field($_POST["car_type_$i"]);
}
if (isset($_POST["car_basefee_$i"]) != "") {
$car_basefee = sanitize_text_field($_POST["car_basefee_$i"]);
}
if (isset($_POST["car_basemodel_$i"]) != "") {
$car_basemodel = sanitize_text_field($_POST["car_basemodel_$i"]);
}
if (isset($_POST["car_rcusa_$i"]) != "") {
$car_rcusa = sanitize_text_field($_POST["car_rcusa_$i"]);
}
if (isset($_POST["car_bono_$i"]) != "") {
$car_bono = 1;
} else {
$car_bono = 0;
}
$single_model = array(
'post_id' => $post_id,
'car_model' => $car_model,
'car_price' => $car_price,
'car_brand' => $car_brand,
'car_year' => $car_year,
'car_type' => $car_type,
'car_basefee' => $car_basefee,
'car_basemodel' => $car_basemodel,
'car_rcusa' => $car_rcusa,
'car_bono' => $car_bono
);
array_push($models_to_insert, $single_model);
}
if (cotizador_query_select($post_id) == null) {
foreach($models_to_insert as $model){
$wpdb->insert($table_name, $model);
}
} else {
$wpdb->delete($table_name, array('post_id' => $post_id));
foreach($models_to_insert as $model){
$wpdb->insert($table_name, $model);
}
}
The for counts until 3 because I have 3 times the inputs for 3 car models:
But if I just fill the first model and leave the others inputs empty, I got 3 records inside database with empty fields instead just one because only one model is filled:
How can I just ignore the rest of inputs that are not filled??
Share Improve this question asked Aug 13, 2020 at 3:30 Israel SantiagoIsrael Santiago 112 bronze badges1 Answer
Reset to default 0I found one solutions and it works, just in case someone could have the same question, I know that the response could be with an:
if(input==""){
}
But the problem was that I'm inserting every model in an object, so if the fields are empty, it insert in the object empty values, so I just did this:
if ($single_model['car_model'] != "") {
array_push($models_to_insert, $single_model);
} else {}
because if no model name is added in the first input and user clicks "update" or "publish" button, means that user don't want/need another model
Not sure if it's the best way but it works
本文标签: pluginsHow to ignore fields if empty
版权声明:本文标题:plugins - How to ignore fields if empty? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742190970a2430206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论