使用表单的Wordpress更新数据库

时间:2012-01-09 22:51:53

标签: php mysql wordpress

我写了这个快速脚本,在移动新闻博客的位置时更新数据库。问题: 如果它显示已更新了多少行将会很好。这可能吗?

 <?php
if(isset($_GET['confirm'])){

    // Load Wordpress
    require_once('wp-load.php');

    // Form variables
    $site_was = $_GET['site_was'];
    $site_now = $_GET['site_now'];

    $db_queries = array(
        "UPDATE wp_options SET option_value = replace(option_value, '".$site_was."', '".$site_now."') WHERE option_name = 'home' OR option_name = 'siteurl'",
        "UPDATE wp_posts SET guid = REPLACE (guid, '".$site_was."', '".$site_now."')",
        "UPDATE wp_posts SET post_content = REPLACE (post_content, '".$site_was."', '".$site_now."')",
        "UPDATE wp_posts SET post_content = REPLACE (post_content, 'src=\"".$site_was."', 'src=\"".$site_now."')",
        "UPDATE wp_posts SET  guid = REPLACE (guid, '".$site_was."', '".$site_now."') WHERE post_type = 'attachment'",
        "UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, '".$site_was."','".$site_now."')"
    );

    foreach ($db_queries as $sql) {
        $wpdb->query($sql);
    }

} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <p>E.g http://examples.com or http://www.example.com/shop</p>
    <label>
        Site was:
        <input type="text" name="site_was" value="<?php if(isset($_GET['site_was'])) echo $_GET['site_was']?>" size="50" />
    </label>
    <br />
    <label>
        Site now:
        <input type="text" name="site_now" value="<?php if(isset($_GET['site_now'])) echo $_GET['site_now']?>" size="50" />
    </label>

    <input type="submit" name="confirm" value="Update Datebase" />
</form>
<?php } ?>

1 个答案:

答案 0 :(得分:1)

您应该使用相关的表行和数据调用$ wpdb-&gt; update()。这将返回受影响的行数。

e.g。

$rows = $wpdb->update( 
    'table', 
    array( 
        'column1' => 'value1',  // string
        'column2' => 'value2'   // integer (number) 
    ), 
    array( 'ID' => 1 ), 
    array( 
        '%s',   // value1
        '%d'    // value2
    ), 
    array( '%d' ) 
);

见这里:http://codex.wordpress.org/Class_Reference/wpdb#UPDATE_rows