2015-02-06 12:26
浏览 15


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);
    点赞 评论

相关推荐 更多相似问题