Php中使用INSERT插入数据语句的方法
保存数据到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); ?>
例子,代码如下:
/* * 资料保存 * 程序:刘兴 * 时间: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('-',$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='$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入门网提醒你,保存数据时我们要注意页面之间的编码以免出现不必要的麻烦了,还有就是数据类型与我们提交的数据要一致否则数据是保存不成功的.
文章地址:
转载随意^^请带上本文地址!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

