当执行mysql insert 时插入两条是怎么回事?

Eason_____________ 2013-07-25 11:24:30
//做了一个手机上传图片到服务器的功能。但是获取到执行insert语句时都要执行两次!

//index.php

<?php
header("Content-Type: text/html; charset=UTF-8");
mysql_connect("localhost","dome_huayan","e2k3e6b8");
mysql_select_db("dome_huayan");
mysql_query("set names utf8");

include 'uploadImage.php';
$gettitle=$_GET['title'];
$getcontent=$_GET['content'];
$i = new uploadImage($_FILES['filename'],'./img/');
$i->doWork();
$i->imageCheck();

$sql="insert into json_bbs values('','".$gettitle."','".$getcontent."','".$i->iamgePath."','".time()."','')";
mysql_query($sql);
?>


//uploadImage.php


<?php
/**
*
* 图片上传类
* @author ChenYue
*
* @param $mageStauts 图片上传状态 1为正常状态
* @param $iamgePath 图片上传成功保存在数据库的路径
* @param $imagePathTemp 临时保存图片上传成功保存在数据库的路径
* @param $destination_folder 上传文件路径
* @param $imageName 上传的图片名(可自定义)
* @param $fileArray 上传的图片信息数组
* @param $updateImage 判断是否更新原有图片 0表示不更新 , 1 表示更新
* @param $uptypes 支持上传的图片类型
* @param max_file_size 支持上传的图片最大类型
* @param imageType 图片的类型
*
*/
class uploadImage{

public $imageStauts = 1;
public $iamgePath='';
public $imagePathTemp = "";
private $destination_folder;
private $imageName;
private $fileArray;
private $updateImage = 0;
private $uptypes = array(
'image/jpg',
'image/jpeg',
'image/png',
'image/x-png');

private $imageType = array('jpg','jpeg','png');
const max_file_size=2000000;


/**
* 构造函数
* @param $file 上传的图片信息数组
* @param $destination 上传文件路径
* @param $name 上传的图片名(可自定义)没定义,上传后的图片名为time()
* @param $dbPath 图片上传成功保存在数据库的路径
* @param $update 判断是否更新原有图片 0表示不更新 , 1 表示更新 。 更新会把已经存在的图片替换掉
*/

function __construct($file,$destination="",$name="",$dbPath="",$update=0){

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
$this->imageStauts = 'Error! Wrong HTTP method!';
}
if(is_array($file) && count($file)>0 && !empty($destination)){
$this->fileArray = $file;
$this->destination_folder = $destination;
$this->imageName = $name;
$this->imagePathTemp = $dbPath;
$this->updateImage = $update;
}else{

$this->imageStauts = '初始化失败';

}

}


