我想用php更新我的數據庫中的表。我有一個函數調用我的數據庫中設置爲可見(可見= 1)的所有頁面,並在網站上列出它們。每個頁面都有一個單選按鈕,如果單選按鈕設置爲yes(value = 1),則javascript調用下拉選項。我希望用戶能夠選擇他們的選項,並且當他們點擊信息提交按鈕(他們設置的頁面名稱和位置編號)插入到我的表格中。數據庫中的更新表動態php
這裏是下面的代碼:
<?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
** * ** * ** * ** * *UPDATE* ** * ** * ** * ** * ** * ***
我改變了名稱屬性的單選按鈕是一個計數器。我將單選按鈕名稱屬性設爲變量的原因是因爲我已動態創建了單選按鈕,每組單選按鈕都必須具有唯一的名稱,否則所有的組都鏈接在一起。
$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;
}
在執行它之前輸出你的sql到屏幕上,並嘗試在像phpMyAdmin這樣的工具中運行它來找出錯誤;) –
問題是什麼?你有錯誤嗎?一條消息說它已更新,但尚未更新?有人說它沒有更新,但它有? – andrewsi
@Terry。如何在執行之前將sql輸出到屏幕? – ristenk1