Home > php教程 > php手册 > Php中使用INSERT插入数据语句的方法

Php中使用INSERT插入数据语句的方法

WBOY
Release: 2016-05-27 08:47:13
Original
2395 people have browsed it

保存数据到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(&#39;Could not connect: &#39; . mysql_error()); 
} 
mysql_select_db("my_db", $con); 
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)  
VALUES (&#39;Peter&#39;, &#39;Griffin&#39;, &#39;35&#39;)"); 
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)  
VALUES (&#39;Glenn&#39;, &#39;Quagmire&#39;, &#39;33&#39;)"); 
mysql_close($con); 
?>
Copy after login

例子,代码如下:

/*   
  * 资料保存   
  * 程序:刘兴   
  * 时间:2010-6   
  */   
    
<!DOCTYPE html>    
<html xmlns="http://www.w3.org/1999/xhtml">    
<?php     
  require_once("gxpass.php");//引入密码验证     
  require_once("connDB.php");//引入数据库连接参数      
  $zl=$_POST;    
   
//数据保存    
  list ($pid,$pname)=split(&#39;-&#39;,$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;               
   }   //www.phprm.com 
   else{    
          $khxm=substr($key,2,strlen($key)-2);    
   }       
      $khfs=$value;     
   $khinfo=$zl[$khxm];          
          $sql="delete from khzl where peopleid=&#39;$pid&#39; and khbm=&#39;$khxm&#39; and time=&#39;$jd&#39; and year=&#39;$year&#39;";    
    $result=mysql_query($sql) ;              
       $sql="INSERT INTO khzl (peopleid ,khbm ,fenshu ,time ,year ,info,fs ) VALUES  (&#39;$pid&#39;, &#39;$khxm&#39;, $khfs, &#39;$jd&#39;, &#39;$year&#39;, &#39;$khinfo&#39;,&#39;$fs&#39;)";    
    $fs=0;    
   $result=mysql_query($sql);      
   }    
 }     
?>
<head>    
<meta http-equiv="Content-Type" content="text/html; charset=gb2312&Prime; />    
<title>数据保存</title>    
</head>    
<body>    
<?php     
//网址转向    
echo "<script language=&#39;javascript&#39;>";     
echo " location=&#39;peoplexz.php&#39;;";     
echo "</script>";    
</body>    
</html>
Copy after login

php入门网提醒你,保存数据时我们要注意页面之间的编码以免出现不必要的麻烦了,还有就是数据类型与我们提交的数据要一致否则数据是保存不成功的.


文章地址:

转载随意^^请带上本文地址!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template