保存数据到mysql数据库中我们需要使用update或insert来协助我们完成,下面我给学习php的朋友介绍几个应用例子,希望文章给你带来帮助.
我们先来介绍INSERT语句
语法1:INSERT INTO table_name VALUES ("1", "2")
解说:就是插入插入2个值,第一个值为:1 第二个值为:2
语句2:INSERT INTO table_name (column1, column2)VALUES (value1, value2)
解说:就是根据指定的键插入指定的值,在字段 column1 插件的值为:value1,然后再字段column2 插入的值为:value2.
INSERT语句的列子,代码如下:
-
<?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);
-
?>
例子,代码如下:
-
<?php
-
-
-
-
-
-
?>
-
<!DOCTYPE html PUBLIC ”-
-
<html xmlns=”http:
-
<?php
-
require_once(“gxpass.php”);
-
require_once(“connDB.php”);
-
$zl=$_POST;
-
-
-
list ($pid,$pname)=split(‘-’,$zl[people]);
-
$year=$zl[year];
-
$jd=$zl[jd];
-
foreach($zl as $key => $value){
-
if (strstr($key,”fs”)) {
-
if (strstr($key,”kfs”)){
-
$khxm=substr($key,3,strlen($key)-3);
-
$fs=$zl[$key];
-
continue;
-
}
-
else{
-
$khxm=substr($key,2,strlen($key)-2);
-
}
-
$khfs=$value;
-
$khinfo=$zl[$khxm];
-
$sql=”delete from khzl where peopleid=’$pid’ and khbm=’$khxm’ and time=’$jd’ and year=’$year’”;
-
$result=mysql_query($sql) ;
-
$sql=”INSERT INTO khzl (peopleid ,khbm ,fenshu ,time ,year ,info,fs ) VALUES (‘$pid’, ’$khxm’, $khfs, ’$jd’, ’$year’, ’$khinfo’,'$fs’)”;
-
$fs=0;
-
$result=mysql_query($sql);
-
}
-
}
-
?>
-
<head>
-
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
-
<title>数据保存</title>
-
</head>
-
<body>
-
<?php
-
-
-
echo ”<script language=’javascript’>”;
-
echo ” location=’peoplexz.php’;”;
-
echo ”</script>”;
-
?>
-
</body>
-
</html>
php粉丝网提醒你,保存数据时我们要注意页面之间的编码以免出现不必要的麻烦了,还有就是数据类型与我们提交的数据要一致否则数据是保存不成功的.
(责任编辑:最模板) |