當前位置:首頁 » 編程語言 » php表單mysql

php表單mysql

發布時間: 2022-05-02 17:11:32

php mysql 表單 插入數據

dingdan_create.php

$bianhao=$_POST['bianhao'];
$fullname=$_POST['fullname'];
$chang=$_POST['chango'];
...

//連接資料庫
...

//插入
$sql="insert into XXX (a,b,c,...) values ('$bianhao','$fullname','$chang',...)";
mysql_query($sql);

⑵ php用表單提交數據到mysql的問題

可以在前台用javascript對你必填的欄位進行判斷,如為空不能提交並給出提示,後台也可以通過empty對需要填寫的欄位進行判斷,如果為空,跳回之前的頁面,不對資料庫進行操作

⑶ PHP 表單 提交數據到mysql

PHP 提交表單,然後保存資料庫示例:

1.sql腳本

createdatabasecompany;
usecompany;
createtableemployee(
idint(11)notnullprimarykeyauto_increment,
emp_namevarchar(20)notnull,
emp_novarchar(30)notnull,
emp_jobvarchar(50)
);

2.index.php代碼:

<?php
header("Content-type:text/html;charset=utf-8;");
//判斷是否提交表單
if(isset($_POST['btn'])){
//連接資料庫
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die("資料庫連接錯誤!".mysql_error());
}
mysql_select_db("company");
mysql_query("setnamesutf8");
//獲取表單提交元素
$emp_name=$_POST['emp_name'];
$emp_no=$_POST['emp_no'];
$emp_job=$_POST['emp_job'];
//驗證表單元素,然後入庫操作
if($emp_name&&$emp_no&&$emp_job){
$sql="insertintoemployee
(emp_name,emp_no,emp_job)
values
('{$emp_name}','{$emp_no}','{$emp_job}')
";
$int=mysql_query($sql);
$suc_msg="<fontcolor='green'>數據插入成功!</font><ahref='javascript:history.go(-1);'>返回</a>";
$err_msg="數據插入失敗";
exit($int?$suc_msg:$err_msg);
}else{
exit("提交數據全部為必填項!");
}
}
?>
<html>
<head>
<title>PHP表單提交示例</title>
<metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
</head>
<body>
<formid="myform"action="<?phpecho$_SERVER['PHP_SELF']?>"method="post">
員工姓名:<inputname="emp_name"type="text"/><br/>
工號:<inputname="emp_no"type="text"/><br/>
工作職責:<inputname="emp_job"type="text"/><br/>
<inputname="btn"type="submit"value="提交表單"/><br/>
</form>
</body>
</html>

3.運行效果:

⑷ PHP連接mysql表單數據增刪改查,為什麼數據增加不上

method屬性表示提交表單的方式
action屬性表示表單提交的目標地址
以你的第二張圖為例:意思是將表單數據以post方式提交給list.php
夠清楚了吧 不清楚再問
method 屬性值只有兩個,post或get
用哪種方法看list.php 是怎麼獲取的。
method空值的默認值是get
action空值的話默認是當前頁面
php獲取get值用 $_GET
php獲取post值用$_POST

還有一點,你的第一個圖,在下面用到了header()這個方法。你運行沒有報錯么?header()這個方法之前是不能有任何輸出的。除非你開啟了ob_start.

⑸ PHP網頁製作,怎樣把注冊表單的數據導入MySQL資料庫

首先你要建立一個表,例如是注冊的用戶表user
,裡面的結構有欄位
id,
name,nickname,email等。
然後在你的表單處<form
action="a.php"
method="post"
name="regform">(如果有圖片上傳,還要加上enctype="multipart/form-data")
,那麼點擊表單提交按紐後,此表單將會交給處理頁a.php來作處理。
如果簡單點,你就直接可以將表單傳遞過來的數據$_POST,直接用sql插入語句,insert
into來插入到資料庫,表user中。例如insert
into
user
set
name='".$_POST['name']."'.............................

