0

以下を使用して、会社のデータを mysql テーブルに挿入しています。再入力を試みる前に、その会社がすでにデータベースに登録されているかどうかを確認することはできますか? php 変数$companyは、チェックしたいものです。

   <?php
    require("database.php");
    // Opens a connection to a MySQL server
    $con = mysql_connect("localhost", $username, $password);

    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("medicom_wp", $con);

    $pages = get_posts(array(
        'orderby' => 'title', 
        'post_type' => 'members',
        'numberposts' => 300,
        'post_status' => 'any'  
        ));
        foreach($pages as $post) {
        setup_postdata($post);

        $company = get_field('company_name');
        $address = get_field('address');
        $city = get_field('city');
        $post_code = get_field('post_code');

        mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '')");
        }
        wp_reset_query();

    mysql_close($con);
    ?>
4

3 に答える 3

1
  1. 挿入を行う前に、selectステートメントを実行してそれらがそこにあるかどうかを確認してください

  2. (非推奨)名前フィールド(または会社を識別するその他のフィールド、一意のキーを作成して、もう一度入力しようとすると拒否されるようにする)

于 2012-06-14T15:34:25.743 に答える
1

以下を試してください。基本的に、別のクエリを送信して、同じ会社名の重複行をチェックし、クエリが 0 行を返す場合は、insert コマンドのみを実行します。

<?php
require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);

if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("medicom_wp", $con);

$pages = get_posts(array(
    'orderby' => 'title', 
    'post_type' => 'members',
    'numberposts' => 300,
    'post_status' => 'any'  
    ));
foreach($pages as $post) {
    setup_postdata($post);

    $company = get_field('company_name');
    $address = get_field('address');
    $city = get_field('city');
    $post_code = get_field('post_code');

    // prepare query to check for duplicate company
    $sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
    // run query
    $row_dup = mysql_fetch_assoc(mysql_query($sql,$conn));
    // if no row exist
    if ($row_dup['cnt'] == 0) {
        // insert new company
        // consider preparing this query using sprintf() and mysql_real_escape_string() as above
        mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '')");
    }
}
wp_reset_query();

mysql_close($con);
?>
于 2012-06-14T15:41:33.730 に答える
0

The natural solution would be to run another query prior (e.g. select count()) to check if the marker exists, and branch your conditional logic from there.

A more interesting solution would be to use the concept of an UPSERT (Update + Insert). An upsert inserts the row if it doesn't exist, and updates it if it does exists. So effectively there will be only 1 row in the end regardless, but this is assuming you don't mind overwriting the data. Here's a sample SO question about that.

Another technique involves creating a primary key column and taking advantage of mysql's integrity checks to forcefully keep 1 row per record. So the first insert for each primary key would be successful, but all other would fail.

于 2012-06-14T15:37:38.473 に答える