2012-11-19 39 views
-1

由於某種原因,此sql正在執行並輸出: 已成功將以下PayPal按鈕添加到此產品中...但它沒有更新。我會很感激這方面的幫助。爲什麼此SQL執行但不更新任何內容?

if(isset($_REQUEST['submitedform'])) { 

    if ($_POST['paypal']) { 

     $paypal=$_POST['paypal']; 

     $id = $_GET['id']; 

     $query = "UPDATE `video_info` SET paypal_button_html='".$paypal 
     ."' WHERE id='".mysql_real_escape_string($id) ."'"; 

     mysql_query($query) or die(mysql_error()); 
     echo "successfully added the following paypal button to this product: 
     <br /><br /> 
     {$paypal}"; 
    } 
} 

?> 

<? 
if ($_GET['id']) { 
?> 
<h1>Add PayPal Button In for this product:</h1> 
<form action="add_paypal.php" method="POST"> 
    *Paypal button html: <br><textarea rows="2" cols="20" name="paypal"></textarea><br> 
    <input type="hidden" name="submitedform" value="true" /> 
    <input type="submit" value="Add paypal button in for this product"> 
</form> 

<? 

} else { 

    echo "You can not come to this page manually."; 
} 

?> 

回答

1

的幾個問題:

  • 你是不是在消毒您的數據庫輸入
  • 一致的你沒有明確的驗證規則
  • 你的形式不設置$_GET['id']場(所以數據庫提交總是不及格)

修訂代碼:

<?php 

// Init an Array to hold any error messages 
$errors = array(); 

if(isset($_REQUEST['submitedform'])){ 

    // Validate the required fields 
    if(!isset($_POST['paypal']) || $_POST['paypal']=='') 
    $errors['paypal'] = 'No value for "paypal"'; 
    if(!isset($_GET['id']) || !is_numeric($_GET['id'])) 
    $errors['id'] = 'No value for "id"'; 

    // If Validation was successful 
    if(!$errors){ 

    // Prepare the Variables for Database Usage 
    $paypal = mysql_real_escape_string($_POST['paypal']); 
    $id = (int) $_GET['id']; 

    // Template and Complete the SQL Query 
    $sqlTpl = 'UPDATE `video_info` SET paypal_button_html="%s" WHERE `id` = %s'; 
    $sqlStr = sprintf($sqlTpl , $paypal , $id); 

    // Submit the Query 
    if(!mysql_query($sqlStr)){ 

     // Something went wrong 
     $errors[] = 'An error occured when submitting the data to the database'; 

    }else{ 

     // Submitted OK 
     echo 'Successfully added the following paypal button to this product:'.$paypal; 

    } 

    } 

} 

// Check for any errors 
if($errors){ 

    // Show errors to user 
    echo 'The following errors occurred:'; 
    echo '<ul><li>'.implode('</li><li>' , $errors).'</li></ul>'; 

} 

?> 

<? 
if(isset($_GET['id']) && is_int($_GET['id'])){ 
?> 
<h1>Add PayPal Button In for this product:</h1> 
<form action="add_paypal.php?id=<?php echo $_GET['id']; ?>" method="POST"> 
    *Paypal button html: <br><textarea rows="2" cols="20" name="paypal"></textarea><br> 
    <input type="hidden" name="submitedform" value="true" /> 
    <input type="submit" value="Add paypal button in for this product"> 
</form> 

<? 

} else { 

    echo "You can not come to this page manually."; 
} 

?> 

此代碼...

  1. 包括表單的操作URL的id
  2. 檢查已提交
  3. 驗證提交的值
  4. 創建數據庫查詢
  5. 提交查詢
  6. 檢查查詢是否正常

修訂:is_numeric()替換is_int()如,RTFMing後,我發現一個字符串,由只有數字,顯然將返回false如果與is_int()測試。

+0

omg非常感謝你!!!!!!!是的,我看到了is_int的東西,並取而代之。非常感謝你爲我做這些:) –

0

UPDATE

請使用$_REQUEST OR $_GET$_POST但不是所有的人都3。

另外,你爲什麼不mysql_real_escape_string變量$_POST['paypal']

+0

我使用Request來避免重複提交。 我使用獲取,因爲我從其他頁面獲取數據,通過url將其發送到此頁面。 我拿出mysql_real_escape_string,因爲我認爲這是問題所在。 謝謝你的時間。 –

+0

那麼,也許這個ID根本不存在?這樣,聲明成功,但不會改變任何內容。 –

0

您混合$_GET$_POST變量。您應該使用GET或POST,但不能同時使用。如果這是發佈請求,請將$_GET['id']更改爲$_POST['id']

在這種情況下,更新不會因爲where id = ''而失敗。這不會更新任何內容,因爲沒有空字符串id。但它也不會失敗,因爲它是一個有效的更新聲明。

+0

我不得不從url中獲取id值,因爲它從另一個頁面發送了這些數據。 –

+0

所以你說什麼都行不通。謝謝你的時間。 –

+0

試試'echo $ query;' – Gaui