新聞中心
如何將PHP表單提交實(shí)現(xiàn)提交到多個(gè)表的不同字段?
如果您想在PHP表單中實(shí)現(xiàn)提交到多個(gè)表的不同字段,可以使用如下步驟來實(shí)現(xiàn):
創(chuàng)新互聯(lián)長(zhǎng)期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鐵西企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,鐵西網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
在表單中定義相應(yīng)的字段,用于獲取用戶輸入的數(shù)據(jù)。
使用PHP代碼從表單中獲取用戶輸入的數(shù)據(jù)。
使用PHP中的數(shù)據(jù)庫操作函數(shù)(例如MySQLi或PDO),連接到數(shù)據(jù)庫,并且向不同的表插入數(shù)據(jù)。
例如,如果您想插入用戶名和電子郵件到users表,并插入用戶的年齡和住址到profiles表,可以這樣寫:
// 獲取用戶輸入的數(shù)據(jù)
$username = $_POST['username'];
$email = $_POST['email'];
$age = $_POST['age'];
$address = $_POST['address'];
// 連接到數(shù)據(jù)庫
$conn = mysqli_connect('localhost', 'username', 'password', 'database');
// 插入用戶名和電子郵件到users表
$sql = "INSERT INTO users (username, email) VALUES ('$username', '$email')";
mysqli_query($conn, $sql);
// 插入用戶的年齡和住址到profiles表
$sql = "INSERT INTO profiles (age, address) VALUES ('$age', '$address')";
mysqli_query($conn, $sql);
PHP怎么獲取表單提交的數(shù)據(jù)啊?
一、用file_get_contents以get方式獲取內(nèi)容,需要輸入內(nèi)容為:
1、?php
2、$url='';
3、$html=file_get_contents($url);
4、echo$html;
5、?
二、用file_get_contents函數(shù),以post方式獲取url,需要輸入內(nèi)容為
1、?php
2、$url='';
3、$data=array('foo'='bar');
4、$data=http_build_query($data);
5、$opts=array(
6、'http'=array(
7、?'method'='POST',
8、?'header'="Content-type:application/x-www-form-urlencoded\r\n".
9、??????????"Content-Length:".strlen($data)."\r\n",
10、?'content'=$data
11、)
12、);
13、$ctx=stream_context_create($opts);
14、$html=@file_get_contents($url,'',$ctx);
15、?
三、用fopen打開url,以get方式獲取內(nèi)容,需要輸入內(nèi)容為
1、?php
2、$fp=fopen($url,'r');
3、$header=stream_get_meta_data($fp);//獲取信息
4、while(!feof($fp)){
5、$result.=fgets($fp,1024);
6、}
7、echo"urlheader:{$header}br":
8、echo"urlbody:$result";
9、fclose($fp);
10、?
四、用fopen打開url,以post方式獲取內(nèi)容,需要輸入內(nèi)容為
1、?php
2、$data=array('foo2'='bar2','foo3'='bar3');
3、$data=http_build_query($data);
4、$opts=array(
5、'http'=array(
6、'method'='POST',
7、'header'="Content-type:application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n".
8、"Content-Length:".strlen($data)."\r\n",
9、'content'=$data
10、)
11、);
12、$context=stream_context_create($opts);
13、$html=fopen(';id2=i4','rb',false,$context);
14、$w=fread($html,1024);
15、echo$w;
16、?
五、用fsockopen函數(shù)打開url,以get方式獲取完整的數(shù)據(jù),包括header和body,需要輸入內(nèi)容為
1、?php
2、functionget_url($url,$cookie=false)
3、{
4、$url=parse_url($url);
5、$query=$url[path]."?".$url[query];
6、echo"Query:".$query;
7、$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
8、if(!$fp){
9、returnfalse;
10、}else{
11、$request="GET$queryHTTP/1.1\r\n";
12、$request.="Host:$url[host]\r\n";
13、$request.="Connection:Close\r\n";
14、if($cookie)$request.="Cookie:??$cookie\n";
15、$request.="\r\n";
16、fwrite($fp,$request);
17、while(!@feof($fp)){
18、$result.=@fgets($fp,1024);
19、}
20、fclose($fp);
21、return$result;
22、}
23、}
24、//獲取url的html部分,去掉header
25、functionGetUrlHTML($url,$cookie=false)
26、{
27、$rowdata=get_url($url,$cookie);
28、if($rowdata)
29、{
30、$body=stristr($rowdata,"\r\n\r\n");
31、$body=substr($body,4,strlen($body));
32、return$body;
33、}
34、?returnfalse;
35、}
36、?
參考資料:
php-file_get_contents
PHP 提交表格 插入數(shù)據(jù)
if($submit)
你改成
if($_POST['submit'])
試試,看看行不行
PHP表單提交不同數(shù)據(jù)到數(shù)據(jù)庫的不同表的不同字段怎么實(shí)現(xiàn)?
你可以用 PHP 的 MySQLi 或 PDO 庫來實(shí)現(xiàn)。
首先,你需要使用這些庫中的函數(shù)連接數(shù)據(jù)庫,然后把表單中的字段值插入到相應(yīng)的數(shù)據(jù)表中。
例如,如果你使用 MySQLi,你可以這樣寫:
$conn = mysqli_connect("hostname", "username", "password", "database_name");
$rwid = $_POST['rwid'];
$rwdj = $_POST['rwdj'];
$rwzb = $_POST['rwzb'];
$sql1 = "INSERT INTO table_a (s) VALUES ('$rwid')";
mysqli_query($conn, $sql1);
$sql2 = "INSERT INTO table_b (d) VALUES ('$rwdj')";
mysqli_query($conn, $sql2);
$sql3 = "INSERT INTO table_c (f) VALUES ('$rwzb')";
mysqli_query($conn, $sql3);
如果你使用 PDO,你可以這樣寫:
$conn = new PDO("mysql:host=hostname;dbname=database_name", "username", "password");
$rwid = $_POST['rwid'];
$rwdj = $_POST['rwdj'];
$rwzb = $_POST['rwzb'];
$stmt1 = $conn-prepare("INSERT INTO table_a (s) VALUES (:s)");
$stmt1-bindParam(':s', $rwid);
$stmt1-execute();
$stmt2 = $conn-prepare("INSERT INTO table_b (d) VALUES (:d)");
$stmt2-bindParam(':d', $rwdj);
$stmt2-execute();
$stmt3 = $conn-prepare("INSERT INTO table_c (f) VALUES (:f)");
$stmt3-bindParam(':f', $rwzb);
$stmt3-execute();
希望這個(gè)答案對(duì)你有幫助。
文章標(biāo)題:php提交表格數(shù)據(jù) 提交表單數(shù)據(jù)有哪幾種方法,php如何獲取表單提交的數(shù)據(jù)
鏈接地址:http://fisionsoft.com.cn/article/dopdpij.html