Wordpress Plugin多次执行SQL

时间:2013-07-04 14:59:18

标签: php sql wordpress plugins

我创建了一个简单的Wordpress插件,它将Text-Data存储在数据库中

if(isset($_POST['staticbox_submit'])) {
    $sql = "UPDATE staticbox SET content = '".$_POST['staticbox_content']."';";
    if(!$wpdb->query($sql)) {echo '<p style="color:#990000;">Fehlgeschlagen.</p>';} else {echo '<p style="color:#009900;">Ausgef&uuml;hrt.</p>';}   
}

并在前端输出

function staticbox() {
    global $wpdb;

    $staticbox_content = $wpdb->get_col( "SELECT content FROM staticbox" );
    foreach($staticbox_content as $item) {echo $item;}
}

我在表单中有一个textarea和按钮。如果设置了按钮的后变量,则将文本存储在数据库中。

    <form action="?page=staticbox/main.php" method="post">
        <textarea name="staticbox_content" cols="100" rows="10"><?php $staticbox_content = $wpdb->get_col( "SELECT content FROM staticbox" );   foreach($staticbox_content as $item) {echo $item;} ?></textarea><br />
        <input type="submit" name="staticbox_submit" />
    </form>

由于任何原因,文本在数据库中存储3次。你能帮我找到问题吗?

以下是一些截图

我在Backend中输入的内容

What I entered in Backend

MySQL中的内容

enter image description here

在前端看起来像什么

What it looks like in Frontend

修改 继承整个文件:

<?php
/*** WIRD BEIM AKTIVIEREN AUSGEFÜHRT ***/
function staticbox_activate() {
    $sql = "
        CREATE TABLE IF NOT EXISTS `staticbox` (
        `content` text NOT NULL 
        )";
    global $wpdb;
    $wpdb->query($sql);
}
register_activation_hook( __FILE__, 'staticbox_activate' );

/*** WIRD BEIM DEINSTALLIEREN AUSGEFÜHRT ***/
function staticbox_uninstall() {
    $sql = "DROP TABLE `staticbox`";
    global $wpdb;
    $wpdb->query($sql);
}
register_uninstall_hook( __FILE__, 'staticbox_uninstall' );

/*** DIE FRONTEND FUNKTIONEN ***/
function staticbox() {
    global $wpdb;

    $staticbox_text = "<strong>Überschrift</strong><p>Der Text wurde hier eingefügt</p>";
    $staticbox_text_html = utf8_encode($staticbox_text);
    $staticbox_content = $wpdb->get_col( "SELECT content FROM staticbox" );
    foreach($staticbox_content as $item) {echo $item;}
}

/*** DIE BACKEND FUNKTIONEN ***/
function staticbox_admin() {
    global $wpdb;   ?>
    <div class="wrap">
        <h2>Staticbox</h2>
        <p>Hier k&ouml;nnen Sie der Staticbox Inhalt geben, welcher dann im Frontend ausgegeben wird.</p>
        <p>Dies erfolgt durch das Einspeisen folgender Funktion an einer beliebigen Stelle im Theme des Blogs:</p>
        <code>
            if ( function_exists ( staticbox() ) ) {
                staticbox();
            }
        </code>
        <p>&nbsp;</p>
        <h3>Inhalt einf&uuml;gen</h3>
        <form action="?page=staticbox/main.php" method="post">
            <textarea name="staticbox_content" cols="100" rows="10"><?php $staticbox_content = $wpdb->get_col( "SELECT content FROM staticbox" );   foreach($staticbox_content as $item) {echo $item;} ?></textarea><br />
            <input type="submit" name="staticbox_submit" />
        </form>
        <?php echo $_POST['staticbox_content']; ?>
    </div>

    <?php
    if(isset($_POST['staticbox_submit'])) {
        $sql = "UPDATE staticbox SET content = '".$_POST['staticbox_content']."';";
        if(!$wpdb->query($sql)) {echo '<p style="color:#990000;">Fehlgeschlagen.</p>';} else {echo '<p style="color:#009900;">Ausgef&uuml;hrt.</p>';}   
    }

    echo $sql;
}

/*** DIE MENÜS WERDEN HINZUGEFÜGT ***/
function staticbox_menu() {
  add_menu_page('Staticbox', 'Staticbox', 10, __FILE__, 'staticbox_admin');
  //add_submenu_page(__FILE__, 'Bloggerei', 'Bloggerei', 10, 'bloggerei', 'bloggerei');
}
add_action('admin_menu', 'staticbox_menu');

?>

0 个答案:

没有答案