MySQL 数据库
//做了一个手机上传图片到服务器的功能。但是获取到执行insert语句时都要执行两次!//index.php
<?phpheader("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; } }}?>
回复讨论(解决方案)
求帮忙看看!
index.php 代码就只有这些?
是不是执行后又刷新了?跳转了?
index.php 代码就只有这些?
是不是执行后又刷新了?跳转了?
还有就是js传过来的 title 和 content
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
index.php 代码就只有这些?
是不是执行后又刷新了?跳转了?
还有就是js传过来的 title 和 content
js传来的title和content?怎么传递的
会不会js传递时执行了脚本,提交上传图片时又执行了insert。所以是两条记录?
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
你这个一看就是2个不同操作执行得到的插入
你这2个动作,一个应该是更新动作,应该说是后来执行的那个动作只能是更新动作,不能是插入动作
所以你本身逻辑上出现了问题,插入2条数据属于正常的
看看你的表单
看看你的表单
index.php 代码就只有这些?
是不是执行后又刷新了?跳转了?
还有就是js传过来的 title 和 content
js传来的title和content?怎么传递的
会不会js传递时执行了脚本,提交上传图片时又执行了insert。所以是两条记录?
json传递的
看看你的表单
斑竹说表单,你给数据库。。
是表单部分的代码和js部分的代码。
看看你的表单
斑竹说表单,你给数据库。。
是表单部分的代码和js部分的代码。
ajax?
传递过来不就执行insert了么?
提交表单又执行一次。
不就刚好两条数据么
看看你的表单
<!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 src="/static/imghwm/default1.png" data-src="" id=" class="lazy" style="max-width:90%" id="showPic" / alt="当执行mysql insert 时插入两条是怎么回事?" > </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>
看看你的表单
斑竹说表单,你给数据库。。
是表单部分的代码和js部分的代码。
ajax?
传递过来不就执行insert了么?
提交表单又执行一次。
不就刚好两条数据么
如果我判断一下
就是 if($_GET["title"]!=''){
}
就执行了一次。
但是插入的数据 $i->iamgePath的值就为空 这个是怎么回事?
你的文字提交和文件上传本身就是分开的
你的文字提交和文件上传本身就是分开的
嗯 这是我用appcan做的手机app.
你的文字提交和文件上传本身就是分开的
现在插入两条的问题解决了 就是插入的时候 获取不到$i->iamgePath的值。
但是当insert 是两条的时候 $i->iamgePath就会有。
这是在提交文字
105 var url = 'http://localhost/json/bbs/index.php?title='+fbtitle+'&content='+fbcontent;
106 $.getJSON(url,function(data){
这是在提交文件
152 uexUploaderMgr.uploadFile(1,upload_image_url,"filename",4);
虽然在文本提交前就启动了文件提交
102 uexUploaderMgr.createUploader(1,uploadHttp);
但一般文件上传总要慢于文本提交,所以你能先收到 get 数据,后收到 上传文件
但如果情况恰恰相反呢?
你的文字提交和文件上传本身就是分开的
现在插入两条的问题解决了 就是插入的时候 获取不到$i->iamgePath的值。
但是当insert 是两条的时候 $i->iamgePath就会有。
给你一个思路和建议,其实不少网站也在用,就是:
上图图片部分用iframe,在ajax提交的是时候先执行上图图片部分,然后得到返回正常的图片地址(这里还不插入数据库,纯粹上传图片)以后再执行再执行文字表单部分,这时候图片地址是用一个参数传,这样就可以和文字一起插入数据库了,也就只有一条数据了
你的文字提交和文件上传本身就是分开的
现在插入两条的问题解决了 就是插入的时候 获取不到$i->iamgePath的值。
但是当insert 是两条的时候 $i->iamgePath就会有。
给你一个思路和建议,其实不少网站也在用,就是:
上图图片部分用iframe,在ajax提交的是时候先执行上图图片部分,然后得到返回正常的图片地址(这里还不插入数据库,纯粹上传图片)以后再执行再执行文字表单部分,这时候图片地址是用一个参数传,这样就可以和文字一起插入数据库了,也就只有一条数据了
谢谢两位版主给的思路。 我在试一试

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能