-1

何らかの理由で、この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."; 
}

?>
4

3 に答える 3

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. フォームidのアクションURLにを含める
  2. 提出物をチェックします
  3. 送信された値を検証します
  4. データベースクエリを作成します
  5. クエリを送信します
  6. クエリが正常に機能したことを確認します

修正済み: asに置き換えis_int()is_numeric()RTFMを実行した後、数字のみで構成される文字列は、でfalseテストすると明らかに返されることがわかりましたis_int()

于 2012-11-19T01:12:18.487 に答える
0

アップデート

$_REQUESTOR $_GETORを使用してください$_POST。ただし、3つすべてではありません。

mysql_real_escape_stringまた、変数を使ってみません$_POST['paypal']か?

于 2012-11-19T00:20:42.020 に答える
0

$_GET変数を混ぜ$_POSTます。GETまたはPOSTのいずれかを使用する必要がありますが、両方を使用することはできません。これがPOSTリクエストの場合は、に変更$_GET['id']$_POST['id']ます。

この場合、が原因で更新が失敗することはありませんwhere id = ''id空の文字列がないため、これは何も更新しません。ただし、これは有効な更新ステートメントであるため、失敗することもありません。

于 2012-11-19T00:21:14.870 に答える