2012-07-31 38 views
-1

我試圖讓編輯功能編輯產品, 像小CMS更新MySQL表。

的問題是,我不能用的$ id = $ _GET [ '身份證']更新我的MySQL表 ;然後$ query =「UPDATE producten SET naam ='$ naam'WHERE id ='$ id'」; 我搜索了整個互聯網,但找不到任何幫助。

當我嘗試的$ id = '50';例如I'T確實與50

的ID更新產品如果我嘗試的$ id = $ _GET [ '身份證']; echo $ id;我確實得到了正確的身份證。

這裏是我的全碼:

 <?php   
     include "../php_includes/config.php"; 

     //Conect to the server/database 
     if($connect){ 
      $errors = array(); 

     // check if the 'id' variable is set in URL, and check that it is valid 
     if (isset($_GET['id']) && is_numeric($_GET['id'])) 
     { 
     $id = $_GET['id']; 
     } 
     $query = mysql_query("SELECT * FROM producten WHERE id='$id'"); 
     while($query_row = mysql_fetch_assoc($query)){ 
       $naam_oud = $query_row['naam']; 
       $naam_de_oud = $query_row['naam_de']; 
       $prijs_oud = $query_row['prijs']; 
       $image_oud = $query_row['image']; 
     } 
     //Display Posts 
     if(isset($_POST['naam'],$_POST['naam_de'], $_POST['prijs'])){ 
      $naam = mysql_real_escape_string(htmlentities($_POST['naam'])); 
      $naam_de = mysql_real_escape_string(htmlentities($_POST['naam_de'])); 
      $prijs = mysql_real_escape_string(htmlentities($_POST['prijs'])); 
      $image = $_FILES['file']['name']; 
      $tmp_name = $_FILES['file']['tmp_name']; 

      if($image){ 
       $location = "product_foto/.$image"; 
       move_uploaded_file($tmp_name,$location); 

      }else{ 
       $location = $image_oud; 
      } 
    //Errors 
      if(empty($naam)){ 
       $errors[] = '<p class="error">Vul een nederlandse product naam in.</p>'; 
      } 
      if(empty($naam_de)){ 
       $errors[] = '<p class="error">Vul een duitse product naam in.</p>'; 
      } 

      if(empty($prijs)){ 
       $errors[] = '<p class="error">Vul een prijs in</p>'; 
      } 
    //Insert Into Database 
      if (empty($errors)){ 
       $query = "UPDATE producten SET naam='$naam',naam_de='$naam_de',prijs='$prijs', image='$location' WHERE id='$id'"; 
        if (mysql_query($query)){ 
         header('Location: ../admin/admin.php');die();    
        } else{ 
         $errors[] = '<p class="error">Oeps.. Er is iets verkeerd gegaan. Probeer later opnieuw.</p>'; 
        } 
    //Errors 
      } else{ 
       foreach($errors as $error){ 
       echo $error; 
       } 
      } 
     } 
     }else{ 
      echo'<p class="error">Kan geen verbinding maken.<br/> Probeer later opnieuw.</p>'; 
     } 
    ?> 


      <form class="margin" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="POST" enctype='multipart/form-data'> 
       <strong>Product naam Nederlands</strong> <br/> <input value="<?php echo $naam_oud;?> " id="focus" type="text" class="input_field" name="naam" maxlength="150" style="width:600px;"><br/><br/> 
       <strong>Product naam Duits</strong> <br/> <input id="focus" type="text" class="input_field" value="<?php echo $naam_de_oud;?>"name="naam_de" maxlength="150" style="width:600px;"><br/><br/> 
       <strong> Prijs</strong> <br/> <input id="focus" type="text" class="input_field" name="prijs" value="<?php echo $prijs_oud;?> "maxlength="150" style="width:600px;"><br/> 
       Vorm: euros,centen. zonder euro tekens. (Toevoegingen kunnen na het bedrag geplaatst worden.)<br/><br/> 
       <strong> Foto</strong> <br/><input type="file" name="file"/><br/><br/> 
       <br/><input type="submit" class="button" value="Bewerken" id="submit"/><a href="../admin/admin">Annuleren</a> 
      </form> 
+1

我會'只是請求mysql_query前添加'的var_dump($查詢),看看那是什麼出來的完整聲明。如果語句正確,您可以嘗試對數據庫手動運行它,看看它是否符合您的期望。 – Diego 2012-07-31 16:17:14

+0

當你說你無法更新時,你看到的錯誤是什麼? – ernie 2012-07-31 16:17:19

+0

$ _GET ['id']從哪裏來?你能告訴我那個代碼嗎? – Jim 2012-07-31 16:17:39

回答

0
// check if the 'id' variable is set in URL, and check that it is valid 
    if (isset($_GET['id']) && is_numeric($_GET['id'])) 
    { 
     $id = $_GET['id']; 
    } 

你驗證,如果這個說法是正確的並且$ id是真正集?

+0

顯然,他做到了。 「如果我嘗試$ id = $ _GET ['id']; echo $ id;我確實得到正確的ID。」 – Diego 2012-07-31 16:18:19

+0

我不能確認他在他的if語句中測試了這個:) – Sutuma 2012-07-31 16:19:11

+0

我會添加$ id = intval($ _ GET ['id'])來防止SQL注入 – Waygood 2012-07-31 16:23:25

0

試試這個

"UPDATE producten SET naam='".$naam."', naam_de='".$naam_de."', prijs='".$prijs."', image='".$location."' WHERE id='".$id."'";