将多行发布到sql中?

yeotifhr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(345)

因此,我有一个表格,我需要编辑,以便它可以提交多行,目前我有一个工作的php提交到sql中的数据(见下面的代码),但我现在添加了一个js脚本,重复另一个'代理'的输入字段行唯一的问题是当提交的形式只提交最后一个'代理'输入。
我知道这与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>
xxhby3vn

xxhby3vn1#

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

<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 值并将其插入表中。

相关问题