[請益] PHP 輸入欄位多筆資料

看板PHP作者 (mooncolor)時間11年前 (2013/07/22 18:13), 編輯推噓5(50101)
留言106則, 12人參與, 最新討論串1/1
想請問 要輸入兩筆資料,有新增欄位按鍵 如果在欄位輸入編號10和編號20這兩筆資料,從資料庫搜尋編號10和編號20的資料, 顯示在另一個網頁上,請問要怎麼編寫語法? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.128.90.10

07/22 18:19, , 1F
這已經不是語法的問題了。首先你得具備以下基本技能
07/22 18:19, 1F

07/22 18:20, , 2F
HTML,SQL了解GET跟POST的差異...
07/22 18:20, 2F

07/23 00:22, , 3F
就只是單純傳兩個數字告訴後台這兩個數字間的SCOPE
07/23 00:22, 3F

07/23 03:58, , 4F
[Database]
07/23 03:58, 4F

07/23 03:58, , 5F
Table: mydata {id int INDEX, data text UTF8}
07/23 03:58, 5F

07/23 03:58, , 6F
[insert.php]
07/23 03:58, 6F

07/23 03:58, , 7F
<?php
07/23 03:58, 7F

07/23 03:59, , 8F
$db = new PDO(
07/23 03:59, 8F

07/23 03:59, , 9F
"mysql:host=localhost;dbname=mydb",
07/23 03:59, 9F

07/23 03:59, , 10F
"root",
07/23 03:59, 10F

07/23 03:59, , 11F
"pass",
07/23 03:59, 11F

07/23 03:59, , 12F
array(
07/23 03:59, 12F

07/23 03:59, , 13F
PDO::MYSQL_ATTR_INIT_COMMAND
07/23 03:59, 13F

07/23 03:59, , 14F
=> "SET NAMES UTF8"
07/23 03:59, 14F

07/23 04:00, , 15F
)
07/23 04:00, 15F

07/23 04:00, , 16F
);
07/23 04:00, 16F

07/23 04:00, , 17F
if (isset($_POST["id"]) && isset($_POST["data"])) {
07/23 04:00, 17F

07/23 04:00, , 18F
foreach ($_POST["id"] as $i => $id) {
07/23 04:00, 18F

07/23 04:00, , 19F
$data = $_POST["data"][$i];
07/23 04:00, 19F

07/23 04:00, , 20F
$insert = $db->prepare(
07/23 04:00, 20F

07/23 04:01, , 21F
"INSERT INTO `mydata` (`id`, `data`)" .
07/23 04:01, 21F

07/23 04:01, , 22F
" VALUES (:id, :data)"
07/23 04:01, 22F

07/23 04:01, , 23F
);
07/23 04:01, 23F

07/23 04:01, , 24F
$insert->bindParam(":id", $id);
07/23 04:01, 24F

07/23 04:01, , 25F
$insert->bindParam(":data", $data);
07/23 04:01, 25F

07/23 04:02, , 26F
$insert->execute();
07/23 04:02, 26F

07/23 04:02, , 27F
}
07/23 04:02, 27F

07/23 04:02, , 28F
header("location: /");
07/23 04:02, 28F

07/23 04:02, , 29F
exit();
07/23 04:02, 29F

07/23 04:02, , 30F
}
07/23 04:02, 30F

07/23 04:02, , 31F
?>
07/23 04:02, 31F

07/23 04:02, , 32F
<!DOCTYPE html>
07/23 04:02, 32F

07/23 04:02, , 33F
<meta charset="utf-8" />
07/23 04:02, 33F

07/23 04:03, , 34F
<script>
07/23 04:03, 34F

