簡體   English   中英

如何在數組中使用具有不同名稱的多個文本框在mysql中插入多行

[英]How can I use multple textbox with different names in array to to insert multiple rows in mysql

我有不同名稱的HTML文本框,例如全名,學校代碼,分數。 這些文本框是通過對我的表的查詢生成的。 從返回的結果中,我想將它們的值插入多行中。

下面的代碼是我到目前為止嘗試過的

/* below code display the form input tags in this form */
$sql = $xconnect->prepare("select * from table group by(schoolcode)");
$sql->execute();
$count = $sql->rowCount();
if ($count == 0) {
    echo "No result found";
} else {
    echo '<form method="POST" action="">';
    while ($row = $sql->fetch()) {
        echo '<input type="text" value="'.$row["fullname"].
        '" name="fullname[]">'.
        '<input type="hidden" value="'.$row["schoolcode"].
        '" name="schoolcode[]">'.
        '<input type="text" value="" name="score[]">'.
        "<br><br>";
    }
    echo ' <input type="submit" name="submit"> </form>';
}

//and this other code below is to insert the values to multiple rows in Mysql

if (isset($_POST["submit"])) {
    $fullname = "";
    $schoolcode = "";
    $score = "";
    if (!empty($_POST['score'])) {
        $fullname = $_POST['fullname'];
        $schoolcode = $_POST['schoolcode'];
        $score = $_POST['score'];
        for ($i = 0; $i < sizeof($fullname); $i++) {
            $sql2 = $xconnect->prepare("insert into table(fname, scode, score) values (:f, :c, :s)");
            $sql2->execute(array(":f" => $fullname[$i], ":c" => $schoolcode[$i], ":s" => $score[$i], ));
            echo "success";
        }
    }
}

預期結果將根據與學生的全名和學校代碼相對應的輸入分數,以以下格式顯示

fullname | schoolcode | score  
---------|------------|------- 
Ken      | 743627373  | 60     
---------|------------|------- 
Jude     | 876735836  | 40     
---------|------------|------- 
Anne     | 657556864  | 70     
---------|------------|------- 
Mike     | 685675112  | 30     
---------|------------|------- 

也許這會幫助你

if (isset($_POST["submit"])) {
$fullname = "";
$schoolcode = "";
$score = "";
if (!empty($_POST['score'])) {
    $fullname = $_POST['fullname'];
    $schoolcode = $_POST['schoolcode'];
    $score = $_POST['score'];
    echo "<table border='1'>";
    for ($i = 0; $i < sizeof($fullname); $i++) {
        $sql2 = $xconnect->prepare("insert into table(fname, scode, score) values (:f, :c, :s)");
        $sql2->execute(array(":f" => $fullname[$i], ":c" => $schoolcode[$i], ":s" => $score[$i], ));
        echo "<tr><td>".$fullname[$i]."</td><td>".$schoolcode[$i]."</td><td>".$score[$i]."</td></tr>";
    }
    echo "</table>";
}
}

暫無
暫無

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

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