/**
* 开始图片上传
*/
function imageStart(){
if($this->imageStauts === 1){
$this->imageCheck();

}
if($this->imageStauts === 1){
$this->doWork();
}
}
/**
*
* 图片的检查工作
*/
function imageCheck(){
$file = $this->fileArray;
//print_r($file);
if(!is_uploaded_file($file['tmp_name']) && $this->imageStauts === 1){
$this->imageStauts = '图片不存在!';

}

if(uploadImage::max_file_size < $file['size'] && $this->imageStauts === 1){
$this->imageStauts = '文件太大';
}
/*
//检查mime-type
if(!in_array(strtolower($file['type']), $this->uptypes) && $this->imageStauts === 1){
$this->imageStauts = '不支持 '.$file['type'].' 类型的文件';
}
*/
//防止在图片元数据的Comment字段中加入了php代码
//通过二进制匹配检查
$fileInfo = pathinfo($this->fileArray['name']);
$fileType = strtolower($fileInfo['extension']);
if(!in_array($fileType, $this->imageType) && $this->imageStauts === 1){
$this->imageStauts = '不支持 '.$fileType.' 类型的文件';
}

if(!file_exists($this->destination_folder) && $this->imageStauts === 1){
mkdir($this->destination_folder,0777);//设置文件权限
}
}
/**
*
* 开始图片上传的工作
*/
function doWork(){

$fileName = $this->fileArray['tmp_name'];
$fileSize = getimagesize($fileName);
$fileInfo = pathinfo($this->fileArray['name']);
$fileType = strtolower($fileInfo['extension']);
$n = !empty($this->imageName) ? $this->imageName : date("Y_n_d_H_i_s");
$destination = $this->destination_folder.$n.'.'.$fileType;//图片本地路径
$this->imagePathTemp = $this->imagePathTemp.$n.'.'.$fileType;//将要保存在数据库的路径

//上传图片,若图片存在不更新已有图片
if(file_exists($destination) && $this->imageStauts === 1 && $this->updateImage == 0){
$this->imageStauts = '图片已存在';
}

//上传图片,若图片存在更新已有图片
if($this->imageStauts === 1 && $this->updateImage == 1){
$deleteIMageDestination = $this->destination_folder.$n; //图片保存本地路径,包含文件名,但不包含图片后缀名
if($this->deleteImage($deleteIMageDestination)){
}else{
$this->imageStauts = '删除已存在图片失败';
}
}

if(!move_uploaded_file($fileName, $destination) && $this->imageStauts === 1){
$this->imageStauts = '传输错误';
}

if($this->imageStauts === 1){
$this->iamgePath = $this->imagePathTemp;
return $this->imageStauts;
}


}

/**
* 删除图片
* @param $path 图片在本地的保存路径
* @return 成功返回1 失败返回0
*/
function deleteImage($path){
if(!empty($path)){
foreach($this->imageType as $type){
$_path = $path.'.'.$type;
if(file_exists($_path)){
//echo $_path;
if(!unlink($_path)){
$this->imageStauts = '删除已存在图片失败';
return 0;
}
}

}
return 1;
}else{
$this->imageStauts = '待删除图片路径不能为空';
return 0;
}
}


}

?>
...全文
470 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackkettle 2013-07-26
  • 打赏
  • 举报
回复
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 15 楼 xuzuning 的回复:
你的文字提交和文件上传本身就是分开的
现在插入两条的问题解决了 就是插入的时候 获取不到$i->iamgePath的值。 但是当insert 是两条的时候 $i->iamgePath就会有。
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 15 楼 xuzuning 的回复:
你的文字提交和文件上传本身就是分开的
嗯 这是我用appcan做的手机app.
xuzuning 2013-07-25
  • 打赏
  • 举报
回复
你的文字提交和文件上传本身就是分开的
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 12 楼 ohmygirl 的回复:
[quote=引用 11 楼 ohmygirl 的回复:] [quote=引用 9 楼 Eason_____________ 的回复:] [quote=引用 8 楼 xuzuning 的回复:] 看看你的表单
[/quote] 斑竹说表单,你给数据库。。 是表单部分的代码和js部分的代码。[/quote] ajax? 传递过来不就执行insert了么? 提交表单又执行一次。 不就刚好两条数据么[/quote] 如果我判断一下 就是 if($_GET["title"]!=''){ } 就执行了一次。 但是插入的数据 $i->iamgePath的值就为空 这个是怎么回事?
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 9 楼 Eason_____________ 的回复:
[quote=引用 8 楼 xuzuning 的回复:] 看看你的表单
[/quote]

<!DOCTYPE html>
<html class="um landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px">
  <head>
    <title>
    </title>
    <meta charset="utf-8">
    <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" href="css/ui-input.css">

    <link rel="stylesheet" href="css/ui-btn.css">
    <link rel="stylesheet" href="css/ui-img.css">
    <link rel="stylesheet" href="css/ui-list.css">
    <link rel="stylesheet" href="css/ui-res.css">
    <link rel="stylesheet" href="css/ui-fold.css">
    <link rel="stylesheet" href="css/ui-base.css">
    <link rel="stylesheet" href="css/ui-box.css">
    <link rel="stylesheet" href="css/ui-color.css">
    <script src="js/zy_anim.js">
    </script>
    <script src="js/zy_control.js">
    </script>
 	<script src="js/zy_tmpl.js">
    </script>
	<script src="js/zy_click.js">
    </script>
	<script src="js/zy_json.js">
    </script>
    </head>
  
