thinkPHP在往MySQL添加数据时,如何确保其中一个字段,随机添加一个与表中内容不相同的15位数据

thinkPHP在往MySQL添加数据时,如何确保其中一个字段,随机添加一个与表中内容不相同的15位数据,第1张

可以试试,设置该字段(row_a)为unique索引,

使用insert into table_a(row_a,row_b)values(val_a,val_b)ON DUPLICATE KEY UPDATE rowa=XXX

其中,val_a和XXX都是你随机生成的值,联系重复的的可能性应该比较小

第一步:建立数据库和数据表(按照自己的Excel数据设立字段)。

[sql] view plain copy print?

CREATE DATABASE php_excel

USE php_excel

CREATE TABLE IF NOT EXISTS php_excel(

id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,

gid varchar(20) NOT NULL,

stu_no varchar(20) NOT NULL,

name varchar(45) NOT NULL,

age int(4) NOT NULL

)ENGINE=MyISAM DEFAULT CHARSET=utf8

第二步:前台index.php文件。

[html] view plain copy print?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>phpexcel导入excel数据到MYSQL数据库</title>

</head>

<body>

<form name="frm1" action="insertdb.php" method="post" enctype="multipart/form-data">

<input name="filename" type="file" />

<input name="submit" type="submit" value="import" />

</form>

</body>

</html>

第三步:向数据库插入数据的insertdb.php文件。

[php] view plain copy print?

session_start()

header("Content-type:text/htmlcharset:utf-8")

//全局变量

$succ_result=0

$error_result=0

$file=$_FILES['filename']

$max_size="2000000"//最大文件限制(单位:byte)

$fname=$file['name']

$ftype=strtolower(substr(strrchr($fname,'.'),1))

//文件格式

$uploadfile=$file['tmp_name']

if($_SERVER['REQUEST_METHOD']=='POST'){

if(is_uploaded_file($uploadfile)){

if($file['size']>$max_size){

echo "Import file is too large"

exit

}

if($ftype!='xls'){

echo "Import file type is error"

exit

}

}else{

echo "The file is not empty!"

exit

}

}

require("./conn.php") //连接mysql数据库

//调用phpexcel类库

require_once 'PHPExcel.php'

require_once 'PHPExcel\IOFactory.php'

require_once 'PHPExcel\Reader\Excel5.php'

$objReader = PHPExcel_IOFactory::createReader('Excel5')//use excel2007 for 2007 format

$objPHPExcel = $objReader->load($uploadfile)

$sheet = $objPHPExcel->getSheet(0)

$highestRow = $sheet->getHighestRow()// 取得总行数

$highestColumn = $sheet->getHighestColumn()// 取得总列数

$arr_result=array()

$strs=array()

for($j=2$j<=$highestRow$j++)

{

unset($arr_result)

unset($strs)

for($k='A'$k<= $highestColumn$k++)

{

//读取单元格

$arr_result .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().','

}

$strs=explode(",",$arr_result)

$sql="insert into php_excel(gid,stu_no,name,age) values ($strs[0],'$strs[1]','$strs[2]',$strs[3])"

echo $sql."<br/>"

mysql_query("set names utf8")

$result=mysql_query($sql) or die("执行错误")

$insert_num=mysql_affected_rows()

if($insert_num>0){

$succ_result+=1

}else{

$error_result+=1

}

}

echo "插入成功".$succ_result."条数据!!!<br>"

echo "插入失败".$error_result."条数据!!!"

其中conn.php代码如下:

[php] view plain copy print?

$mysql=mysql_connect("localhost","root","") or die("数据库连接失败!")

mysql_select_db("php_excel",$mysql)

mysql_query("set names utf8")

我的导入效果如下:

至此,从Excel文件读取数据批量导入到Mysql数据库完成。

<?php

class MysqlManage{

/*创建数据库,并且主键是aid

* table 要查询的表名

*/

function createTable($table){

$sql="CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB"

M()->execute($sql)

$this->checkTable($table)

}

/*

* 检测表是否存在,也可以获取表中所有字段的信息

* table 要查询的表名

* return 表里所有字段的信息

*/

function checkTable($table){

$sql="desc `$table`"

$info=M()->execute($sql)

return $info

}

/*

* 检测字段是否存在,也可以获取字段信息(只能是一个字段)

* table 表名

* field 字段名

*/

function checkField($table,$field){

$sql='desc `$table` $field'

$info=M()->execute($sql)

return $info

}

/*

* 添加字段

* table 表名

* info 字段信息数组 array

* return 字段信息 array

*/

function addField($table,$info){

$sql="alter table `$table` add column"

$sql.=$this->filterFieldInfo()

M()->execute($sql)

$this->checkField($table,$info['name'])

}

/*

* 修改字段

* 不能修改字段名称,只能修改

*/

function editField($table,$info){

$sql="alter table `$table` modify "

$sql.=$this->filterFieldInfo($info)

M()->execute($sql)

$this->checkField($table,$info['name'])

}

/*

* 字段信息数组处理,供添加更新字段时候使用

* info[name] 字段名称

* info[type] 字段类型

* info[length] 字段长度

* info[isNull] 是否为空

* info['default'] 字段默认值

* info['comment'] 字段备注

*/

private function filterFieldInfo($info){

if(!is_array($info))

return

$newInfo=array()

$newInfo['name']=$info['name']

$newInfo['type']=$info['type']

switch($info['type']){

case 'varchar':

case 'char':

$newInfo['length']=empty($info['length'])?100:$info['length']

$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL'

$newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default']

$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment']

break

case 'int':

$newInfo['length']=empty($info['length'])?7:$info['length']

$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL'

$newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default']

$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment']

break

case 'text':

$newInfo['length']=''

$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL'

$newInfo['default']=''

$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment']

break

}

$sql=$newInfo['name']." ".$newInfo['type']

$sql.=(!empty($newInfo['length']))?($newInfo['length']) .' ':' '

$sql.=$newInfo['isNull'].' '

$sql.=$newInfo['default']

$sql.=$newInfo['comment']

return $sql

}

/*

* 删除字段

* 如果返回了字段信息则说明删除失败,返回false,则为删除成功

*/

function dropField($table,$field){

$sql="alter table `$table` drop column $field"

M()->execute($sql)

$this->checkField($table,$filed)

}

/*

* 获取指定表中指定字段的信息(多字段)

*/

function getFieldInfo($table,$field){

$info=array()

if(is_string($field)){

$this->checkField($table,$field)

}else{

foreach($field as $v){

$info[$v]=$this->checkField($table,$v)

}

}

return $info

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11829327.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存