簡體   English   中英

如何將行數可變的表單中的數據添加到MySQL表中?

[英]How do I add data from a form with a variable number of rows into a MySQL table?

我在網頁上的兩個部分中都有一個表單。 每個部分包含可變數量的行(用戶可以添加更多行)。 我有一個PHP頁面,該頁面將表單作為電子郵件處理,但是現在我希望對此進行擴展,以便將信息添加到已有的My SQL數據庫中。 下面是表單和php處理頁面的代碼。

我已經開始在PHP頁面底部添加用於將數據提交到MySQL表的代碼,但是由於兩節中的行數是可變的,因此我不確定如何執行此操作,不勝感激為使它正常工作提供任何幫助。

這是表單的HTML:

<form method="post" name="booking" action="bookingengine.php">
    <fieldset>
        <h2>Waged/Organisation Rate</h2>
        <p>
            <input type="text" name="name[]">
            <input type="text" name="email[]">
            <input type="text" name="organisation[]">
            <input type="text" name="position[]">
        </p>
        <p><span class="add">Add person</span></p>
    </fieldset>

    <fieldset>
        <h2>Unwaged Rate</h2>
        <p>
            <input type="text" name="name2[]">
            <input type="text" name="email2[]">
        </p>
        <p><span class="add">Add person</span></p>
    </fieldset>

    <p><input type="submit" name="submit" id="submit" value="Submit and proceed to payment page" class="submit-button" /></p>

</form>

這是預訂engine.php:

<? include 'connection.php'; ?>

<?php

$emailFrom = "****";
$emailTo = "****";
$subject = "Booking for Soteria Conference";

$body = "The following people have booked for the Soteria Conference in Derby:" . "\n\n" . "Waged/Organisation Rate:" . "\n\n";
$row_count = count($_POST['name']);
$row_count2 = count($_POST['name2']);



for($i = 0; $i < $row_count; $i++)
{
  // variable sanitation...
  $name = trim(stripslashes($_POST['name'][$i]));
  $email = trim(stripslashes($_POST['email'][$i]));
  $organisation = trim(stripslashes($_POST['organisation'][$i]));
  $position = trim(stripslashes($_POST['position'][$i]));

  // this assumes name, email, and telephone are required & present in each element
  // otherwise you will have spurious line breaks. 
  $body .= "Name: " . $name . "    Email: " . $email . "  Organisation: " . $organisation . "   Position: " . $position . "\n\n";
}

$body .= "Unwaged Rate:" . "\n\n";

for($j = 0; $j < $row_count2; $j++)
{
  // variable sanitation...
  $name2 = trim(stripslashes($_POST['name2'][$j]));
  $email2 = trim(stripslashes($_POST['email2'][$j]));

  // this assumes name, email, and telephone are required & present in each element
  // otherwise you will have spurious line breaks. 
  $body .= "Name: " . $name2 . "    Email: " . $email2 . "\n\n";
}

// send email 
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=payment.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}

?>

這是connection.php的結構:

<?php

$hostname = "localhost";
$database = "****";
$username = "****";
$password = "****";

$conn = mysql_connect($hostname, $username, $password) or die ('Error connecting to mysql');
mysql_select_db($database);

?>

我不確定你是什么意思。 但是您的HTML代碼應該更像這樣:

<p>
  <input type="text" name="persons[][name]">
  <input type="text" name="persons[][email]">
  <input type="text" name="persons[][organisation]">
  <input type="text" name="persons[][position]">
</p>

然后在您的PHP腳本結尾

$values = array();
foreach ($_POST['persons'] as $person) {
    // Sanitize your datas
    ...
    // SQL VALUES TO INSERT
    $values[] = '(' . $person['name'] . ',' . $person['email'] . ',' . $person['organisation'] . ',' . $person['position'] . ')';
}
$query = "INSERT INTO person (name, email, organization, position) VALUES " . implode(',', $values);

相同的邏輯適用於未工資率

編輯是否要保留HTML

 $values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));
     $organisation = trim(stripslashes($_POST['organisation'][$i]));
     $position = trim(stripslashes($_POST['position'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "  Organisation: " . $organisation . "   Position: " . $position . "\n\n";

     //prepare the values for MySQL
     $values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';
}
$query = "INSERT INTO person (name, email, organization, position) VALUES " . implode(',', $values);

 And for unwaged rate

 $values = array();

 for($i = 0; $i < $row_count; $i++) {
     // variable sanitation...
     $name = trim(stripslashes($_POST['name'][$i]));
     $email = trim(stripslashes($_POST['email'][$i]));

     // this assumes name, email, and telephone are required & present in each element
     // otherwise you will have spurious line breaks. 
     $body .= "Name: " . $name . "    Email: " . $email . "\n\n";

     //prepare the values for MySQL
     $values[] = '(' . $name . ',' . $email . ')';
}
$query = "INSERT INTO person (name, email) VALUES " . implode(',', $values);

如果您需要多個輸入,建議您使用多維數組(當前正在執行,但效果不佳):

<fieldset id="1">
   <legend>Row 1</legend>
   <input type="text" name="name[1]"/>
   <input type="text" name="email[1]"/>
   <input type="text" name="address[1]"/>
</fieldset>
<fieldset id="2">
   <legend>Row 2</legend>
   <input type="text" name="name[2]"/>
   <input type="text" name="email[2]"/>
   <input type="text" name="address[2]"/>
</fieldset>

您可以使用JavaScript(如jQuery)追加更多行(例如,如果他們單擊按鈕)。 您可以使用字段集ID來填充新行的數組鍵。 這是我要使用的PHP:

foreach ($_POST['name'] as $row_number => $value){
   $query = 'INSERT INTO table (name' . $row_number . ', user_id) VALUES (' . $value . ', ' . $user_id . ')';  
}

如果您期望有很多行,則可以設置一個表,將每個名稱作為單獨的行(帶有user_id列)列出,而不是作為列出每個用戶行的表中的列列出。 因此,與其做:

user_id | name1 | name2 | name 3

1 | James | John | Jimbob
2 | Sandy | Samm | Sandra

你可以做:

id | order | user_id | value

1 | 1 | 1 | James
2 | 2 | 1 | John
3 | 3 | 1 | Jimbob
4 | 1 | 2 | Sandy
5 | 2 | 2 | Samm
6 | 3 | 2 | Sandra

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM