Wie speichere ich mehrere Daten aus dynamisch hinzugefügten Textfeldern (JavaScript) in einer MySQL-Datenbank?

Ich habe es versucht, aber keinen Erfolg. Ich benutze PHP, HTML, JavaScript und MySQL. Hier ist mein HTML-Code:

<code><div id="Author">
    <LI>Author</LI> 
    <input type = "text" name="Author[]" value = "1. "/>
    <input type="button" value="+" id="Authorbutton" onclick="addAuthor()" />
</div>
</code>

Wenn Sie auf die Schaltfläche "Hinzufügen" klicken, wird ein anderes Textfeld angezeigt, und Sie können einen anderen Namen eingeben. Hier ist mein JavaScript:

<code>var counter3 = 0;
function addAuthor() {
    // Get the main Div in which all the other divs will be added
    var mainContainer = document.getElementById('Author');

    // Create a new div for holding text and button input elements
    var newDiv = document.createElement('div');

    // Create a new text input
    var newText = document.createElement('input');
    newText.type = "text";
    //var i = 1;
    newText.name = "Author[]";
    newText.value = counter3 + 2 + ". ";

    //Counter starts from 2 since we already have one item
    //newText.class = "input.text";
    // Create a new button input
    var newDelButton = document.createElement('input');
    newDelButton.type = "button";
    newDelButton.value = "-";

    // Append new text input to the newDiv
    newDiv.appendChild(newText);
    // Append new button input to the newDiv
    newDiv.appendChild(newDelButton);
    // Append newDiv input to the mainContainer div
    mainContainer.appendChild(newDiv);
    counter3++;
    //i++;

    // Add a handler to button for deleting the newDiv from the mainContainer
    newDelButton.onclick = function() {
        mainContainer.removeChild(newDiv);
        counter3--;
    }
}
</code>

Hier ist mein PHP-Code:

<code>if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($database, $con);



$sql="INSERT INTO savetest (type, number)
VALUES
('$_POST[type]','$_POST[Author]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
echo "Thank you for submitting your details!";
</code>

Ich habe gehört, dass viele Leute dies mithilfe von Arrays zum Laufen bringen können, aber die Daten, die ich in der Datenbank gespeichert habe, sindArray. Es spielt keine Rolle, wie viele Textfelder ich gerade erstellt habeArray.

Wende ich den richtigen Ansatz an? Soll ich dieses Datenarray in einem Datenbankfeld speichern?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage