2012-04-19 19 views
2

私はこれを試してきましたが、成功しませんでした。私はPHP、HTML、JavaScript、MySQLを使用しています。ここに私のHTMLコードがあります:動的に追加されたテキストボックス(JavaScript)から複数のデータをMySQLデータベースに保存する方法は?

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

追加ボタンをクリックすると、別のテキストボックスが表示され、別の名前を入力します。ここに私のJavaScriptのである:

ここ
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--; 
    } 
} 

は私のPHPコードは次のとおりです。

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!"; 

私は多くの人が、これは配列を使って仕事を得ることができます聞いたが、私は、データベースに格納されたデータはArrayです。私が作成したテキストボックスの数は関係ありません。ちょうどArrayです。

私は正しいアプローチを使用していますか?このデータの配列を1つのデータベースフィールドに保存する必要がありますか?

+0

GeorgioCZY、私はこれを3回読みましたが、私はまだあなたが求めているものを理解できません。別のテキストフィールドを表示しますか?それは現れていないのですか?また、コードにはいくつかの書式設定の問題があります。改訂してください、そして、はるかに手助けするでしょう。 –

+0

すべてのデータを1つのフィールドに保存したい場合は、データのインボード( '、'、$ _POST [Author])を暗黙指定する必要があります。 –

+0

http://stackoverflow.com/questions/1978438/save-php-array-to-mysql –

答えて

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

mysql_select_db($database, $con); 

$authors = $_POST['Author']; 
foreach($authors as $author) : 
    $sql="INSERT INTO savetest (type, number) VALUES ('{$_POST['type']}','{$author}')"; 

    if (!mysql_query($sql,$con)){ 
     die('Error: ' . mysql_error()); 
    } 
endforeach; 
+0

これは非常にうまくいった!どうもありがとうございます! – GeorgioCZY

+0

これはあなたの質問に答えていますが、ユーザー入力をSQLクエリに直接入力しないでください。 http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-phpを参照してください。 –

関連する問題