本文目錄一覽:
php 初學者怎麼實現表單的提交
給 form 表單添加 action 屬性,屬性值就是你接收表單數據的php頁面地址,比如
html代碼:
form action=”1.php”
input type=”text” name=”user”
input type=”submit” value=”提交”
/form
php代碼:
$user = $_GET[‘user’];
echo $user;
php 表單提交
“insert into student set 學號=’$no’,姓名=’$n’,年齡=’$a’,性別=’$s’,學院=’$d'”
這個SQL語句可能有誤。
你確定你的 student 表 字段是 學號 姓名 這樣的中文嗎?
另外我用的是這樣的
“insert into 表名 (字段1,字段2) values(值1,值2)” 字段一般都是英文,用中文容易出錯。
你可以看一下你的表的結構。
html+php向數據庫提交表單
1:首先要使用PHP的超全局變量 $_GET 和 $_POST 用於收集表單數據(form-data)
2:然後使用INSERT INTO 語句用於向數據庫表中插入新記錄。
具體示例:
(1)首先創建了一個名為 “Persons” 的表,有三個列:”Firstname”, “Lastname” 以及 “Age”。
?php$con = mysql_connect(“localhost”,”peter”,”abc123″);if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } mysql_select_db(“my_db”, $con); mysql_query(“INSERT INTO Persons (FirstName, LastName, Age) VALUES (‘Peter’, ‘Griffin’, ’35’)”); mysql_query(“INSERT INTO Persons (FirstName, LastName, Age) VALUES (‘Glenn’, ‘Quagmire’, ’33’)”); mysql_close($con);?
(2)其次創建一個 HTML 表單,這個表單可把新記錄插入 “Persons” 表。
htmlbody form action=”insert.php” method=”post”Firstname: input type=”text” name=”firstname” /Lastname: input type=”text” name=”lastname” /Age: input type=”text” name=”age” /input type=”submit” //form /body/html
(3)接着當用戶點擊上例中 HTML 表單中的提交按鈕時,表單數據被發送到 “insert.php”。”insert.php” 文件連接數據庫,並通過 $_POST 變量從表單取回值。然後,mysql_query() 函數執行 INSERT INTO 語句,一條新的記錄會添加到數據庫表中。
?php$con = mysql_connect(“localhost”,”peter”,”abc123″);if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } mysql_select_db(“my_db”, $con); $sql=”INSERT INTO Persons (FirstName, LastName, Age)VALUES(‘$_POST[firstname]’,’$_POST[lastname]’,’$_POST[age]’)”; if (!mysql_query($sql,$con)) { die(‘Error: ‘ . mysql_error()); }echo “1 record added”; mysql_close($con)?
如何用PHP實現表單提交
創建go.php,代碼如下
?php
@$username = $_POST[‘name’];
@$usermessage = $_POST[‘message’];
if(!empty($username) || !empty($usermessage)){
echo “您的姓名:”.$username.”,您的留言內容:”.$usermessage;
}else{
echo ‘form action=”go.php” method=”post”
您的姓名:input type=”text” name=”name”brbr
留言內容:input type=”text” name=”message”
button提交/button
/form’;
案例截圖:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/308370.html