⑹ 如何在php網頁中通過一個表單讓使用者輸入數據提交後把輸入的數據傳遞到mysql資料庫中

表單通過get或者post傳值,你應該知道吧,在PHP頁讀取你傳過來的值
$_POST['name'],$_POST['pwd']
然後寫sql語句
$sql="INSERT INTO tables/*這是表名,自己定義*/ VALUES ('{$_POST['name']}','{$_POST['pwd']}')";有幾個欄位你就應該傳過來幾個值。
mysql_query($sql);//執行SQL語句,把值插到資料庫里。

⑺ 如何通過PHP把html的表單提交到mysql資料庫

首先,你得在diaocha.php這個文件,接收表單傳的值

$radiogroup = isset($_POST['radiogroup'])?$radiogroup:''

isset用來檢測是否有選中提交,然後就是資料庫的鏈接

$con = mysql_connect('localhost','root',''); //三個參數,分別是,連接的主機名,mysql的賬號,mysql密碼
mysql_query('set names utf8'); //設置連接的字元集,如果頁面是utf8的編碼,就是utf8,如果是gbk的話,那就寫 set names gbk
mysql_select_db('xxx',$con);&nbsp; //xxx就是你要選擇的資料庫名稱

插入數據

$sql = "insert into xxxx set xxx = $radiogroup" //xxxx 是你要插入的表名,xxx就是欄位名
mysql_query($sql);

⑻ 請問 PHP表單 為什麼無法寫入 mysql 資料庫

if
($_post['submit']){
$sql="insert
into
message(user,title,conten,lastdate)
values
('".$_post[user]."','".$_post[title]."','".$_post[content]."',now())";
mysql_query($sql)
or
die(mysql_error());
}
id是主鍵,自增,沒必要寫上。
如果還不行,你看看後面報錯信息。

⑼ php表單寫入mysql資料庫的代碼

<!--表單文件,拷入index.php-->
<!DOCTYPEhtml>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>


<title>Addstudents</title>
</head>
<body>

<!--資料庫用mysqli面向過程調用方法-->
<formmethod="post"action="write2db.php">

<!--資料庫用mysqli面向過程調用方法
<formmethod="post"action="write2db_sqlio.php">
-->
<!--資料庫用PDO調用方法
<formmethod="post"action="write2db_pdo.php">
-->

<label>FirstName</label>
<inputtype="text"name="first_name"/>
<br/>
<label>LastName</label>
<inputtype="text"name="last_name"/>
<br/>
<label>department</label>
<inputtype="text"name="department"/>
<br/>
<label>Email</label>
<inputtype="text"name="email"/>

<br/>
<inputtype="submit"value="Addstudents">
</form>

</body>
</html>

------------------------------

<?php
//拷貝命名為write2db.php,資料庫用mysqli面向過程調用方法
//print_r($_POST);

//createavariable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];

//調試用
echo"Yourinput:";
echo$first_name;
echo'<br/>';
echo$last_name;
echo'<br/>';
echo$department;
echo'<br/>';
echo$email;
echo'<br/>';


$servername="localhost";
//
//$username="username";
//$password="password";
$username="tester";
$password="testerPassword";
//yourdatabasename
$dbname="test";

$tablename="student";//Createconnection
$connect=mysqli_connect($servername,$username,$password,$dbname);

if(!$connect){
die("Connectionfailed:".mysqli_connect_error());
}
//Executethequery

$sql="INSERTINTO$tablename(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";


if(mysqli_query($connect,$sql)){
echo"Hooray!.Pleasecheckdatabase.";
}else{
echo"Error:".$sql."<br/>".mysqli_error($connect);
}

mysqli_close($connect);

?>
<?php
//拷貝命名為write2db_sqlio.php,資料庫用mysqli面向對象調用方法
//print_r($_POST);

//createavariable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];

//調試用
echo"Yourinput:";
echo$first_name;
echo'<br/>';
echo$last_name;
echo'<br/>';
echo$department;
echo'<br/>';
echo$email;
echo'<br/>';


