从自定义表单

时间:2015-11-29 16:11:39

标签: sql wordpress forms

Insert data into Wordpress database table from a custom form

我已经使用过您的示例,但我遇到了问题。我一开始有错误。我收到这条消息:

  

WordPress数据库错误:[无法添加或更新子行:异类   键约束失败(prowebex_barbara.barbara_schedule,CONSTRAINT   barbara_schedule_ibfk_1 FOREIGN KEY(day_id)参考文献barbara_days   (id_day))]

INSERT INTO barbara_schedule (id, challenger, challenged,
 date_match, day_id, term_start, term_end) VALUES ('', 'gfdgdfgdfg', 'gdfgfdgdf', '2015-11-30', '', '23:00', '00:59');

提交的数据

你能告诉我怎么改变这个。我已经截断表,删除两个表。我不知道该怎么做。

       <?php
       if(isset($_POST['submit'])) {
           global $wpdb;

           $id = sanitize_text_field($_POST['id']);
           $challenger = sanitize_text_field($_POST['challenger']);
           $challenged = sanitize_text_field($_POST['challenged']);
           $date_match = sanitize_text_field($_POST['date_match']);
           $day = sanitize_text_field($_POST['day_id']);
           $term_start = sanitize_text_field($_POST['term_start']);
           $term_end = sanitize_text_field($_POST['term_end']);

           $table_name = $wpdb->prowebex_barbara.barbara_schedule;

           $wpdb->insert($table_name, array(
               'id' => $id,
               'challenger' => $challenger,
               'challenged' => $challenged,
               'date_match' => $date_match,
               'day_id' => $day,
               'term_start' => $term_start,
               'term_end' => $term_end,
           ),
               array( '%s', '%s', '%s', '%s', '%s', '%s', '%s')
           );
           $msg = "Data Submited";
           echo $msg;
       }?>

        <form action="" method="post" id="subForm">
            <div>
                <input type="hidden" name="id" id="iiihuu-iiihuu" placeholder="" />
                <br>
                <input type="text" name="challenger" id="iiihuu-iiihuu" placeholder="Izazivač" />
                <br>
                <input type="text" name="challenged" id="iiihuu-iiihuu" placeholder="Izazvani" />
                <br>
                <input type="date" name="date_match" id="iiihuu-iiihuu" placeholder="Datum" />
                <br>
                <select>
                       <option>I don't know how to do this</option>
                </select>
                <br>
                <input type="time" name="term_start" id="iiihuu-iiihuu" placeholder="Termin od" />
                <br>
                <input type="time" name="term_end" id="iiihuu-iiihuu" placeholder="Termin do" />
                <br>
                <input type="submit" name="submit" value="Spasi" class ="submit_button" id="formSubmit" />
            </div>
        </form>
    </div>

1 个答案:

答案 0 :(得分:0)

错误看起来自定义表实际上彼此之间存在关系,不确定数据库结构是如何的,但是为了在barbara_schedule中创建记录,您还必须在barbara_days中创建记录。 / p>

因此,为了检查是否是这种情况,您应该传递假的day_id。因此,将$ _POST ['day_id']替换为barbara_days表中已存在的现有day_id。

但如果不是这样,那么这里有一个快速思考,你在这个文件的顶部有一个require语句吗? 通常这是为了在元表之外的自定义表上进行插入。

require_once('LINK-TO/wp-load.php');

其次我不确定,这是什么输出

$table_name = $wpdb->prowebex_barbara.barbara_schedule;

但是以防万一,尝试像这样对

这样的表名进行硬编码
$table_name = $wpdb->prefix."table-name"