07/23 04:03, , 35F
function more() {
07/23 04:03, 35F

07/23 04:04, , 36F
var insert = document.getElementById("insert");
07/23 04:04, 36F

07/23 04:04, , 37F
var breaker= document.createElement("br");
07/23 04:04, 37F

07/23 04:04, , 38F
var label1 = document.createElement("label");
07/23 04:04, 38F

07/23 04:04, , 39F
var text1 = document.createTextNode("ID:");
07/23 04:04, 39F
還有 27 則推文
07/23 04:08, , 67F
array(
07/23 04:08, 67F

07/23 04:09, , 68F
PDO::MYSQL_ATTR_INIT_COMMAND
07/23 04:09, 68F

07/23 04:09, , 69F
=> "SET NAMES UTF8"
07/23 04:09, 69F

07/23 04:09, , 70F
)
07/23 04:09, 70F

07/23 04:09, , 71F
);
07/23 04:09, 71F

07/23 04:09, , 72F
$list = $db->prepare("SELECT * FROM `mydata`");
07/23 04:09, 72F

07/23 04:09, , 73F
$list->execute();
07/23 04:09, 73F

07/23 04:09, , 74F
$tbody = "";
07/23 04:09, 74F

07/23 04:10, , 75F
while ($data = $list->fetch()) {
07/23 04:10, 75F

07/23 04:10, , 76F
$tbody .= "<tr>";
07/23 04:10, 76F

07/23 04:10, , 77F
$tbody .= "<td>{$data['id']}</td>";
07/23 04:10, 77F

07/23 04:10, , 78F
$tbody .= "<td>{$data['data']}</td>";
07/23 04:10, 78F

07/23 04:10, , 79F
$tbody .= "</tr>\n";
07/23 04:10, 79F

07/23 04:10, , 80F
}
07/23 04:10, 80F

07/23 04:10, , 81F
?>
07/23 04:10, 81F

07/23 04:10, , 82F
<!DOCTYPE html>
07/23 04:10, 82F

07/23 04:11, , 83F
<meta charset="utf-8" />
07/23 04:11, 83F

07/23 04:11, , 84F
<a href="insert.php">insert</a>
07/23 04:11, 84F

07/23 04:11, , 85F
<table>
07/23 04:11, 85F

07/23 04:12, , 86F
<thead>
07/23 04:12, 86F

07/23 04:12, , 87F
<tr>
07/23 04:12, 87F

07/23 04:12, , 88F
<th>ID</th>
07/23 04:12, 88F

07/23 04:12, , 89F
<th>Data</th>
07/23 04:12, 89F

07/23 04:12, , 90F
</tr>
07/23 04:12, 90F

07/23 04:12, , 91F
</thead>
07/23 04:12, 91F

07/23 04:12, , 92F
<tbody>
07/23 04:12, 92F

07/23 04:13, , 93F
<?php echo($tbody); ?>
07/23 04:13, 93F

07/23 04:13, , 94F
</tbody>
07/23 04:13, 94F

07/23 04:13, , 95F
</table>
07/23 04:13, 95F

07/23 04:13, , 96F
[2013/7/23]
07/23 04:13, 96F

07/23 11:19, , 97F
這推文...
07/23 11:19, 97F

07/23 11:31, , 98F
還縮排良好! Orz
07/23 11:31, 98F

07/23 12:10, , 99F
真厲害的推文
07/23 12:10, 99F

07/23 12:40, , 100F
給a60301:你可以用回文…
07/23 12:40, 100F

07/23 13:31, , 101F
樓上,我覺得他故意的XD
07/23 13:31, 101F

07/23 13:31, , 102F
推文縮排良好,結構清楚,推!
07/23 13:31, 102F

07/23 14:11, , 103F
神回覆!!
07/23 14:11, 103F

07/23 14:36, , 104F
縮的真好....
07/23 14:36, 104F

07/23 17:31, , 105F
神回覆
07/23 17:31, 105F

07/23 19:14, , 106F
這我已經不知道要怎麼形容了。光是不推文出錯就很厲害了。
07/23 19:14, 106F
文章代碼(AID): #1HxGMuZj (PHP)
文章代碼(AID): #1HxGMuZj (PHP)