<body class="um-vp" ontouchstart>
<div class="btm" id="yinying" style="display:none"></div>
<div class="c-wh" id="fb_content">    	 

    <!--文本开始-->
    <div class="ub t-bla ulab">  
    <div class="ub-f1 c-wh uba uc-a1 b-gra us-i uinput uinn4" style="margin-top:5%">  
    <input placeholder="标题..."  type="text" name="title" class="uc-a1" id="fb-title">   
    </div>
    </div>
    <!--文本结束-->

    <!--文本开始-->
    <div class="ub t-bla ulab fb-btn">  
    <div class="ub-f1 c-wh uba uc-a1 b-gra us-i uinput uinn4">  
    <textarea  placeholder="请输入内容"  name="content" rows="7" id="fb-content" ></textarea>
    </div>
    </div>
    <!--文本结束-->


    <div style=" display:none; width:20%; height:15%; margin-top:5%; left:1%" id="show_img">
    	<span class="del" onClick="del()">x</span>
        <img style="width:100%; height:100%;display:block; border:1px solid #CCC;" src="" id="showPic" />        
    </div>

	<!--按钮开始-->
    <div ontouchstart="zy_touch('btn-act')" class="btn uba b-bla uinn5 c-blu1 c-m2 uc-a1 t-wh img-add"  onclick="picSeclet();" id="selectPic">添加图片</div>
	<!--按钮结束-->
    
	<!--按钮开始-->
    <div ontouchstart="zy_touch('btn-act')" class="btn uba b-bla uinn5 c-blu1 c-m2 uc-a1 t-wh img-add" onClick="upload()">发布</div>
	<!--按钮结束-->

    <div style="position:absolute; bottom:0; width:100%; z-index:100; display:none" id="divPic"> 
            <div ontouchstart="zy_touch('c-m2');" data-role="button" onclick="uexImageBrowser.pick();" class="btn uba b-bla uinn5 c-bla c-m1 uc-a1 t-wh img-add1"> 
                    <span class="ui-btn-inner ui-btn-corner-all"> 
                    <span class="ui-btn-text t-wh">从手机相册选择</span> 
                    </span>
            </div> 
            <div ontouchstart="zy_touch('c-m2');" data-role="button" onclick="uexCamera.open();" class="btn uba b-bla uinn5 c-bla c-m1 uc-a1 t-wh img-add1"> 
                    <span class="ui-btn-inner ui-btn-corner-all"> 
                    <span class="ui-btn-text t-wh">拍照</span> 
                    </span>
            </div>
            <div ontouchstart="zy_touch('c-m2');" data-role="button" onclick="picClose();" class="btn uba b-bla uinn5 c-bla c-m1 uc-a1 t-wh img-add1"> 
                    <span class="ui-btn-inner ui-btn-corner-all"> 
                    <span class="ui-btn-text t-wh">取消</span> 
                    </span>
            </div>
    </div>

