2015-09-14 51 views
2

這裏我試圖通過模態彈出窗體將文本區域和隱藏字段的值傳遞給php。更新數據庫時缺少參數錯誤

我有一個隱藏字段,其中包含product ID,文本區域包含一些文本。當我點擊提交按鈕時,它應該傳遞給PHP並在數據庫中更新。 當我填寫表格,然後點擊提交我收到錯誤

Notice: Undefined variable: prod_id 

Warning: Missing argument 1 for updateProduct() 

HTML

<div class="modal fade" id="rejectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog" role="document"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
     <h4 class="modal-title" id="myModalLabel">Are you sure to reject this product?</h4> 
     </div> 
     <div class="modal-body"> 
     <h4>Why do you want to reject this product!</h4> 
     <form action="product-reject.php" method="get" role="form" enctype="multipart/form-data"> 
     <?php 
     echo '<input type="hidden" class="form-control" name="product_id" id="user_role" value="'.$qs.'">'; 
     echo '<textarea cols="43" rows="5" class="reject-textarea" name="reject_reason"></textarea>'; 
     ?>     
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
     <input type="submit" class="btn btn-primary" value="OK"> 
     </div> 
     </form> 
    </div> 
    </div> 
</div> 

<?php 
    require_once('configuration.php'); 
    $product_id = $_GET['product_id']; 
    $reject_for = $_GET['reject_reason'];  

      if (isset($product_id,$reject_for)){ 
       rejectProduct($product_id,$reject_for); 
      } else { 
       echo "Are you trying to do something nasty??"; 
      } 

      function rejectProduct($product_id,$reject_for) 
      { 
       $conn = new mysqli(db_host, db_user, db_password, db_name); 
       if ($conn->connect_error) { 
        die("Connection failed: " . $conn->connect_error); 
       } 
       $sql = "INSERT INTO rejected_products(product_id,reason_to_reject) VALUES ($product_id,'$reject_for')"; 
       $result = $conn->query($sql); 
       if ($conn->query($sql) === TRUE) { 
        updateProduct($prod_id);   
       } else { 
        echo "Error updating record: " . $conn->error; 
       } 
       $conn->close(); 
      } 


      function updateProduct($prod_id) 
      { 
       $conn = new mysqli(db_host, db_user, db_password, db_name); 
       if ($conn->connect_error) { 
        die("Connection failed: " . $conn->connect_error); 
       } 
       $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";  
       $result = $conn->query($sql); 
       if ($conn->query($sql) === TRUE) { 
        updateProduct(); 
        header("Location: index.php"); 
       } else { 
        echo "Error updating record: " . $conn->error; 
       } 
       $conn->close(); 
      } 
?> 

我怎樣才能解決這個問題?

+0

是什麼'的UpdateProduct($ PROD_ID)'$ prod_id';'? – Lal

+0

發現SQL注入 – Naruto

+0

爲什麼要遞歸調用'updateProduct()'? – jeroen

回答

1

$prod_id在你的PHP代碼是不明確的。

如果您想要的產品ID作爲參數傳遞,然後更換

updateProduct($prod_id); 

updateProduct($product_id); 
+0

我認爲它可能是DV'd,因爲它沒有完全回答這個問題,是的,你已經解決了一個問題,但仍然有我缺少的arg錯誤 –

0

您正在使用$ PROD_ID時,你應該叫

updateProduct($product_id); 
0

與$ PRODUCT_ID替換$ PROD_ID。

function updateProduct($product_id) // Make Changes Here 
{ 
    $conn = new mysqli(db_host, db_user, db_password, db_name); 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";  
    $result = $conn->query($sql); 
    if ($conn->query($sql) === TRUE) { 
     updateProduct(); 
     header("Location: index.php"); 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 
    $conn->close(); 
} 
0

你已經這樣做了這裏:

function updateProduct($prod_id) 
{ 
    $conn = new mysqli(db_host, db_user, db_password, db_name); 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";  
    $result = $conn->query($sql); 
    if ($conn->query($sql) === TRUE) { 
     updateProduct(); 
     header("Location: index.php"); 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 
    $conn->close(); 
} 

if ($conn->query($sql) === TRUE) { updateProduct(); header("Location: index.php"); }

它應該有$prod_id在那裏還,這就是它缺少在哪裏。

如果我能提一個建議,以幫助您這我會建議使用帶有語法高亮的IDE,它將輕鬆快速地顯示了這樣的錯誤,我怎麼它發現它:)

而且還,你不需要做$conn->query($sql) === TRUE,if爲你處理真假比較。

另外,在第一個功能你做

$result = $conn->query($sql); 
if ($conn->query($sql) === TRUE) { 

2數據庫的行程?這不是很貴嗎?

而且使用的是(再次第一個函數)

updateProduct($prod_id); 

當所給出的參數是$product_id(我認爲其他用戶已經提到了這一點)。

另一個!在查詢中執行表名和列名時,最好使用「**`**」(Esc鍵下的鍵)的反勾號,並用單引號「'」環繞字符串,然後轉義變量,例如:

$sql = "INSERT INTO `rejected_products`(`product_id`, `reason_to_reject`) VALUES ('{$product_id}','{$reject_for}');"; 

一個結構良好的查詢是一個快樂的查詢:)