douzi4766 2015-02-06 12:26
浏览 16
已采纳

使用wordpress将页面上的许多输入中的一个提交到mysql数据库

At current I managed to get a form on a wordpress page to submit data to a mysql database (a very large victory for me; not big into php/coding)

This is the code I have been using, but need help changing:

<form method="post">
Value1: <input type="text" name="field1" />
Value2: <input type="text" name="field2" />
Value3: <input type="text" name="field3" />
Value4: <input type="text" name="field4" />
Value5: <input type="text" name="field5" />
<input type="submit" /></form>


$newdb = new wpdb( 'username', 'password', 'database', 'localhost' );

$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$field3 = $_POST['field3'];
$field4 = $_POST['field4'];
$field5 = $_POST['field5'];

$newdb->insert("test", array(
"field1" => $field1,
"field2" => $field2,
"field3" => $field3,
"field4" => $field4,
"field5" => $field5
   ));

As you can see, this causes all the inputs on the page to be submitted, and ends up creating blank cells in my table rows when there is no data inserted into the fields.

I need to be able to submit each input separately, hopefully without creating a seperate php script for each input, and without entering blank cells in my table.

  • 写回答

1条回答 默认 最新

  • dongyuanguang3893 2015-02-06 17:32
    关注

    If you want o avoid inserting blanks, you just need to check each input before inserting it.

    For example,

    $inserts = array();
    for ($i = 1; $i < 6; $i++) {
        $fieldName = 'field' . $i . '-name';
        if ($_POST[$fieldName]) {
            $inserts[$field-name] = $_POST[$fieldName];
        }
    } 
    $newdb->insert("test", $inserts);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记