将多行发布到SQL中?

时间:2018-01-19 07:13:17

标签: php mysql forms

所以我有一个我需要编辑的表单,所以它可以提交多行,目前我有一个工作的PHP将数据提交到SQL中(参见下面的代码)但是我现在添加了一个重复了另一个代理人的输入字段行#39;唯一的问题是,提交表单时只提交最后一个代理商'进入。

我知道它与PHP而不是html有关,如果有2个代理商,我需要更改它以提交所有字段/行。或者200名代理商'。

谢谢。

(Ps im通过服务器上的另一个.php连接到数据库,与SQL的连接很好)。

PHP:     

# DEBUG 
#ini_set('display_startup_errors', 1);
#ini_set('display_errors', 1);
#error_reporting(E_ALL);

if (!isset($_POST['post_agent_name'])) header('Location: /');

// get database class
require_once('database.php');
$db = DataBase::SharedInstance();

// get fields
$name = $_POST['post_agent_name'];
$email = $_POST['post_agent_email'];
$location = $_POST['post_agent_location'];
$tel = $_POST['post_agent_telephone'];
$brand = $_POST['post_agent_brand'];

// do database request
$query = "INSERT INTO agents (agent_name, agent_email, agent_location, agent_telephone, agent_brand) VALUES ('%s', '%s', '%s', '%s', '%s');";
$db_result = $db->dbQuery($query, $name, $email, $location, $tel, $brand);

// handle response
if ($db_result['affected_rows']) {
    echo "<script type='text/javascript'>window.location.href = 'http://whostheagent.com/thank-you.html';</script>";
    // or header('Location: /thank-you.html');
} else {
    echo "Failed to add Agent! Please contact support";
}

Html

<section class="post">
  <header class="major">
    <h2>Add your Agents
    </h2>
  </header>
  <!-- Form -->
  <form method="post" action="agent_signup_sql.php" class="alt">
    <div class="row uniform">
      <div class="12u$ 12u$(xsmall)">
        <hr>
        <h5>Agent Details
        </h5>
      </div>
      <div id="agent" class="12u$">
        <div class="row" style="padding-bottom:10px">
          <div class="1u 1u(xsmall)">
            <select name="post_agent_title" id="agent-title">
              <option value="Mr">Mr
              </option>
              <option value="Mrs">Mrs
              </option>
              <option value="Miss">Miss
              </option>
              <option value="Ms">Ms
              </option>
            </select>
          </div>
          <div class="3u 12u(xsmall)">
            <input type="text" name="post_agent_name" id="agent-name" placeholder="Agent Name" />
          </div>
          <div class="4u 12u$(xsmall)">
            <input type="email" name="post_agent_email" id="agent-email" placeholder="Email" />
          </div>
          <div class="2u 12u$(xsmall)">
            <input type="text" name="post_agent_telephone" id="agent-telephone" placeholder="Telephone" />
          </div>
          <div class="2u$ 12u$(xsmall)">
            <input type="text" name="post_agent_location" id="agent-location" placeholder="location" />
          </div>
        </div>
      </div>
      <div class="12u$ 12u$(xsmall)">
        <a style="font-size: 10px;float:right;cursor:not-allowed;opacity:0.3;">add another agent
        </a>
        <!--id="addagent"-->
      </div>
      <div class="12u$ 12u$(xsmall)">
        <h5>Brand working for
        </h5>
        <input type="text" name="post_agent_brand" id="brand" placeholder="brand working for" />
      </div>
      <!-- Break -->
      <div class="12u$">
        <ul class="actions">
          <center>
            <br>
            <li>
              <input type="submit" value="Submit Agent" class="special" />
            </li>
          </center>
        </ul>
      </div>
    </div>
  </form>
</section>

1 个答案:

答案 0 :(得分:4)

您需要创建输入数组以提交多个字段。例如,

<input type="text" name="post_agent_name[]" placeholder="Agent Name" />
<input type="text" name="post_agent_name[]" placeholder="Agent Name" />

然后在PHP端,将特定字段的POST值检索为数组。例如,

echo $_POST['post_agent_name'][0]; // would output first agent name

echo $_POST['post_agent_name'][1]; // would output second agent name and so on.

在此之后,您必须遍历POST值并将其插入表格中。