0

我正在尝试使用 php 更新我的数据库中的表。我有一个函数可以调用我的数据库中设置为可见(可见 = 1)的所有页面并在网站上列出它们。每个页面都有一个单选按钮,如果单选按钮设置为是(值 = 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;
}   




?>

这是网站的链接:http ://www.firetreegraphics.com/widget_corp-final/add_feature2.php

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

我将单选按钮上的名称属性更改为计数器。我将单选按钮名称属性设为变量的原因是因为我动态创建了单选按钮,并且每组单选按钮都必须具有唯一的名称,否则所有组都链接在一起。

$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}'"; 

然而

你真的应该考虑移动到PDOor 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,你不必这样做,但它几乎相同,这就是我建议它的原因。

当我第一次查看时,我在您的一个查询中看到了这个问题:

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

大概{$name}是一个字符串。因此,您的查询以 while 结尾,WHERE menu_name = string应该用引号括起来。

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