php — Zbiorcze zapisywanie danych w niestandardowej tabeli
Mam formularz na stronie administratora, który przechowuje dane w niestandardowej tabeli z wieloma polami wyboru. Po przesłaniu formularza muszę zapisać identyfikator zaznaczonego pola z identyfikatorem sklepu, który jest powiązany w parę kluczy (store_id => checkbox_id lub 588 => 552, 588 => 325, 588 => 61, więc każdy data_id może być nowa linia z kluczem sotre_id, więc później mogę wybrać dowolny identyfikator i uzyskać wszystkie sprawdzone wartości z tym identyfikatorem)
a także sprawdź, czy niektóre niezaznaczone dane są zapisane, a jeśli tak, usuń je; a może jeśli dane już tam są, nic nie rób.
Jaki jest najlepszy sposób, aby to zrobić? Wybierz każdy rekord store_id, usuń wszystkie, a następnie po prostu zapisz nowy sprawdzony identyfikator lub sprawdź, czy już tam jest, ale odznacz, a następnie usuń i zapisz nowe wpisy, których jeszcze nie ma? pola wyboru nie będą miały więcej niż 15 wpisów, więc nie są ogromne. W każdym razie wszystkie moje próby kończyły się na
Błąd bazy danych WordPress: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ” at line 1]
WŁÓŻ Wwp_mytable
(identyfikator_sklepu,id_danych) WARTOŚCI;
Form.php
add_action('wp_ajax_filterBrandRestrict', 'filter_brand_retailer');
add_action('wp_ajax_nopriv_filterBrandRestrict', 'filter_brand_retailer');
function filter_brand_retailer()
{
$store_id = $_POST['retailerfilter'];
global $wpdb;
//var_dump($_POST);
$query = $wpdb->prepare("SELECT data_id FROM `mytable` WHERE `store_id` = '%d'", $store_id);
$results = $wpdb->get_results($query, OBJECT_K);
$Brand = new WP_Query(array('post_type' => 'watch_brand'));
?>
<?php
if ($Brand->have_posts()) : ?>
<div class="control-group legend_brand">
<form method="post" action="<?php echo site_url() ?>/wp-admin/admin-post.php" contentType: false,>
<!-- Store id -->
<input type="hidden" name="store_ids" value="<?php echo $store_id ?>">
<input type="hidden" name="action" value="saveBrand">
<!-- loop on brand to create checkboxes -->
<?php while ($watchBrand->have_posts()) : $watchBrand->the_post(); ?>
<label class="control control-checkbox">
<?php echo get_the_title(); ?>
<input name="data_ids" value="<?php echo get_the_ID(); ?>" <?= isset($results[get_the_ID()]->data_id) == get_the_ID() ? 'checked' : '' ?> type="checkbox" />
<div class="control_indicator"></div>
</label>
<?php endwhile; ?>
<div class="submit_default">
<button name="Submit" class="submit_button" type="submit">Envoyer</button>
</div>
</form>
</div>
<?php
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
}
brand.php
<div class="wrap">
<div class="brand">
<div class="brand_dropdown">
<p class="brand_text">Form </p>
<form method="POST" action="<?php echo site_url() ?>/wp-admin/admin-ajax.php">
<div class="select">
<input type="hidden" name="action" value="filterBrandRestrict">
<select name="retailerfilterwatch">
<option value="">Select retailer...</option>
<?php
$posts = new WP_Query(array('post_type' => 'retailer'));
while ($posts->have_posts()) : $posts->the_post();
wp_reset_postdata(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile; ?>
</select>
<button id="filter_btn">Apply filter</button>
</div>
</form>
</div>
<div class="brand__checkbox" id="responseBrand">
</div>
function save,
global $wpdb;
// Get the values
$store_id[] = (null !== $_POST['store_ids']) ? sanitize_text_field($_POST['store_ids']) : '';
$watch_brand_id = (null !== $_POST['data_ids']) ? sanitize_text_field($_POST['data_ids']) : ''; // Empty value if data not set
// die(var_dump(['store_ids']));
// die(gettype($watch_brand_id));
$attribut_table = $wpdb->prefix . 'jr_restriction';
$format = array('%d', '%d', '%d');
// print_r($store_id);
// get the saved values
$itemsInDB = $wpdb->prepare("SELECT data_id FROM `wp_of_jr_restriction` WHERE `store_id` = %d", $store_id);
$results = $wpdb->get_results($itemsInDB, ARRAY_A);
$match = array_intersect($results, $_POST);
$nomatch = array_values(array_diff($results, $_POST['data_ids']));
$insertArray = array($store_id[0] => $nomatch);
$insertQuery = "INSERT INTO `$attribut_table`( store_id,data_id) VALUES";
foreach ($insertArray as $an_item) {
$insertQuery .= $wpdb->prepare(
"(%s, %s, %d),",
$an_item['store_id'],
$an_item['nomatch'],
);
}
$q = rtrim($insertQuery, ',') . ';';
//die(print_r($q));
$wpdb->query($q);
Dzięki