Blogger Information
Blog 27
fans 1
comment 1
visits 21984
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作插入数据-PHP线上五期
哥特的博客
Original
1011 people have browsed it

总结:和删除、修改不同,插入数据需要有汉字,并且需要根据插入的字段写values的时候汉字要在单引号里使用双引号,一些可以为空的数据也可以设置默认值。



实例

<?php
header("Content-type: text/html; charset=utf-8");
	function con(){
		$dsn = 'mysql:host=127.0.0.1;dbname=a1;charset=utf8;port=3306';
		$dbname = 'root';
		$dbpw = 'root';
		try{
			$p = new PDO($dsn,$dbname,$dbpw);
		}catch(PDOException $e){
			print_r($e->getMessage());
			exit;
		}
		return $p;
	}
	// INSERT INTO student(id,name,grade) VALUES(1,'zhangshan',98);
	function insert_user  ($table,$name,$age,$phone,$company,$job,$address,$sal,$edu='无',$school='无'){
		$c = con(); 
		$sql = 'INSERT INTO '; 
		$sql .= $table;
		$sql .='(name,age,phone,company,job,address,sal,edu,school) VALUES (' ;
	 
		$sql .= $name; 
		$sql .= $age; 
		$sql .= $phone; 
		$sql .= $company; 
		$sql .= $job; 
		$sql .= $address; 
		$sql .= $sal; 
		$sql .= $edu; 
		$sql .= $school;
		$sql .=')';
		  // echo $sql;exit; 
		$a = $c->prepare($sql);
		if($a->execute()){
			if($a->rowCount()){
				$a->setFetchMode(PDO::FETCH_ASSOC);
				$set = $a->fetchALL(); 
				return $set;
			}else{
				return false;
			}
			
		}else{
			return false;
		}
	}
	$s  =insert_user(' staff ','"张学友",','63, ','15666666666,','"渣打银行",','"总经理",','"***",','999999,','"博士",','"牛津大学"');
	  var_dump($s);
	if($s!=0){
		echo '插入成功';
	} else{
		echo '插入失败';
	}
	
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post