PHP development corporate website tutorial to modify products
It is also queried in the database based on the parameter id brought by the form submission
The code is as follows:
<?php require_once('conn.php'); $id = $_GET['id']; $sql = "SELECT * from product where id= '$id'"; $res = mysql_query($sql); $info = mysql_fetch_array($res); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>修改產(chǎn)品</title> <style type="text/css"> #cnt{ width:400px; height:400px; margin-top:15px; margin-left: 15px; } iframe{border:0px;} </style> </head> <body> <div id="cnt"> <form method="post" action="modifyproduct.php?id=<?php echo $info['id']?>" enctype="multipart/form-data"> 產(chǎn)品名稱:<input type="text" name="title" id="title" value="<?php echo $info['title']?>"></br></br> <iframe src="file.php"></iframe> <input type="text" id="imgname" name="imgname" value="<?php echo $info['imgname'];?>"></br></br> 產(chǎn)品價格:<input type="text" name="price" value="<?php echo $info['price']?>"> </br></br> <input type="submit" value="修改產(chǎn)品"> </form> </div> </body> </html>
You can see that there is a file.php file in our form
When modifying, we may re-upload the image, so I used an iframe tag.
Let’s take a look at the code of file.php:
<?php if(!empty($_POST['go']) && $_POST['go']==1){ //接受文件,臨時文件信息 $fileinfo=$_FILES["myFile"];//降維操作 $filename=$fileinfo["name"]; $tmp_name=$fileinfo["tmp_name"]; $size=$fileinfo["size"]; $error=$fileinfo["error"]; $type=$fileinfo["type"]; //服務器端設定限制 $maxsize=10485760;//10M,10*1024*1024 $allowExt=array('jpeg','jpg','png','gif');//允許上傳的文件類型(拓展名 $ext=pathinfo($filename,PATHINFO_EXTENSION);//提取上傳文件的拓展名 //目標存放文件夾 $path="../uploads"; if (!file_exists($path)) { //當目錄不存在,就創(chuàng)建目錄 mkdir($path,0777,true);//創(chuàng)建目錄 chmod($path, 0777);//改變文件模式,所有人都有執(zhí)行權(quán)限、寫權(quán)限、度權(quán)限 } //得到唯一的文件名!防止因為文件名相同而產(chǎn)生覆蓋 $uniName=md5(uniqid(microtime(true),true)).".$ext"; //md5加密,uniqid產(chǎn)生唯一id,microtime做前綴 //目標存放文件地址 $destination=$path."/".$uniName; //當文件上傳成功,存入臨時文件夾,服務器端開始判斷 if ($error==0) { if ($size>$maxsize) { exit("上傳文件過大!"); } if (!in_array($ext, $allowExt)) { exit("非法文件類型"); } if (!is_uploaded_file($tmp_name)) { exit("上傳方式有誤,請使用post方式"); } //判斷是否為真實圖片(防止偽裝成圖片的病毒一類的 if (!getimagesize($tmp_name)) {//getimagesize真實返回數(shù)組,否則返回false exit("不是真正的圖片類型"); } //move_uploaded_file($tmp_name, "uploads/".$filename); if (@move_uploaded_file($tmp_name, $destination)) {//@錯誤抑制符,不讓用戶看到警告 echo "<script>parent.document.all.imgname.value = '$destination';</script>文件".$filename."上傳成功!"; }else{ echo "文件".$filename."上傳失敗!"; } }else{ switch ($error){ case 1:echo "超過了上傳文件的最大值,請上傳2M以下文件";break; case 2:echo "上傳文件過多,請一次上傳20個及以下文件!";break; case 3:echo "文件并未完全上傳,請再次嘗試!";break; case 4:echo "未選擇上傳文件!";break; case 7:echo "沒有臨時文件夾";break; } } } ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form method="post" action="?" enctype="multipart/form-data"> 上傳文件:<input type="file" name="myFile"></br></br> <input type="hidden" value="1" name="go"> <input type="submit" value="上傳"></br></br> </form> </body> </html>
Do it on this page Process the uploaded file
We then look down at the code of the modifyproduct.php file
<?php require_once('conn.php'); $id = $_GET['id']; $title = $_POST['title']; $imagename = $_POST['imgname']; $price = $_POST['price']; $goodtime = time(); $sql = "UPDATE product SET title='$title',imgname='$imagename',price='$price',goodtime='$goodtime' where id='$id'"; $res = mysql_query($sql); if($res){ echo "<script>alert('修改產(chǎn)品成功');location.href='product.php'</script>"; }else{ echo "<script>alert('修改產(chǎn)品失敗');history.go(-1);</script>"; } ?>
Based on the above code, we have completed the product modification function