$servername="localhost";
//
//$username="username";
//$password="password";
$username="tester";
$password="testerPassword";
//databasename
$dbname="test";

$tablename="student";//Createconnection
$conn=newmysqli($servername,$username,$password,$dbname);
//Checkconnection
if($conn->connect_error){
die("Connectionfailed:".$conn->connect_error);
}

$sql="INSERTINTO$tablename(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";

if($conn->query($sql)===TRUE){
echo"Newrecordcreatedsuccessfully";
}else{
echo"Error:".$sql."<br>".$conn->error;
}

$conn->close();

?>
<?php
//拷貝為文件write2db_pdo.php,資料庫用PDO調用方法

//print_r($_POST);
avariable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];

//調試用
echo"Yourinput:";
echo$first_name;
echo'<br/>';
echo$last_name;
echo'<br/>';
echo$department;
echo'<br/>';
echo$email;
echo'<br/>';


$servername="localhost";
//
//$username="username";
//$password="password";
$username="tester";
$password="testerPassword";
//yourdatabasename
$dbname="test";

$tablename="student";//Createconnection
try{
$conn=newPDO("mysql:host=$servername;dbname=$dbname",$username,$password);
//setthePDOerrormodetoexception
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="INSERTINTO$tablename(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
//useexec()
$conn->exec($sql);
echo"Newrecordcreatedsuccessfully";
}
catch(PDOException$e)
{
echo$sql."<br>".$e->getMessage();
}

$conn=null;

?>
--創建資料庫test,將此文件存為test.sql導入資料庫,或者手動創建表結構
--phpMyAdminSQLDump
--version4.7.4
--https://www.phpmyadmin.net/
--
--Host:127.0.0.1:3306
--GenerationTime:Mar12,2018at04:04AM
--Serverversion:5.7.19
--PHPVersion:7.1.9

SETSQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SETAUTOCOMMIT=0;
STARTTRANSACTION;
SETtime_zone="+00:00";


/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101SET@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101SETNAMESutf8mb4*/;

--
--Database:`test`
--

----------------------------------------------------------

--
--Tablestructurefortable`student`
--

DROPTABLEIFEXISTS`student`;
CREATETABLEIFNOTEXISTS`student`(
`id`tinyint(3)UNSIGNEDNOTNULLAUTO_INCREMENT,
`first_name`varchar(20)NOTNULL,
`last_name`varchar(20)NOTNULL,
`department`varchar(50)NOTNULL,
`email`varchar(50)NOTNULL,
PRIMARYKEY(`id`)
)ENGINE=MyISAMAUTO_INCREMENT=2DEFAULTCHARSET=utf8;

--
--Dumpingdatafortable`student`
--

INSERTINTO`student`(`id`,`first_name`,`last_name`,`department`,`email`)VALUES
(1,'first1','last1','cs','[email protected]');
COMMIT;

/*!40101SETCHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101SETCHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS*/;
/*!40101SETCOLLATION_CONNECTION=@OLD_COLLATION_CONNECTION*/;
熱點內容
Pua腳本 發布:2025-05-14 19:24:56 瀏覽:448
蘋果像素低為什麼比安卓好 發布:2025-05-14 19:13:23 瀏覽:459
安卓機微信怎麼設置紅包提醒 發布:2025-05-14 19:00:15 瀏覽:271
androidsystem許可權設置 發布:2025-05-14 18:56:02 瀏覽:970
mq腳本 發布:2025-05-14 18:45:37 瀏覽:25
仙境傳說ro解壓失敗 發布:2025-05-14 18:45:01 瀏覽:868
betweenand的用法sql 發布:2025-05-14 18:39:25 瀏覽:250
tplink攝像頭存儲卡格式化 發布:2025-05-14 18:37:08 瀏覽:347
安卓平板怎麼安裝excel的軟體 發布:2025-05-14 18:35:44 瀏覽:42
廣州數控圓弧編程實例 發布:2025-05-14 18:25:00 瀏覽:401