我正在尝试使用 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 .= " <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 .= " <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 .= " <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 .= " <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;
}