</div>
</body>
<script>
zy_init();
window.uexOnload=function(type)
{
	if(!type){
		uexWindow.setBounce("1");
		uexWindow.showBounceView("0","#FFF","0");
		uexWindow.showBounceView("1","#FFF","0");
	}
}
        var uploadHttp = "http://www.huayan.cd/json/bbs/index.php";
        function setLog(msg){
                document.getElementById("msgid").innerHTML = msg;
        }
        function upload(){
				if($$("fb-title").value=='' || $$("fb-content").value==''){
					alert("标题或内容不能为空");
				}else{
					uexUploaderMgr.createUploader(1,uploadHttp);
					var fbtitle = $$("fb-title").value;
					var fbcontent = $$("fb-content").value;
					var url = 'http://localhost/json/bbs/index.php?title='+fbtitle+'&content='+fbcontent;
					$.getJSON(url,function(data){
						alert(data.title);
						alert(data.content);
						alert(data.time);
						alert(data.time1);
					});
				}
        }
        function picSeclet(){
                document.getElementById("yinying").style.display = "block";
                document.getElementById("divPic").style.display = "block";
        }
        function picClose(){
                document.getElementById("yinying").style.display = "none";
                document.getElementById("divPic").style.display = "none";
        }
        function del(){
                document.getElementById("show_img").style.display = "none";
                document.getElementById("showPic").src = "";
        }
        var upload_image_url = "";
        window.uexOnload = function(){
                uexCamera.cbOpen = function(opCode, dataType, data){
                        upload_image_url = data;
                        document.getElementById("showPic").src = data;
                        document.getElementById("show_img").style.display = "block";
                        document.getElementById("divPic").style.display = "none";
						document.getElementById("yinying").style.display = "none";
                }
				
                uexWidgetOne.cbError = function(opCode, errorCode, errorInfo){
                        setLog(errorInfo);
                }
                
                uexImageBrowser.cbPick=function (opCode,dataType,data){
		        if(dataType==0){
									upload_image_url = data;
	              				document.getElementById("showPic").src = data;
									document.getElementById("show_img").style.display = "block";
									document.getElementById("yinying").style.display = "none";
									document.getElementById("divPic").style.display = "none";
		        }
	           }
	
                uexUploaderMgr.cbCreateUploader =function(opCode,dataType,data){
                        if(data == 0){        
                                uexUploaderMgr.uploadFile(1,upload_image_url,"filename",4);        
                                uexWindow.toast(1,5,"图片上传中...",0);
                        }else{
                        }
                        
                }
				
                uexUploaderMgr.onStatus = function(opCode,fileSize,percent,serverPath,status){
                        switch (status) {
                                        case 0:
                                                break;
                                        case 1:
                                                uexWindow.closeToast();//关闭提示消息框
                                                uexWindow.toast(0,5,"发布成功!",2000);
                                                //uexWindow.closeToast();//关闭提示消息框
                                                uexUploaderMgr.closeUploader(1);
                                                break;
                                        case 2:
                                                uexWindow.closeToast();//关闭提示消息框
                                                uexWindow.toast(0,5,"出错啦~",2000);
                                                uexUploaderMgr.closeUploader(1);
                                                break;

                        }
                        
                }
        }
</script>
</html>
ohmygirl 2013-07-25
  • 打赏
  • 举报
回复
引用 11 楼 ohmygirl 的回复:
[quote=引用 9 楼 Eason_____________ 的回复:] [quote=引用 8 楼 xuzuning 的回复:] 看看你的表单
[/quote] 斑竹说表单,你给数据库。。 是表单部分的代码和js部分的代码。[/quote] ajax? 传递过来不就执行insert了么? 提交表单又执行一次。 不就刚好两条数据么
ohmygirl 2013-07-25
  • 打赏
  • 举报
回复
引用 9 楼 Eason_____________ 的回复:
[quote=引用 8 楼 xuzuning 的回复:] 看看你的表单
[/quote] 斑竹说表单,你给数据库。。 是表单部分的代码和js部分的代码。
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 6 楼 ohmygirl 的回复:
[quote=引用 4 楼 Eason_____________ 的回复:] [quote=引用 3 楼 jordan102 的回复:] index.php 代码就只有这些? 是不是执行后又刷新了?跳转了?
还有就是js传过来的 title 和 content [/quote] js传来的title和content?怎么传递的 会不会js传递时执行了脚本,提交上传图片时又执行了insert。所以是两条记录?[/quote] json传递的
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 8 楼 xuzuning 的回复:
看看你的表单

xuzuning 2013-07-25
  • 打赏
  • 举报
回复
看看你的表单
  • 打赏
  • 举报
