展开全部
1:首先要使用PHP的超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)
2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。
具体示例:
(1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。
<?php
$con = <a href="https://www.baidu.com/s?wd=mysql_connect&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_connect</a>("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
<a href="https://www.baidu.com/s?wd=mysql_select_db&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_select_db</a>("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() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。
<?php
$con = <a href="https://www.baidu.com/s?wd=mysql_connect&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_connect</a>("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
<a href="https://www.baidu.com/s?wd=mysql_select_db&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_select_db</a>("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)
?>
2:然后使用INSERT INTO 语句用于向数据库表中插入新记录。
具体示例:
(1)首先创建了一个名为 "Persons" 的表,有三个列:"Firstname", "Lastname" 以及 "Age"。
<?php
$con = <a href="https://www.baidu.com/s?wd=mysql_connect&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_connect</a>("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
<a href="https://www.baidu.com/s?wd=mysql_select_db&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_select_db</a>("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() 函数执行 INSERT INTO 语句,一条新的记录会添加到数据库表中。
<?php
$con = <a href="https://www.baidu.com/s?wd=mysql_connect&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_connect</a>("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
<a href="https://www.baidu.com/s?wd=mysql_select_db&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9m1nzuARvmhcLuWb1PvF90ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHR1n164rHc4rj6YP16znHTz" target="_blank" class="baidu-highlight">mysql_select_db</a>("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)
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询