简体   繁体   中英

Insert batch to MySQL with PHP/PDO

I have an form for bulk add domains and these domains could have one or more countries and one language attribute.

My form (reduced to code):

Add domains:

<textarea name="bulk" rows="10" class="form-control no-resize"><?php echo $_POST['bulk']; ?></textarea>

Choose one or more countries:

<div class="checkbox">
  <?php
  if ($queries->query("SELECT * FROM countrycodes ORDER BY code ASC")) {
    if ($queries->count() > 0) {
      $contents = $queries->fetchAll();
      foreach ($contents as $content) {
  ?>
  <input type="checkbox" id="country_<?php echo $content->code; ?>" name="country[]" value="<?php echo $content->code; ?>" <?php if (in_array($content->code, $_POST['country'])) echo "checked = 'checked'"; ?> />
  <label for="country_<?php echo $content->code; ?>"><?php echo $content->country; ?></label><br />
  <?php }}} ?>
</div>

Choose the language:

<div class="radio">
<?php
if ($queries->query("SELECT * FROM langcodes ORDER BY code ASC")) {
  if ($queries->count() > 0) {
    $contents = $queries->fetchAll();
    foreach ($contents as $content) {
?>
<input type="radio" name="lang" id="lang_<?php echo $content->code; ?>" value="<?php echo $content->code; ?>" <?php if ($content->code == $_POST['lang']) { ?>checked="" <?php } ?> />
<label for="lang_<?php echo $content->code; ?>"><?php echo $content->language; ?></label><br />
<?php }}} ?>
</div>

Get data from submit:

if (isset($_POST['addData'])) {
  $doms = explode("\n", str_replace("\r", "", $_POST['bulk']));
  $coun = implode(", ", $_POST['country']);
  $lang = $_POST['lang'];
}

I found this one for bulk inserts:

$d    = ['Osian', 'Williams', 1];
$data = array_fill(0, 1000, $d);
$db   = new PDO('mysql:host=localhost;dbname=MYDB','MYUSER','MYPASS');
$db->beginTransaction();
$stmt = $db->prepare('INSERT INTO members (firstname, surname, title) VALUES (?,?,?)');
for($i=0;$i<count($data);$i++) {
  $stmt->execute($data[$i]);
}
$db->commit();

I have no idea, how to get my data (domains, countries, lang) working for this code.

And, I need to check if a domain is already in the db, but only, if the domain has the same language.

So, would be nice to get some help.

I found an solution. Was a bit confused at the beginning, how to add country and lang to the list of domains. But the country data and lang data are static - so I just need to loop through the domains.

That's my solution and it works:

if (isset($_POST['addData'])) {
  $doms = explode("\n", str_replace("\r", "", $_POST['bulk']));
  $coun = implode(", ", $_POST['country']);
  $lang = $_POST['lang'];

  $queries->transStart();
  foreach ($doms as $dom) {
    if ($queries->query("SELECT domain FROM websites WHERE domain = ? AND lang = ?", [$dom, $lang])) {
      if ($queries->count() > 0) {
        $infoBox = "error_duplicate";
      } else {
        $queries->query("INSERT INTO websites (domain, country, lang) VALUES (?,?,?)", [$dom, $coun, $lang]);
        $infoBox = "success";
      }
    }
  }
  $queries->transStop();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM