以一种形式

时间:2016-02-14 06:14:11

标签: php

昨天我的问题是关于插入和更新的一种形式我做了但插入了数据,当我点击displayp.php上可用的编辑链接时,它显示所有数据的字段但是当我更新任何数据并点击提交按钮不更新,但它下次在数据库中插入数据,我在数据库中获得多个记录。实际上我已经插入和更新如果条件当我更新它没有移动其他部分所以请帮助我...谢谢你..

<div id="page-wrapper" >
<div id="page-inner">
<div class="row">
   <div class="col-md-12">
      <h2>Add Product</h2>
   </div>
</div>
<!-- /. ROW  -->
<hr />
<div class="row">
<?php 
   include('config.php');
   if($_SERVER['REQUEST_METHOD']=='POST')
   {
   if(isset($_POST['submit'])=='POST')
   {

   echo "working"; 
   $fileName=$_FILES["filepdf"]["name"];
   $fileSize=$_FILES["filepdf"]["size"]/2024;
   $fileType=$_FILES["filepdf"]["type"];   
   $fileTmpName=$_FILES["filepdf"]["tmp_name"];  

   if($fileType=="application/pdf"){
   if($fileSize<=200){

   //New file name
   $random=rand(1111,9999);
   $newFileName=$random.$fileName;

   //File upload path
   $uploadPath="testUpload/".$newFileName;

   //function for upload file

   if(move_uploaded_file($fileTmpName,$uploadPath))
   {
   echo "Successful<BR>"; 
   echo "File Name :".$newFileName."<BR>"; 
   echo "File Size :".$fileSize." kb"."<BR>"; 
   echo "File Type :".$fileType."<BR>"; 
   }
   else
   {


   echo "Maximum upload file size limit is 200 kb";
   }
   }
   else
   {


   return false;


   echo "You can only upload a pdf doc file.";
   }  


       echo "here downld";


   $updates = isset($_POST['updates']) ? 'Yes' : 'No';

       $catnm=$_POST['catnm'];
       $prodnm=$_POST['prodnm'];
       $prod_dtl=$_POST['prod_dtl'];

       //$updates=$_POST['downld'];
       $date=$_POST['date'];



   $query=("insert into addproduct(catnm,prodnm,prod_dtl,pdf,downld,date)


   values('$catnm','$prodnm','$prod_dtl','$uploadPath','$updates','$date')");
       $result = mysql_query($query,$conn);
       echo"record inserted";
       if($result)
       {
           header("location:addProduct.php");
       }
   }

   }
   }
   else if($_SERVER['REQUEST_METHOD']=='GET')
   {
   if(isset($_GET['id']))
   {

   $id = $_GET['id'];
   if(isset($_POST['submit']))
   {




   $updates['downld'] = isset($_POST['downld']) ? 'Yes' : 'No';
   $cat['catnm']=$_POST['catnm'];
   $prodnm['prodnm']=$_POST['prodnm'];
   $prod_dtl['prod_dtl']=$_GET['prod_dtl'];
   $pdf['pdf']=$_GET['pdf'];
   $downld['downld']=$_GET['downld'];
   $date['date']=$_GET['date'];



   $query3=mysql_query("update addproduct set catnm='$cat',            
   prodnm='$prodnm',prod_dtl='$prod_dtl'


   ,pdf='$pdf',downld='$updates',date='$date' where id='$id'");

   if($query3)
   {
       header('location:addProduct.php');
   }
   }


   $query1=mysql_query("select * from addproduct where id='$id'");
   $query4=mysql_fetch_array($query1);
   //echo "<pre>";
   //print_r($query4);
   }
   }
   ?>
<div class="col-md-6 col-sm-12 col-xs-12">
   <div class="panel-body">
      <form role="form"  method="post" 
         enctype="multipart/form-data"  name="myform" >
         <br/>
         <label>Select Category:</label>      
         <div class="form-group">
            <?php
               include('config.php');


               $query1=mysql_query("select catnm from addcategory");
               ?>
            <select class=form-control m-b-10 name=catnm id=catnm >
            <?php
               echo"<option>---Select---</option required>";
               while($query=mysql_fetch_array($query1))
               {

                  $selectedCat = '';
                  if($query[catnm] == $query4['catnm'])
                  {
                      $selectedCat = "selected";
                  }



               echo "<option value='$query[catnm]' $selectedCat >$query[catnm]</option>";


               }

               ?>                                    
            </select>
            <br/>                
            <div class="form-group"> 
               <input type="text" class="form-control" 
                  id="prodnm" placeholder="Product Name" name="prodnm" required 
                  value= " <?php if(isset($query4['prodnm'])){echo $query4['prodnm'];}?>"/>
            </div>
            <div class="form-group">        
               <textarea class="form-control" id="catdtl" 
                  name="prod_dtl" placeholder="Product Detail"  
                  "rows="10" cols="62" required>
               <?php if(isset($query4['prod_dtl'])){
                  echo $query4['prod_dtl'];} ?>
               </textarea>
            </div>
            <div class="form-group">
               <label for="exampleInputFile">PDF File</label>
               <input type="file" id="exampleInputFile" 
                  accept="application/pdf" name="filepdf" required >  
               file : <?php if(isset($query4['pdf'])){
                  echo $query4['pdf'];} ?>                        
            </div>
            <div class="checkbox">
               <label>
               <input id="checkbox" type="checkbox" name="updates" required 
                  <?php  if(isset($query4['downld'])) 
                     { if($query4['downld'] == 'Yes') 
                     echo "checked='checked'"; echo $query4['downld'];} 
                                 ?> />
               Is Downloadable <br />
               </label>
            </div>
            <input type="text"  
               placeholder="Date of Upload" id="datepicker-7" 
               name="date"  value="<?php if(isset($query4['date'])){
                  echo $query4['date'];}?>" required  />
            <div style="padding-top:20px;padding-bottom:40px;width:650px;">
               <input type="submit" name="submit" id="button" tabindex="2"/>
               <hr/>
      </form>
      </div>
      <!-- /. ROW  -->
      </div>
      <!-- /. PAGE INNER  -->
   </div>
   <!-- /. PAGE WRAPPER  -->
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我昨天回答你,但你没有添加带id的隐藏输入。这可以吗?

  

insert and update in one form in php

另外,您如何设想帖子

if(isset($_GET['id']))
{

$id = $_GET['id'];
if(isset($_POST['submit']))
{

*  *  *
}
}