【问题标题】:How do i add multiple records to my database如何将多条记录添加到我的数据库
【发布时间】:2017-10-10 19:50:48
【问题描述】:

一直在处理表格。我想将表单中的信息添加到数据库中,这样我就可以在网站上其他地方的日历中显示它。

我在网上找到了一些其他代码,但仅适用于 1 个值。并且由于某种原因只有 1 次。 现在有了这个(顺便说一句在wordpress中工作)

function elh_insert_into_db() {

global $wpdb;
// creates my_table in database if not exists
$table = $wpdb->prefix . "table_form"; 
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(9) NOT NULL AUTO_INCREMENT,
    `name` text NOT NULL,
    `email` text NOT NULL,
    `date` text NOT NULL,
    `starttijd` text NOT NULL,
    `eindtijd` text NOT NULL,
    `opmerkingen` text NOT NULL,
UNIQUE (`id`)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
// starts output buffering
ob_start();
?>
<form action="#v_form" method="post" id="v_form">
    <label for="visitor_name"><h3>Naam:</h3></label>
        <input type="text" name="visitor_name" id="visitor_name" />

<label for="visitor_email"><h3>E-mail:</h3></label>
    <input type="email" name="visitor_email" id="visitor_email" />

<label for="visitor_date"><h3>Datum:</h3></label>
    <input type="date"  name="visitor_date" id="visitor_date" />

<label for="visitor_start_time"><h3>Starttijd:</h3></label>
    <input type="time"  name="visitor_start_time" id="visitor_start_time" />

<label for="visitor_end_time"><h3>Eindtijd:</h3></label>
    <input type="time"  name="visitor_end_time" id="visitor_end_time" />

    <label for="visitor_text"><h3>Opmerkingen:</h3></label>
        <input type="text" name="visitor_text" id="visitor_text" />

    <input type="submit" name="submit_form" value="Aanvragen" />
</form>
<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" ) {
$table = $wpdb->prefix."table_form";
$name = strip_tags($_POST["visitor_name"], "");
$wpdb->insert( 
    $table, 
    array( 
        'name' => $name
    )
);


    $html = "<p>check this is inserted in the database <strong>$name, $email, $date , $starttijd, 
$eindtijd, $opmerkingen</strong> what a succes!</p>";
}
// if the form is submitted but the name is empty
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
    $html .= "<p>You need to fill the required fields.</p>";
// outputs everything
return $html;

}
// adds a shortcode you can use: [insert-into-db]
add_shortcode('elh-db-insert', 'elh_insert_into_db');

【问题讨论】:

  • 所以这是有效的,还是无效?
  • 它现在不工作
  • 什么不起作用?它应该做什么以及它实际上在做什么?
  • 应该将表单中的信息放入名为“table_form”的数据库中。此时它只将名字放入这个数据库,并且也只用于第一次输入。如果您第二次填写表格,它不会做任何事情

标签: php mysql database wordpress


【解决方案1】:

您只需处理/插入名称(形成您的代码):

$name = strip_tags($_POST["visitor_name"], "");    
$wpdb->insert( 
    $table, 
    array( 
        'name' => $name
    )
)

修改这部分代码以添加其他信息。您的 $_POST 中的每个项目都需要处理。

表单的良好起点(还有很多其他东西!):https://www.w3schools.com/php/php_forms.asp

【讨论】:

    【解决方案2】:
        function elh_insert_into_db() {
        global $wpdb;
        // creates nizam_table in database if not exists
        $table = $wpdb->prefix . "nizam_table"; 
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE IF NOT EXISTS $table (
            `id` mediumint(9) NOT NULL AUTO_INCREMENT,
            `name` text NOT NULL,
            `email` text NOT NULL,
        UNIQUE (`id`)
        ) $charset_collate;";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        // starts output buffering
        ob_start();
        ?>
        <form action="#v_form" method="post" id="v_form">
            <label for="visitor_name"><h3>Hello there! What is your name?</h3></label>
            <input type="text" name="visitor_name" id="visitor_name" />
            <input type="text" name="visitor_email" id="visitor_email" />
            <input type="submit" name="submit_form" value="submit" />
        </form>
        <?php
        $html = ob_get_clean();
        // does the inserting, in case the form is filled and submitted
        if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] && $_POST["visitor_email"]  != "" ) {
            $table = $wpdb->prefix."nizam_table";
            $name = strip_tags($_POST["visitor_name"], "");
            $email = strip_tags($_POST["visitor_email"], "");
            $wpdb->insert( 
                $table, 
                array( 
                    'name' => $name,
                    'email' => $email
                )
            );
            $html = "<p>Your name <strong>$name</strong> was successfully recorded. Thanks!!</p>";
        }
        // if the form is submitted but the name is empty
        if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] == "" )
            $html .= "<p>You need to fill the required fields.</p>";
        // outputs everything
        return $html;     
    }
    // adds a shortcode you can use: [insert-into-db]
    add_shortcode('elh-db-insert', 'elh_insert_into_db');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      相关资源
      最近更新 更多