2015-08-03 102 views
0

請看看這段代碼無法找出實際的錯誤編輯在表中的數據:無法使用PHP形式

這是PHP上傳代碼:

<?php 
      include_once("config.php"); 
      if(isset($_GET['pro_id'])) 
      { 
      $id=$_GET['pro_id']; 
      if(isset($_POST['submitBtn']))   { 
      $dept_id = $_POST['dept_id']; 
      $cat_id = $_POST['cat_id']; 
      /*$pro_id = $_POST['pro_id'];*/ 
      $pro_name = $_POST['pro_name']; 
      $pro_desc = $_POST['pro_desc']; 
      $pro_spec = $_POST['pro_spec']; 
      $pro_price = $_POST['pro_price']; 
      $status = 'on'; 
      $pro_keywords = $_POST['pro_keywords']; 
      //image names 
      $pro_image = $_FILES['pro_image']['name']; 
      //temp images names 
      $temp_image = $_FILES['pro_image']['tmp_name']; 

      if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='') 
        { 
      echo "<script>alert('All the fields are mandatory')</script>"; 
      exit(); 
        } 
      else 
      { 
      //upload image to folder 
      move_uploaded_file($temp_image,"images/product_images/$pro_image"); 
      $run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'"); 

      if($run_query1) 
        { 
       echo "<script>alert('Product updated successfully')</script>"; 
       exit();  
        } 
      else 
       { 
       echo "<script>alert('Errors')</script>"; 
       } 
      }   } 

      $query1 = mysqli_query($login, "select * from products1 where pro_id='$id'"); 
      $query2 = mysqli_fetch_array($query1); 
      ?> 

這種形式的部分,其中數據從檢索表,當點擊更新按鈕沒有任何反應和頁面重定向查看數據頁面,並顯示舊的數據:

<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
      <table width="650" border="0"> 

      <tr> 
       <td width="183" align="right">Department:</td> 
       <th width="231" align="left"> 
       <select name="dept_id" id="dept_id"> 
        <option>Select Department</option> 
       <?php 
       $result=dept_show(); 

       while($row=mysqli_fetch_assoc($result)) 
       { 
        echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>"; 
       }      
       ?> 
       </select></th></tr> 
       <tr> 
       <td width="183" align="right">Catagory</td> 
       <th width="231" align="left"> 
       <select name="cat_id" id="cat_id"> 
        <option>Select Catagory</option> 
       <?php 
       $result1=cat_show(); 

       while($row=mysqli_fetch_assoc($result1)) 
       { 
        echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>"; 
       }      
       ?> 
       </select></th></tr> 
       <tr> 
       <!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>--> 
       </tr> 
       <tr> 
       <td align="right">Product Name/Model:</td> 
       <td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td> 
       </tr> 
       <tr> 
       <td align="right">Product Description:</td> 
       <td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td> 
       </tr> 
       <tr> 
       <td align="right">Products Specification:</td> 
       <td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td> 
       </tr> 
       <tr> 
       <td align="right">Product Price:</td> 
       <td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td> 
       </tr> 
       <tr> 
       <td align="right">Product Image:</td> 
       <td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td> 
       </tr> 
       <tr> 
       <td></td> 
       <td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td> 
       </tr> 
       <tr> 
       <td align="right">Keywords:</td> 
       <td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td> 
       </tr> 
       <tr> 
       <td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td> 
       </tr> 
      </table> 
      </form> 
      </div> <?php } ?> 
     </td> 
     </tr> 
    </table> 
    </div> 

回答

2

形式方法是POST,並且您使用GET方法,如果廁所p

if(isset($_GET['pro_id'])) 

在這裏使用POST。

我已經在您的完整代碼中進行了更改。使用此代碼,並在需要時所做的更改(如果問題到達)

PHP代碼

<?php 
     include_once("config.php"); 

     if(isset($_POST['submitBtn']))   
     { 
     $dept_id = $_POST['dept_id']; 
     $cat_id = $_POST['cat_id']; 
     $pro_id = $_POST['pro_id'];*/ 
     $pro_name = $_POST['pro_name']; 
     $pro_desc = $_POST['pro_desc']; 
     $pro_spec = $_POST['pro_spec']; 
     $pro_price = $_POST['pro_price']; 
     $status = 'on'; 
     $pro_keywords = $_POST['pro_keywords']; 
     //image names 
     $pro_image = $_FILES['pro_image']['name']; 
     //temp images names 
     $temp_image = $_FILES['pro_image']['tmp_name']; 

     if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR $pro_image=='' OR $pro_keywords=='') 
       { 
     echo "<script>alert('All the fields are mandatory')</script>"; 
     exit(); 
       } 
     else 
     { 
     //upload image to folder 
     move_uploaded_file($temp_image,"images/product_images/$pro_image"); 
     $run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values ( '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'"); 

     if($run_query1) 
       { 
      echo "<script>alert('Product updated successfully')</script>"; 
      exit();  
       } 
     else 
      { 
      echo "<script>alert('Errors')</script>"; 
      } 
     }  
    } 

     $query2 = array(); 
     if(isset($_GET['pro_id'])) 
     { 
      $id=$_GET['pro_id']; 
      $query1 = mysqli_query($login, "select * from products1 where pro_id='$id'"); 
      $query2 = mysqli_fetch_array($query1); 
     }    
     ?> 

