在php

时间:2016-02-13 05:52:31

标签: php

如何使用php中的一个表单插入和更新。插入和更新无效 该怎么办。我有文件productinsert.php和一个文件显示数据displayProduct.php当我插入数据时没有插入它重定向在同一页面它不显示addProduct.php上的数据也没有更新plz帮助

 <!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/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php
include('header1.php');
?>
</head>
<body>
<?php
include('sidenav.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']))
                            {


                                $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="get" enctype="multipart/form-data"   

name="myform"  action= "<?php echo $_SERVER['PHP_SELF'];?>">
                <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 $query4['prodnm']; ?>"/>
           </div>

         <div class="form-group">       


 <textarea   class="form-control" id="catdtl" name="catdtl" 

placeholder="Product Detail"  "rows="10" cols="62" required>
             <?php 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 echo $query4['pdf']; ?>                        
        </div>

        <div class="checkbox">
        <label>
        <input id="checkbox" type="checkbox" name="updates" required 
 
 /> Is Downloadable <br />
            </label>
            </div>









<input type="text"  
placeholder="Date of Upload" id="" name="date" required />
         <div style="padding-top:20px;padding-bottom:40px;width:650px;">
         <input type="submit" name="submit" id="button" tabindex="2"/>


         <hr/>

         </form>

                <?php
                        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');
                                    }
                             }



                        }


                                    ?>

 </div>
                 <!-- /. ROW  -->
    </div>
             <!-- /. PAGE INNER  -->
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

何时需要更新添加以形成id等于行的隐藏字段以更新和命名id。如果需要插入请不要使用id行。

<form>
if(isset($_GET['update'])) echo '<input type="hidden" name="id" value="'.$_GET['id'].'">';
</form> 


  if(isset($_POST['id'])) { do sql to update where id=$_POST['id']; } else {do sql to insert}

同样要更新,您需要使用来自DB的现有数据填充输入

<input value="<?  if(isset($_GET['update'])) echo $value; ?>">
<select> <option value="1" if(isset($_GET['update']) && select1==1) echo 'selected'; >1</options>
<option value="2" if(isset($_GET['update']) && select1==2) echo 'selected'; >2</options>