0

データベース内のテーブルをphpで更新しようとしています。データベースで表示 (表示 = 1) に設定されているすべてのページを呼び出し、それらを Web サイトに一覧表示する関数があります。各ページにはラジオ ボタンがあり、ラジオ ボタンが yes (値 = 1) に設定されている場合、javascript はドロップダウン オプションを呼び出します。ユーザーがオプションを選択できるようにし、送信ボタンをクリックして情報 (設定したページ名と位置番号) をテーブルに挿入できるようにしたいと考えています。

以下にコードを示します。

<?php
    if (isset($_POST['submit'])) {
// Perform Update

            $name = $_POST['visible_{$page["menu_name"]}'];
            $featured_position = $_POST['featured_position'];

            $query = "UPDATE pages SET 
                    featured_position = {$featured_position}
                    WHERE menu_name = {$name}";
            $result = mysql_query($query);
            // test to see if the update occurred
            if (mysql_affected_rows() == 1) {
                // Success!
                $message = "The page was successfully updated.";
            } else {
                $message = "The page could not be updated.";
                $message .= "<br />" . mysql_error();
            }

        }
?>

<?php if (!empty($message)) {
                echo "<p class=\"message\">" . $message . "</p>";
} ?>

<form action="add_feature2.php" method="post">
<?php echo list_all_pages(); ?>

<input type="submit" name="submit" value="Edit Featured Companies" />
</form>         

<?php
        function get_all_pages() {
        global $connection;
        $query = "SELECT * 
                FROM pages ";
        $query .= "WHERE visible = 1 ";
        $query .= "ORDER BY position ASC";
        $page_set = mysql_query($query, $connection);
        confirm_query($page_set);
        return $page_set;
    }

function list_all_pages(){
$output = "<ul>";
//$output .= $counter = 0;
$page_set = get_all_pages();
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li>{$page["menu_name"]}</li>";

$output .= "&nbsp;&nbsp;<div id=\"$page[id]\" style='display: none'><select name='featured_position'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></div>";

$output .= "&nbsp;&nbsp;<input onclick=\"javascript:document.getElementById('$page[id]').style.display = 'none';\" type=\"radio\" name=\"visible_{$page["menu_name"]} \" value=\"0\" checked=\"checked\" /> No <input onclick=\"javascript:document.getElementById('$page[id]').style.display = 'block';\" type=\"radio\" name=\"visible_{$page["menu_name"]} \"value=\"1\" /> Yes";

//$output .= $counter = $counter+1;

    }
$output .= "</ul>";
return $output;
}   




?>

Web サイトへのリンクは次のとおりです。 http://www.firetreegraphics.com/widget_corp-final/add_feature2.php

** * ** * ** * ** * *更新* ** * ** * ** * ** * ** * ***

ラジオ ボタンの name 属性をカウンターに変更しました。ラジオ ボタンの名前属性を変数にした理由は、ラジオ ボタンを動的に作成し、ラジオ ボタンの各セットに一意の名前を付ける必要があるか、すべてのセットがリンクされているためです。

$name = $_POST['{$counter}'];
            $featured_position = $_POST['featured_position'];

            $query = "UPDATE pages SET 
                    featured_position = '{$featured_position}'
                    WHERE menu_name = '{$name}'";
            $result = mysql_query($query);
                            //echo($query);
                            var_dump($_REQUEST);


function list_all_pages(){
$output = "<ul>";
$counter = 0;
$page_set = get_all_pages();
while ($page = mysql_fetch_array($page_set)) {
$output .= "<li>{$page["menu_name"]}</li>";

$output .= "&nbsp;&nbsp;<div id=\"$page[id]\" style='display: none'><select name='featured_position'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></div>";

$output .= "&nbsp;&nbsp;<input onclick=\"javascript:document.getElementById('$page[id]').style.display = 'none';\" type=\"radio\" name=\"$counter\" value=\"0\" checked=\"checked\" /> No <input onclick=\"javascript:document.getElementById('$page[id]').style.display = 'block';\" type=\"radio\" name=\"$counter\" value=\"1\" /> Yes";

$counter = $counter+1;

    }
$output .= "</ul>";
return $output;
}   
4

2 に答える 2

1

変数を引用する必要があると思います:

 $query = "UPDATE pages SET  
      featured_position ='{$featured_position}'
      WHERE menu_name = '{$name}'"; 

でも

PDOまたはへの移動を検討する必要がありますmysqli_*。より安全なコードを作成するのに役立つだけでなく (現在、コードに SQL インジェクションの脆弱性があります)、すべての引用を処理してくれます。

編集:

$output .= "&nbsp;&nbsp;<div id=\"$page[id]\" style='display: none'><select name='featured_position_{$counter}'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></div>";              
$output .= "&nbsp;&nbsp;<input onclick=\"javascript:document.getElementById('$page[id]').style.display = 'none';\" type=\"radio\" name=\"visible_{$counter}\" value=\"0\" checked=\"checked\" /> No <input onclick=\"javascript:document.getElementById('$page[id]').style.display = 'block';\" type=\"radio\" name=\"visible_{$counter}\" value=\"1\" /> Yes";      

ラジオ ボタンの名前を「visible_」に変更しました。これにより、更新クエリでページの ID を使用できるようになります。選択の名前も変更したので、各行には「featured_position_」と呼ばれる独自の選択があります。

各値を確認するには、ループが必要になると思います。

$page_set = get_all_pages();             
while ($page = mysql_fetch_array($page_set)) {
    $id = $_POST["visible_" . $page["id"]];
    $featured_position = $_POST['featured_position_' . $page["id"];  

    $query = "UPDATE pages SET         
        featured_position = '{$featured_position}'        
        WHERE id = '{$name}'";

それで十分だと思います。

于 2012-09-15T17:59:05.063 に答える
0

まず第一に、PHP mysql API は推奨されておらず、ほとんどが「非推奨」です。MySQLi を使用する必要がありますが、その必要はありませんが、ほとんど同じなので、お勧めします。

私が最初に見たとき、私はあなたのクエリの1つでこの問題を見ました:

  "UPDATE pages SET 
  featured_position = {$featured_position}
  WHERE menu_name = {$name}";

おそらく{$name}文字列です。SO クエリはで終わりますがWHERE menu_name = string、引用符で囲む必要があります。

  "UPDATE pages SET 
  featured_position = {$featured_position}
  WHERE menu_name = '{$name}' ";
于 2012-09-15T17:58:54.940 に答える