[英]PHP form not processing
嗨,我的網站http://www.prupt.com/edit_subject.php下有以下實時版本的代碼(來自教程) 。
該頁面具有一個表格,可讓您在左側導航欄中編輯主題。 例如,您可以單擊“ About Widget Corp”,主題文本字段中將出現名稱“ About Widget Corp”,此時您應該可以對其進行編輯(即,如果願意,可以更改其名稱),然后單擊“編輯主題”,它將在左側導航欄中更新新名稱。
根據教程,這就是應該做的。 但是,如果我嘗試編輯其中一個名稱,然后單擊“編輯主題”,則不會進行任何更改。 我猜它沒有更新數據庫,此后沒有將正確/新數據輸出到導航欄
您是否在下面的代碼中看到任何可以解釋為什么單擊“編輯主題”后不更新導航欄的內容?
<?php
//1.Create a database connection
$connection = mysql_connect("98.130.0.87", "username", "password");
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db("C263430_testorwallo" ,$connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
?>
<?php require_once("includes/functions.php"); ?>
<?php
if (intval($_GET['subj']) == 0) {
redirect_to("content.php");
}
if (isset($_POST['submit'])) {
$errors = array();
$required_fields = array('menu_name', 'position', 'visible');
foreach($required_fields as $fieldname) {
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] !=0)) {
$errors[] = $fieldname;
}
}
$fields_with_lengths = array('menu_name' => 30);
foreach($fields_with_lengths as $fieldname => $maxlength ) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) {
$errors[] = $fieldname; }
}
if (empty($errors)){
//Perform Update
$id = mysql_prep($_GET['subj']);
$menu_name = mysql_prep($_POST['menu_name']);
$position = mysql_prep($_POST['position']);
$visible = mysql_prep($_POST['visible']);
$query = "UPDATE subjects SET
menu_name = '{$menu_name}',
position = {$position},
visible = {$visible}
WHERE id = {$id}";
$result = mysql_query($query, $connection);
if (mysql_affected_rows() == 1) {
//Success
} else {
//Failed
}
} else {
// Errors occurred
}
} //end: (isset($_POST['submit']))
?>
<?php find_selected_page();?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<?php echo navigation($sel_subject, $sel_page); ?>
</td>
<td id="page">
<h2>Edit Subject <?php echo $sel_subject ['menu_name'];?></h2>
<form action="edit_subject.php?subj=<?php echo urlencode($sel_subject['id']);?>" method="post">
<p>Subject name: <input type="text" name="menu_name" value="<?php echo $sel_subject['menu_name']; ?>" id="menu_name" /></p>
<p>Position:
<select name="position">
<?php
$subject_set = get_all_subjects();
$subject_count = mysql_num_rows($subject_set);
//$subject_count +1 because we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if ($sel_subject['position'] == $count) {
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0"<?php
if ($sel_subject['visible'] == 0) { echo " checked";}
?>/>No
<input type="radio" name="visible" value="1"<?php
if ($sel_subject['visible'] == 1) { echo " checked"; }
?>/> Yes
</p>
<input type="submit" name"submit" value="Edit Subject"/>
</form>
<br/>
<a href="content.php">Cancel</a>
</td>
</tr>
</table>
<?php include("includes/footer.php"); ?>
<?php
//5. Close connection
mysql_close($connection);
?>
好的,看到了頁面代碼,並且很有可能(請參見上面的注釋)。
<input type="submit" name"submit" value="Edit Subject"/>
您忘記了=
號,將其更正為name="submit"
。 這就是為什么它看不到表單已提交(如果$ _POST ['submit'] ...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.