回复
引用 5 楼 Eason_____________ 的回复:
[quote=引用 3 楼 jordan102 的回复:] index.php 代码就只有这些? 是不是执行后又刷新了?跳转了?
如果判断一下index.php

if($_GET['title']!=''){
$sql="insert into json_bbs values('','".$gettitle."','".$getcontent."','".$i->iamgePath."','".time()."','')";
mysql_query($sql);
}
在执行的话 就只插入一条数据了,但是获取不到$i->iamgePath的值,$i->iamgePath就为空。 不判断的话 是插入以下两条数据: id title content images time uid 127 aaa yyyyy 1374722311 0 128 2013_7_25_11_18_32.jpg 1374722312 0 [/quote] 你这个一看就是2个不同操作执行得到的插入 你这2个动作,一个应该是更新动作,应该说是后来执行的那个动作只能是更新动作,不能是插入动作 所以你本身逻辑上出现了问题,插入2条数据属于正常的
ohmygirl 2013-07-25
  • 打赏
  • 举报
回复
引用 4 楼 Eason_____________ 的回复:
[quote=引用 3 楼 jordan102 的回复:] index.php 代码就只有这些? 是不是执行后又刷新了?跳转了?
还有就是js传过来的 title 和 content [/quote] js传来的title和content?怎么传递的 会不会js传递时执行了脚本,提交上传图片时又执行了insert。所以是两条记录?
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 3 楼 jordan102 的回复:
index.php 代码就只有这些? 是不是执行后又刷新了?跳转了?
如果判断一下index.php

if($_GET['title']!=''){
$sql="insert into json_bbs values('','".$gettitle."','".$getcontent."','".$i->iamgePath."','".time()."','')";
mysql_query($sql);
}
在执行的话 就只插入一条数据了,但是获取不到$i->iamgePath的值,$i->iamgePath就为空。 不判断的话 是插入以下两条数据: id title content images time uid 127 aaa yyyyy 1374722311 0 128 2013_7_25_11_18_32.jpg 1374722312 0
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 3 楼 jordan102 的回复:
index.php 代码就只有这些? 是不是执行后又刷新了?跳转了?
还有就是js传过来的 title 和 content
一起混吧 2013-07-25
  • 打赏
  • 举报
回复
index.php 代码就只有这些? 是不是执行后又刷新了?跳转了?
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
求帮忙看看!
Eason_____________ 2013-07-25
  • 打赏
  • 举报
回复
引用 19 楼 chinmo 的回复:
[quote=引用 17 楼 Eason_____________ 的回复:] [quote=引用 15 楼 xuzuning 的回复:] 你的文字提交和文件上传本身就是分开的
现在插入两条的问题解决了 就是插入的时候 获取不到$i->iamgePath的值。 但是当insert 是两条的时候 $i->iamgePath就会有。[/quote] 给你一个思路和建议,其实不少网站也在用,就是: 上图图片部分用iframe,在ajax提交的是时候先执行上图图片部分,然后得到返回正常的图片地址(这里还不插入数据库,纯粹上传图片)以后再执行再执行文字表单部分,这时候图片地址是用一个参数传,这样就可以和文字一起插入数据库了,也就只有一条数据了[/quote] 谢谢两位版主给的思路。 我在试一试
  • 打赏
  • 举报
回复
引用 17 楼 Eason_____________ 的回复:
[quote=引用 15 楼 xuzuning 的回复:] 你的文字提交和文件上传本身就是分开的
现在插入两条的问题解决了 就是插入的时候 获取不到$i->iamgePath的值。 但是当insert 是两条的时候 $i->iamgePath就会有。[/quote] 给你一个思路和建议,其实不少网站也在用,就是: 上图图片部分用iframe,在ajax提交的是时候先执行上图图片部分,然后得到返回正常的图片地址(这里还不插入数据库,纯粹上传图片)以后再执行再执行文字表单部分,这时候图片地址是用一个参数传,这样就可以和文字一起插入数据库了,也就只有一条数据了
加载更多回复(1)

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