簡體   English   中英

將每行數據插入數據庫

[英]Insert each row of data to database

如何將用戶在每一行中插入的數據保存到數據庫中? 此表單為空白,用戶將根據學生的編號為每一行插入以下數據,如姓名、標記和格雷德

這是用戶插入數據的示例,當用戶單擊提交按鈕時,每行中的所有數據都將保存到數據庫中。 那么我該怎么做呢?

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php //First we need to make a connection with the database $host='localhost'; // Host Name. $db_user= 'root'; //User Name $db_password= ''; $db= 'student'; // Database Name. $conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error()); mysql_select_db($db) or die (mysql_error()); $query = "INSERT INTO student_info (No, Name, Mark, Gred) VALUES "; for ($i = 1; $i < count($_POST['No']); $i++) { $query .= " ({$_POST['No'][$i]}, '{$_POST['Name'][$i]}', '{$_POST['Mark'][$i]}', '{$_POST['Gred'][$i]}'),"; } mysql_close($conn); ?> <form id="form1" name="form1" method="post" action=""> <table width="800" border="2" align="center"> <tr> <td>NO</td> <td>NAME</td> <td>MARKS</td> <td>GRED</td> </tr> <tr> <td>1</td> <td><label for="Name"></label> <input type="text" name="Name[]" id="Name" /></td> <td><label for="Mark"></label> <input type="text" name="Mark[]" id="Mark" /></td> <td><label for="Gred"></label> <input type="text" name="Gred[]" id="Gred" /></td> </tr> <tr> <td>2</td> <td><label for="Name"></label> <input type="text" name="Name[]" id="Name" /></td> <td><label for="Mark"></label> <input type="text" name="Mark[]" id="Mark" /></td> <td><label for="Gred"></label> <input type="text" name="Gred[]" id="Gred" /></td> </tr> </table> <p> <center><input type="submit" name="SubmitButton" id="SubmitButton" value="Submit" /></center> </p> </form> </body> </html>

我假設您已使用 MySQLi 或 PDO 連接到數據庫並且位於本地服務器上。

為此,您需要使用 mySQL“INSERT”語句;

    INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

如果這是您需要的,請研究 - http://www.w3schools.com/sql/sql_insert.asp

祝你好運

暫無
暫無

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

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