HTML代碼

<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
     <table width="650" border="0"> 

     <tr> 
      <td width="183" align="right">Department:</td> 
      <th width="231" align="left"> 
      <select name="dept_id" id="dept_id"> 
       <option>Select Department</option> 
      <?php 
      $result=dept_show(); 

      while($row=mysqli_fetch_assoc($result)) 
      { 
       echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>"; 
      }      
      ?> 
      </select></th></tr> 
      <tr> 
      <td width="183" align="right">Catagory</td> 
      <th width="231" align="left"> 
      <select name="cat_id" id="cat_id"> 
       <option>Select Catagory</option> 
      <?php 
      $result1=cat_show(); 

      while($row=mysqli_fetch_assoc($result1)) 
      { 
       echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>"; 
      }      
      ?> 
      </select></th></tr> 
      <tr> 
      <td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td> 
      </tr> 
      <tr> 
      <td align="right">Product Name/Model:</td> 
      <td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td> 
      </tr> 
      <tr> 
      <td align="right">Product Description:</td> 
      <td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td> 
      </tr> 
      <tr> 
      <td align="right">Products Specification:</td> 
      <td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td> 
      </tr> 
      <tr> 
      <td align="right">Product Price:</td> 
      <td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td> 
      </tr> 
      <tr> 
      <td align="right">Product Image:</td> 
      <td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td> 
      </tr> 
      <tr> 
      <td></td> 
      <td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td> 
      </tr> 
      <tr> 
      <td align="right">Keywords:</td> 
      <td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td> 
      </tr> 
      <tr> 
      <td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td> 
      </tr> 
     </table> 
     </form> 
     </div> <?php } ?> 
    </td> 
    </tr> 
</table> 

+0

如何利用這個$ _GET,我有以第一次從表中檢索數據 –

+0

你寫的代碼是不完整的。 prod_id在表單中註釋。它隱藏的領域。每當表單加載時,取消註釋並將值存儲在其中。表單提交時檢查該值。如果存在值,則更新該prod id上的數據否則不存在然後在表 –

+0

中創建新條目根據您的建議,取消註釋表單中的pro_id隱藏字段,但得到相同問題,請幫助 –

1

你pro_id字段在HTML中使用註釋<!---->,所以下面從來都不是這樣:

if(isset($_GET['pro_id']))

此外,您的表單方法POST和您正在尋找的$ _GET之間存在不匹配。

+0

彼得,無法理解,請解釋。 –

0

嘗試做這步:

的第一件事評論了這條線,

<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>--> 

下一步, 您發送使用POST即(形式方法= 「後」),所以在使用數據這

if(isset($_POST['pro_id'])) , then comment out $pro_id = $_POST['pro_id']; 

您將得到$ pro_id值。

+0

在您的建議完成所有更改後,這次不會顯示使用if(isset($ _ POST ['pro_id']))的表單,請幫助。 –

+0

<! - 」/> - > ---(從html文件格式註釋掉這行,並在值字段中稍作修改,那裏是

+0

我想你是如果(isset($ _ POST ['pro_id']))。我認爲你把這個放在html文件中。不要把它放在html中,你必須把它放在php代碼中。你已經放置if(isset($ _ GET ['pro_id'])),只需將$ _GET更改爲$ _POST並註釋掉$ pro_id = $ _POST ['pro_id']; ,它肯定會工作 –

1

您的詢問更新是正確的?我認爲你必須使用

UPDATE products1 SET dept_id='$dept_id',cat_id ='$cat_id'... the rest of values 
WHERE pro_id='$id' 

,並確認,如果你的dept_id爲是INT以及CAT_ID,所以如果他們INT你不需要'

UPDATE products1 SET dept_id=$dept_id,cat_id =$cat_id 
+0

dept_id,cat_id&pro_id是INT數據類型,我也更新了查詢,但結果相同,即使相同的代碼在編輯用戶部分的另一個編輯頁面中工作正常,請幫助 –

+0

是INT您不需要'$ dept_id'只是嘗試沒有''像每個INT列中的$ dept_id – bicho

+0

不工作...... :( –