如何将自定义表单提交发送到WordPress数据库?

时间:2021-09-01 作者:Coding Student

我已经在数据库中创建了一个表单和表,希望将提交的内容发送到其中。根据我的研究,我发现此代码应将其发送到数据库:

<?php /* PHP code that processes form */
    if(isset($_POST[\'submit\'])){
        
        global $wpdb;
        $tablename = Form_Submissions;
      
        $data=array(
            \'FirstName\' => $_POST[\'fname\'],
            \'LastName\' => $POST[\'lname\'],
            \'Consent\' => $POST[\'consent\']
        );
        
        $format = array(
            \'%s\',
            \'%s\',
            \'%s\'
        );
      
        $wpdb -> insert($tablename, $data, $format);

        echo \'<script type="text/javascript"> document.getElementById("frm").style.display="none"; </script>\';
    }
?>
然而,什么都没有发生。我完全不知道接下来该怎么办。我是错过了一些简单的事情,还是完全偏离了目标?谢谢你的帮助!

这是表单的代码和激活表单的代码,以防万一。

<html>
<style>

/* Stylings for Heading Statement */
    .h3{
        text-align: center;
        font-size: 24px;
        padding: 10px;
        color: white;
    }
/* Stylings for checkbox */
    .box{
        font-size: 22px;
        padding: 20px;
        color: white;
    }
/* Stylings for input labels (names and checkbox labels) */
    .input-label{
        text-align: center;
        font-size: 22px;
        padding: 10px;
        color: white;
    }
/* Button used to open the contact form - fixed at the bottom of the page */
    .open-button {
        width: 170px; 
        height: 60px; 
        background-color: transparent; 
        outline: none; 
        position: relative; 
        left: -168px; 
        top: 20px;
    }
/* The popup form - hidden by default */
    .form-popup {
        display: none;
        position: absolute;
        top: 25%;
        left: -530px;
        border: none;
        z-index: 9;
        background-color: #1e1e1e;
        width: 1000px;
        padding: 20px;
        border-radius: 20px;
        text-align: center;
    }
/* Sets style and size of input fields  */
    .form-popup input[type=text] {
        width: 30%;
        padding: 5px;
        margin: 5px 0 22px 0;
        border: none;
        background: #FFFFFF;
    }
/* Sets color and style of the input boxes when selected */
    .form-popup input:focus {
        background-color: #FFFFFF;
        outline: none;
    }
/* Stylings of the submit button */
    .btn {
        background-color: #DB2600;
        border-radius: 9999px;
        font-size: 24px;
        width: 120px;
        height: 45px;
        transition: 0.3s;
        color: white;
    }
/* Changes the color of the sugmit button when curser hovers over it */
    .btn:hover {
        opacity: 1;
        background-color: #000000;
    }
/* Hides the default checkbox */
    .box input {
        position: absolute;
        opacity: 0;
        cursor: pointer;
        height: 0;
        width: 0;
    }
/* creates custom checkbox */
    .checkmark {
        position: absolute;
        height: 20px;
        width: 20px;
        background-color: white;
        border-radius: 2px;
    }
/* Sets the color of checkbox when checked */
    .box input:checked ~ .checkmark {
        background-color: #DB2600;
    }
/* creates checkmark inside checkbox */
    .checkmark:after {
        content: "";
        position: absolute;
        display: none;
    }
/* Displays checkmark when checkbox is clicked */
    .box input:checked ~ .checkmark:after {
        display: block;
    }
/* Positions and styles checkmark */
    .box .checkmark:after {
        left: 7px;
        top: 3px;
        width: 6px;
        height: 12px;
        border: solid white;
        border-width: 0 3px 3px 0;
        -webkit-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
    }
/* Column formatting for checkbos and statement. This is what is aligning the two items. */
    .column1 {
        float: left;
        width: 26%;
    }
    .column2 {
        float: left;
        width: 74%;
        margin-top:-5px;
    }
/* Clears floats after the columns */
    .row:after {
        content: "";
        display: table;
        clear: both;
    }

</style>
<body>

<!-- Code for the form -->

    <div class="form-popup" id="Form"> 
        <form method="post">
        
        <!-- acknolwdgement statment -->
              <h3 class="h3">form statement</h3> 

        <!-- label and input for First Name -->
              <label for="fname" class="input-label">First name:</label>
              <input type="text" name="fname" required/>

        <!-- label and input for Last Name -->
              <label for="lname" class="input-label">&nbsp &nbsp Last name:</label>
              <input type="text" name="lname" required/> <br> <br>

        <!-- checkbox and label, placed to columns to achieve desired formating -->
              <div class="row">
                  <div class="column1" style="text-align: right;">
                      <label  class="box" style="border-color: red;">  
                      <input type="checkbox" name="consent" value="agree" class="box" required/>
                      <span class="checkmark"></span>
                      </label></div>
                  <div class="column2" style="text-align: left;">
                      <label class="input-label"> I have read and understand the above statement.
                      </label>
                  </div>
              </div>
              <br><br>  

        <!-- Submit Button -->
              <input type="submit" name="submit" value="Submit" class="btn">

        </form>
    </div>

<script>
    function openForm() { /* Opens Consent Form */
        document.getElementById("Form").style.display = "block";
    }
</script>

</body>


<?php /* PHP code that processes form */
    if(isset($_POST[\'submit\'])){
        
        global $wpdb;
        $tablename = Form_Submissions;
      
        $data=array(
            \'FirstName\' => $_POST[\'fname\'],
            \'LastName\' => $POST[\'lname\'],
            \'Consent\' => $POST[\'consent\']
        );
        
        $format = array(
            \'%s\',
            \'%s\',
            \'%s\'
        );
      
        $wpdb -> insert($tablename, $data, $format);

        echo \'<script type="text/javascript"> document.getElementById("frm").style.display="none"; </script>\';
    }
?>
</html>
激活它的开关盒:

<?php /* code for pop-up button conditions */
$host = \'https://\' .$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; /* gets the current page\'s URL */
switch ( $host ){
  case \'https://example1.com\': 
  case \'https://example2.com\': 
  case \'https://example3.com\':
    require \'pop-up-button.php\'; /*calls the PHP file containing the code for the form*/
    echo \'<button id="close" class="open-button" onclick="openForm()"></button>\'; 
    break; 
  default : /* the default case leaves the function and allows all pages with URL\'s not match the cases to run as normal pages */
    break; 
}; ?>

2 个回复
最合适的回答,由SO网友:tiago calado 整理而成

现在已经看到,lastname和approve使用$POST代替$\\u POST,$POST它将是一个php变量,正确的是$\\u POST。也许错误一直都是这样。

无论如何,如果with query不工作,或者sql语句不正确,或者与数据库没有连接,则with query必须工作

global $wpdb;
$wpdb->query($wpdb->prepare("update pro_slider set pos = {$d} where id = {$f->id};"));
这是我用来更新横幅的东西。对于您的情况,应该使用insert语句。

global $wpdb;
$wpdb->query($wpdb->prepare("insert into {$table_name} values ( 0 , 0, 0, 0  ) ")); 
只需将零替换为所需的值,我会在使用硬代码值之前尝试,如果可行,一切都很好,只需将硬代码(写的值)替换为变量,只需记住varchar可能需要comas,有时sql语句有点奇怪,直接在sql语句上使用$\\u POST,有时需要在php变量上使用它。

好的,这就是我要一步一步来检查问题的方法。

global $wpdb;
$first_name = $_POST[\'fname\'];
$LastName = $_POST[\'lname\'];
$Consent = $_POST[\'consent\'];
$sql = "insert into {$table_name} values ( {$first_name} , {$LastName}, {$Consent} )";
echo \'<pre>\';
print_r($sql);
echo \'</pre>\';
die();
这应该打印如下内容

插入自定义表格值(\'Brad\',\'Pitt\',1)

在这种情况下,statment是正确的,但是如果它像这样打印

插入自定义表格值(Brad,Pitt,1)

这不正确,Brad不是varchar,需要介于comas之间,因此让我们在代码中插入comas:

global $wpdb;
$first_name = $_POST[\'fname\'];
$LastName = $_POST[\'lname\'];
$Consent = $_POST[\'consent\'];
$sql = "insert into {$table_name} values ( \'{$first_name}\' , \'{$LastName}\', \'{$Consent}\' )";
echo \'<pre>\';
print_r($sql);
echo \'</pre>\';
die();
其中一个很好,我相信是第一个,现在只需复制$sql并在wpdb中使用它->;查询

global $wpdb;
$first_name = $_POST[\'fname\'];
$LastName = $_POST[\'lname\'];
$Consent = $_POST[\'consent\'];
$wpdb->query($wpdb->prepare("insert into {$table_name} values ( {$first_name} , {$LastName}, {$Consent} ) "));

SO网友:tiago calado

插入模式

global $wpdb;
$FirstName = $_POST[\'fname\'];
$LastName = $_POST[\'lname\'];
$Consent = $_POST[\'consent\'];
$table_name = $wpdb->prefix . \'Form_Submissions\';
$wpdb->insert($table_name, array(\'FirstName\' => $FirstName, \'LastName\' => $LastName, \'Consent\' => $Consent));
根据您的表,如果它们是varchar或不是varchar,变量有时需要介于coma之间,就像这样。我做这个没有测试,阵列可能不需要这个,不过。。。

global $wpdb;
$FirstName = $_POST[\'fname\'];
$LastName = $_POST[\'lname\'];
$Consent = $_POST[\'consent\'];
$table_name = $wpdb->prefix . \'Form_Submissions\';
$wpdb->insert($table_name, array(\'FirstName\' => "$FirstName", \'LastName\' => "$LastName", \'Consent\' => "$Consent"));