1:首先要使用PHP的超全域性變數$_GET和$_POST用於收集表單資料(form-data)2:然後使用INSERTINTO語句用於向資料庫表中插入新記錄。具體示例:(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"表。<html><body><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()函式執行INSERTINTO語句,一條新的記錄會新增到資料庫表中。<?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)?>
"$name","$email","$info","$ip",
你這些變數沒有賦值。。如下
$name = $_REQUEST["name"].
1:首先要使用PHP的超全域性變數$_GET和$_POST用於收集表單資料(form-data)2:然後使用INSERTINTO語句用於向資料庫表中插入新記錄。具體示例:(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"表。<html><body><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()函式執行INSERTINTO語句,一條新的記錄會新增到資料庫表中。<?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)?>