从表单中获取值并保存在数据库中,但插入空格而不是表单值

时间:2019-08-22 作者:STBox
<?php /* Template Name: Dummy Practice Page*/?>
<div id="main-content" class="main-content">
    <div class="main-content-inner">
            <form method="post">
            <p><div>
                <input name="nametxt" id="nametxt" type="text" style="height:30px; width: 350px; " maxlength="5" placeholder="Name" required><br>
            </p></div>
            <p><div>    
                <input name="designationtxt" id="designationtxt" type="text" style="height:30px; width: 350px; " maxlength="50" placeholder="Designation" required><br>
            </p></div>
            <p><div>
                <input name="designationtxt" id="descriptiontxt" type="text" style="height:30px; width: 350px; " maxlength="1000" placeholder="Description" required><br>
            </p></div>

                <input id="submitbtn" type=\'submit\' style="height:40px; width: 130px; padding:10px; color:dodgerblue; background-color:black; border-radius:20px;   " name=\'Submit\' value=\'Add Member\' /><br><br>

            </form>
    </div>
</div>
<?php
     global $wpdb;
    $table = $wpdb->prefix.\'tboxteam\';
     $data = array(\'name\' =>  isset($_POST[\'nametxt\']), \'designation\' =>  isset($_POST[\'designationtxt\']), \'description\' =>  isset($_POST[\'descriptiontxt\']));
     $format = array(\'%s\',\'%s\',\'%s\');
     $wpdb->insert($table,$data,$format);
?>
2 个回复
最合适的回答,由SO网友:Wordpress Dev 整理而成
<?php /* Template Name: Dummy Practice Page*/?>
<div id="main-content" class="main-content">
  <div class="main-content-inner">
        <form method="post">
        <p><div>
            <input name="nametxt" id="nametxt" type="text" style="height:30px; width: 350px; " maxlength="5" placeholder="Name" required><br>
        </p></div>
        <p><div>    
            <input name="designationtxt" id="designationtxt" type="text" style="height:30px; width: 350px; " maxlength="50" placeholder="Designation" required><br>
        </p></div>
        <p><div>
            <input name="descriptiontxt" id="descriptiontxt" type="text" style="height:30px; width: 350px; " maxlength="1000" placeholder="Description" required><br>
        </p></div>

            <input id="submitbtn" type=\'submit\' style="height:40px; width: 130px; padding:10px; color:dodgerblue; background-color:black; border-radius:20px;   " name=\'Submit\' value=\'Add Member\' /><br><br>

        </form>
    </div>
 </div>
 <?php
 // Checking if form submitted 
 if(isset($_POST[\'Submit\'])){

       // Fetching values if they are not empty.
    if( isset($_POST[\'nametxt\']) || isset($_POST[\'designationtxt\']) ||isset($_POST[\'descriptiontxt\']) ){

        global $wpdb;

        $data = array(\'name\' =>  $_POST[\'nametxt\'], \'designation\' =>  $_POST[\'designationtxt\'], \'description\' =>  $_POST[\'descriptiontxt\'] );


        $table = $wpdb->prefix.\'tboxteam\';

        $format = array(\'%s\',\'%s\',\'%s\');

        $wpdb->insert($table,$data,$format);
    }
 }

 ?>
SO网友:James John

PHP isset返回布尔值以检查数组索引是否存在。https://www.php.net/manual/en/function.isset.php

"name" => isset($_POST["nametxt"]) ? $_POST["nametxt"] : ""

这是你应该做的

相关推荐

Wordpress Database Cleanup

重建WordPress数据库和更改网站的目录结构有多困难?我继承了两个网站的维护工作,我很确定目录结构设置不正确,数据库一团糟(两个网站有6个数据库?)。我是WordPress的新手,我怀疑现在使用现有网站重建网站会更容易。丢失数据没什么大不了的,网站也没什么特别的。该网站运行正常,但维护起来越来越困难。这是一个合理的计划吗?