Dave
Dave

Reputation: 121

Post Multiple Rows into SQL?

So I have a form I need to edit so it can submit multiple rows, at the moment I have a working PHP that submits the data into the SQL (see code below) however I have now added a JS script that repeats the row of input fields for another 'agent' the only problem is when submitting the form only submits the last 'agent' entered.

I know its something to do with the PHP rather than the html, what would I need to change to have it submit all fields / rows be it if there are 2 'agents' or 200 'agents'.

Thank you.

(Ps im connecting to the database via another .php for on the server, the connection to the SQL is fine).

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>

Upvotes: 1

Views: 226

Answers (1)

Samir Selia
Samir Selia

Reputation: 7065

You need to create array of inputs to submit multiple fields. For e.g.

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

Then at PHP side, retrieve POST values for a particular field as an array. For e.g.

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.

After this, you have to loop through POST values and insert it into your table.

Upvotes: 4

Related Questions