blob: 3010d8540ece8e5f3e0d88a907c861d6d81f8cad [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
-->
<html>
<head>
<title>使用 PHP 创建数据库驱动的应用程序。在 MySQL 数据库中创建新的记录。</title>
<meta name="KEYWORDS" content="CRUD, Update, Delete, MySQL, PHP, NetBeans">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="DESCRIPTION" content="Creating a Database Driven Application With PHP. Creating new record in MySQL database" >
<link rel="stylesheet" type="text/css" href="../../../netbeans.css" media="screen"></head>
<body>
<h1>使用 PHP 创建数据库驱动的应用程序 </h1>
<h1>第 6 课:在数据库中添加新的愿望</h1>
<div style="margin-left:-3px">
<div class="feedback-box margin-around float-left" style="margin-right:15px">
<h4>教程目录:</h4>
<ol start="0">
<li><a href="wish-list-tutorial-main-page.html">使用 PHP 创建数据库驱动的应用程序 - 主页</a></li>
<li><p>创建数据库</p> <ol type="a"><li><a href="wish-list-lesson1.html">创建 MySQL 数据库</a></li>
<li><a href="wish-list-oracle-lesson1.html">创建 Oracle 数据库表</a></li>
</ol></li>
<li>
<p><a href="wish-list-lesson2.html">设计应用程序。从数据库读取数据</a></p>
</li>
<li><a href="wish-list-lesson3.html">创建新的应用程序用户</a></li>
<li><a href="wish-list-lesson4.html">优化代码</a></li>
<li><a href="wish-list-lesson5.html">添加安全功能。实现应用程序用户登录</a></li>
<li>
<p><b>=&gt; 在数据库中添加新的愿望</b></p>
<ul>
<li><a href="#previousLessonSourceCode">来自上一课的应用程序源代码</a></li>
<li><a href="#addNewWish">提交新的愿望</a>
<ul>
<li><a href="#add-wish-ui-elements">添加用户界面组件</a></li>
<li><a href="#inputFormAfterunsuccessfulSave">在提交失败后 <br>重新显示截止日期</a></li>
</ul></li>
<li><a href="#logonVerification">验证许愿者的登录</a></li>
<li><a href="#insert-new-wish">在数据库中插入新的愿望</a>
<ul>
<li><a href="#add-insert-wish">将 insert_wish 函数添加到 WishDB 中</a></li>
<li><a href="#add-format-date-for-sql">将 format_date_for_sql 函数添加到 WishDB 中</a></li>
<li><a href="#validateAndEnterWishToDatabase">在数据库中输入新的愿望记录</a></li>
<li><a href="#displayingErrorMessages">显示错误消息</a></li>
</ul>
</li>
<li><a href="#backToIndex">返回到主页 index.php</a></li>
<li><a href="#testingAddWishFunctionality">测试添加愿望功能</a></li>
<li><a href="#lessonResultSourceCode">完成当前课程后的应用程序源代码</a></li>
</ul>
</li>
<li><a href="wish-list-lesson7.html">更新和删除数据库中的条目</a></li>
<li><a href="wish-list-lesson8.html">使用 CSS 技术改进应用程序的外观</a></li>
<li><a href="wish-list-lesson9.html">将应用程序部署到远程 Web 服务器</a></li>
</ol>
</div>
</div>
<img alt="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0" class="stamp" src="../../../images_www/articles/73/netbeans-stamp-80-74-73.png" title="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0">
<p>在本课中,将使用以下两个功能扩展应用程序功能:</p>
<ul>
<li style="margin-left:42em"><a href="#addNewWish">添加新的愿望</a></li>
<li style="margin-left:42em"><a href="#backToIndex">返回到主页 index.php</a></li>
</ul>
<p>要实现该功能,请编辑 <tt>editWishList.php</tt> 文件并创建新文件 <tt>editWish.php</tt></p>
<img alt="突出显示第 6 课增加内容的页面流程图" height="368" src="../../../images_www/articles/72/php/oracle-wishlist/page-flow-diagram-l6.png" width="439">
<p>当前文档是<a href="wish-list-tutorial-main-page.html">在适用于 PHP 的 NetBeans IDE 中创建 CRUD 应用程序</a>教程的一部分。 </p>
<br style="clear:left">
<h2><a name="previousLessonSourceCode"></a>来自上一课的应用程序源代码</h2>
<p>MySQL 用户:单击<a href="https://netbeans.org/files/documents/4/1931/lesson5.zip" target="_blank">此处</a>以下载源代码,该代码反映了在完成上一课之后的项目状态。</p>
<p>Oracle 数据库用户:单击<a href="https://netbeans.org/projects/www/downloads/download/php%252Foracle-lesson5.zip" target="_blank">此处</a>以下载源代码,该代码反映了在完成上一课之后的项目状态。</p>
<h2><a id="addNewWish" name="addNewWish"></a>提交新的愿望 </h2>
<p>用户可使用以下步骤提交新的愿望: </p>
<ol>
<li>用户进行登录,切换到 <tt>editWishList.php</tt> 页,然后按 "Add Wish" 按钮。<tt>editWish.php</tt> 页打开,并显示 HTML 窗体。</li>
<li>用户在 HTML 窗体中输入愿望说明,可能还会输入他/她希望实现愿望的日期,然后按 "Save Changes" 按钮。 </li>
<li>如果提交窗体而未输入愿望说明,用户将返回到窗体进行重试。如果用户提交了截止日期而没有提供说明,在重新加载窗体时,将重新显示该日期。</li>
</ol>
<!--<ul>
<li>If the description is not filled in, an error message is displayed and the user returns to the editWish.php page. </li>
<li>Else, if a description is filled in, the application returns to the editWishList.php page with the list of wishes expanded with the new wish. </li>
</ul> -->
<p>要为用户实现此过程,请在应用程序中添加以下功能:</p>
<ul>
<li><a href="#add-wish-ui-elements">用户界面组件</a>,包含添加愿望的 HTML 窗体以及 <tt>editWishList.php </tt>中将用户重定向到该窗体的按钮。 </li>
<li>在提交不完整的窗体时<a href="#inputFormAfterunsuccessfulSave">重新显示截止日期</a>的代码。 </li>
</ul>
<div class="indent">
<h3 id="add-wish-ui-elements">添加用户界面组件</h3>
<p><strong>添加功能以添加新的愿望:</strong></p>
<ol>
<li><a id="addWishButton" name="addWishButton"></a>实现 "Add Wish" 按钮。在 <tt>editWishList.php</tt> 文件的 PHP 块下面添加以下 HTML 代码:
<pre class="examplecode">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="addNewWish" action="editWish.php">
&lt;input type="submit" value="Add Wish">
&lt;/form>
&lt;/body&gt;
&lt;/html&gt;</pre>
<p class="notes"><b>注:</b>您可以忽略来自 HTML 验证器的警告。</p>
<p>窗体包含 <tt>submit</tt> 类型的 "Add Wish" 输入字段。该字段实现 "Add Wish" 按钮。当用户单击 "Add Wish" 时,他们将重定向到 <tt>editWish.php</tt> 页。由于没有通过该窗体传输任何数据,因此,不会使用服务器请求方法。</p>
</li>
<li>在 addNewWish 窗体上面添加一个表以显示许愿者的现有愿望。代码类似于 <tt>wishlist.php</tt>
<p><strong>对于 MySQL 数据库</strong></p>
<pre class="examplecode">&lt;table border=&quot;black&quot;&gt;<br> &lt;tr&gt;&lt;th&gt;Item&lt;/th&gt;&lt;th&gt;Due Date&lt;/th&gt;&lt;/tr&gt;<br> &lt;?php<br> require_once(&quot;Includes/db.php&quot;);<br> $wisherID = WishDB::getInstance()-&gt;get_wisher_id_by_name($_SESSION[&quot;user&quot;]);<br> $result = WishDB::getInstance()-&gt;get_wishes_by_wisher_id($wisherID);<br> while($row = mysqli_fetch_array($result)) {<br>
echo &quot;&lt;tr&gt;&lt;td&gt;&quot; . htmlentities($row['description']) . &quot;&lt;/td&gt;&quot;;<br>
echo &quot;&lt;td&gt;&quot; . htmlentities($row['due_date']) . &quot;&lt;/td&gt;&lt;/tr&gt;\n&quot;;<br> }<br> ?&gt;<br>&lt;/table&gt;</pre>
<p><b>对于 Oracle 数据库:</b></p>
<pre class="examplecode">&lt;table border=&quot;black&quot;&gt;
&lt;tr&gt;&lt;th&gt;Item&lt;/th&gt;&lt;th&gt;Due Date&lt;/th&gt;&lt;/tr&gt;
&lt;?php
require_once(&quot;Includes/db.php&quot;);
$wisherID = WishDB::getInstance()-&gt;get_wisher_id_by_name($_SESSION[&quot;user&quot;]);
$stid = WishDB::getInstance()-&gt;get_wishes_by_wisher_id($wisherID);
while ($row = oci_fetch_array($stid)) {<br> echo &quot;&lt;tr&gt;&lt;td&gt;&quot; . htmlentities($row['DESCRIPTION']) . &quot;&lt;/td&gt;&quot;;<br>
echo &quot;&lt;td&gt;&quot; . htmlentities($row['DUE_DATE']) . &quot;&lt;/td&gt;&lt;/tr&gt;\n&quot;;
}
?&gt;
&lt;/table&gt;</pre>
</li>
<li>在 "Source Files"(源文件)文件夹中创建 <tt>editWish.php</tt> PHP 文件。</li>
<li><a id="EmptyAddWishForm" name="EmptyAddWishForm"></a><tt>editWish.php</tt> 中,实现 "Add Wish" 窗体。在 &lt;? php ?&gt; 块下键入或粘贴以下代码:
<pre class="examplecode">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name=&quot;editWish&quot; action=&quot;editWish.php&quot; method=&quot;POST&quot;&gt;<br> Describe your wish: &lt;input type=&quot;text&quot; name=&quot;wish&quot; value=&quot;&quot; /&gt;&lt;br/&gt;<br> When do you want to get it? &lt;input type=&quot;text&quot; name=&quot;dueDate&quot; value=&quot;&quot;/&gt;&lt;br/&gt;<br> &lt;input type=&quot;submit&quot; name=&quot;saveWish&quot; value=&quot;Save Changes&quot;/&gt;<br> &lt;input type=&quot;submit&quot; name=&quot;back&quot; value=&quot;Back to the List&quot;/&gt;
&lt;/form>
&lt;/body&gt;
&lt;/html&gt; </pre></li>
</ol>
<p>"Add Wish" 窗体包含:</p>
<ul>
<li>两个空文本字段,用于输入愿望说明和截止日期。</li>
<li>在输入字段旁边输出的文本。</li>
<li>表示 "Save Changes" 按钮的 <tt>submit</tt> 字段 </li>
<li>表示 "Back to the List" 按钮的 <tt>submit</tt> 字段,用于返回到 <tt>editWishList.php</tt></li>
</ul>
<p>在按 "Add Wish" 按钮后,窗体将通过请求方法 POST 将输入的数据提交到同一页面,即 <tt>editWish.php</tt></p>
<h3><a id="inputFormAfterunsuccessfulSave" name="inputFormAfterunsuccessfulSave"></a>在提交失败后重新显示截止日期 </h3>
<p>如果用户在 "Add Wish" 窗体中未填写说明,则会显示一条错误消息,并且用户会返回到 <tt>editWish.php</tt> 页。当用户返回到 <tt>editWish.php</tt> 时,"Add Wish" 窗体应显示 <tt>dueDate</tt> 值(如果已输入)。在当前的窗体实现中,这两个字段始终为空。要保留输入的值,您需要将新愿望数据保存到数组中。该数组包含两个名为 <tt>description</tt><tt>due_date</tt> 的元素。然后,您需要更改 "Add Wish" 窗体,使其能够从数组中检索 <tt>dueDate</tt> 字段值。</p>
<p><strong>注:</strong>未输入说明时,重新加载输入窗体的代码包含在<a href="#validateAndEnterWishToDatabase">用于验证数据并将其输入到数据库的代码</a>中。本部分未介绍该代码。本部分中的代码仅保留 <tt>dueDate</tt> 值,以便在重新加载窗体时显示该值。</p>
<p><strong>在用户提交输入窗体失败后重新显示该窗体:</strong></p>
<ol><li><tt>editWish.php</tt> 的 HTML &lt;body&gt; 元素内,紧靠输入窗体上面键入或粘贴以下代码块:
<pre class="examplecode">&lt;?php
if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;)<br> $wish = array(&quot;description&quot; =&gt; $_POST[&quot;wish&quot;], <br> &quot;due_date&quot; =&gt; $_POST[&quot;dueDate&quot;]);<br>else<br> $wish = array(&quot;description&quot; =&gt; &quot;&quot;, <br> &quot;due_date&quot; =&gt; &quot;&quot;);
?&gt; </pre>
<p>该代码检查使用哪个请求服务器方法传输数据,并创建一个名为 $wish 的数组。如果该方法是 POST(表示在尝试保存具有空说明的愿望失败后显示输入窗体),则 <tt>description</tt><tt>due_date</tt> 元素接受通过 POST 传输的值。 </p>
<p>如果该方法不是 POST(表示在重定向窗体后第一次显示输入窗体),则 <tt>editWishList.php</tt> 页以及 <tt>description</tt><tt>due_date</tt> 元素均为空。</p>
<p class="notes"> <strong>注:</strong>在这两种情况下,说明均为空。唯一的区别是 <tt>dueDate</tt></p></li>
<li>更新 "Add Wish" 窗体,以便从 <tt>$wish</tt> 数组中检索其输入字段的值。将 "Add Wish" 窗体中的行:
<pre class="examplecode">Describe your wish: &lt;input type=&quot;text&quot; name=&quot;wish&quot; value=&quot;&quot; /&gt;&lt;br/&gt;
When do you want to get it? &lt;input type=&quot;text&quot; name=&quot;dueDate&quot; value=&quot;&quot;/&gt;&lt;br/&gt;</pre>
替换为:
<pre class="examplecode">Describe your wish: &lt;input type=&quot;text&quot; name=&quot;wish&quot; value=&quot;&lt;?php echo $wish['description'];?&gt;&quot; /&gt;&lt;br/&gt;
When do you want to get it? &lt;input type=&quot;text&quot; name=&quot;dueDate&quot; value=&quot;&lt;?php echo $wish['due_date']; ?&gt;&quot;/&gt;&lt;br/&gt;</pre></li></ol>
</div>
<h2><a id="logonVerification" name="logonVerification"></a>验证许愿者的登录 </h2>
<tt>editWish.php</tt> 文件中,在文件顶部的 &lt;? php ?&gt; 块中输入以下会话处理代码:
<pre class="examplecode">session_start();
if (!array_key_exists("user", $_SESSION)) {
header('Location: index.php');
exit;
}</pre>
该代码将会:
<ul>
<li>打开 $_SESSION 数组以检索数据。</li>
<li>验证 $_SESSION 数组是否包含具有标识符 "user" 的元素。</li>
<li>如果检查失败(表示用户未登录),则将应用程序重定向到主页 index.php 并取消 PHP 处理。 </li>
</ul>
<p>要检查会话处理是否正常工作,请从 IDE 中运行 editWish.php 文件。index.php 页打开,因为没有通过会话将用户传输到 editWish.page。 </p>
<h2 id="insert-new-wish">在数据库中插入新的愿望</h2>
<p>在用户提交新的愿望后,应用程序需要将愿望添加到“愿望”数据库中。要实现该功能,请在应用程序中添加以下代码:</p>
<ul>
<li><tt>db.php</tt><tt>WishDB</tt> 类中添加另外两个辅助函数。
<ul><li>一个函数将新记录添加到 wishes 表中。</li><li> 另一个函数将日期转换为 MySQL 数据库服务器支持的格式。 </li></ul></li>
<li><tt>editWish.php</tt> 中添加代码,以使用 <tt>WishDB</tt> 中的新辅助函数在数据库中输入新的愿望。</li>
</ul>
<h3 id="add-insert-wish">将 insert_wish 函数添加到 WishDB 中</h3>
<p>该函数要求将许愿者 ID、新愿望说明和愿望截止日期作为输入参数,并在数据库的新记录中输入该数据。该函数不会返回任何值。 </p>
<p>打开 <tt>db.php </tt> 并将 <tt>insert_wish</tt> 函数添加到 <tt>WishDB</tt> 类中:</p>
<p><b>对于 MySQL 数据库:</b></p>
<pre class="examplecode">function insert_wish($wisherID, $description, $duedate){
$description = $this-&gt;real_escape_string($description);<br> if ($this->format_date_for_sql($duedate)==null){<br> $this->query("INSERT INTO wishes (wisher_id, description)" .<br> " VALUES (" . $wisherID . ", '" . $description . "')");<br> } else<br> $this-&gt;query(&quot;INSERT INTO wishes (wisher_id, description, due_date)&quot; . <br> &quot; VALUES (&quot; . $wisherID . &quot;, '&quot; . $description . &quot;', &quot; <br> . $this-&gt;format_date_for_sql($duedate) . &quot;)&quot;);
}</pre>
<p><b>对于 Oracle 数据库:</b></p>
<pre class="examplecode">function insert_wish($wisherID, $description, $duedate) {
$query = "INSERT INTO wishes (wisher_id, description, due_date) VALUES (:wisher_id_bv, :desc_bv, to_date(:due_date_bv, 'YYYY-MM-DD'))";
$stid = oci_parse($this->con, $query);
oci_bind_by_name($stid, ':wisher_id_bv', $wisherID);
oci_bind_by_name($stid, ':desc_bv', $description);
oci_bind_by_name($stid, ':due_date_bv', $this->format_date_for_sql($duedate));
oci_execute($stid);
oci_free_statement($stid);
}</pre>
<p>该代码调用 format_date_for_sql 函数,以将输入的截止日期转换为数据库服务器可以处理的格式。然后,执行 INSERT INTO wishes (wisher_id, description, due_date) 查询以在数据库中输入新的愿望。</p>
<h3 id="add-format-date-for-sql">将 format_date_for_sql 函数添加到 WishDB 中</h3>
<p><tt>format_date_for_sql</tt> 函数添加到 <tt>db.php</tt><tt>WishDB</tt> 类中。该函数需要一个字符串,并将日期作为输入参数。该函数返回具有数据库服务器可处理的格式的日期;如果输入字符串为空,则返回<tt>空值</tt></p>
<p class="notes"><b>注:</b>本示例中的函数使用 PHP <tt>date_parse</tt> 函数。该函数仅适用于英语语言的日期(如 December 25, 2010)和阿拉伯数字。专业 Web 站点使用日期选取器。</p>
<strong>对于 MySQL 数据库:</strong>
<pre class="examplecode">function format_date_for_sql($date){<br> if ($date == &quot;&quot;)<br> return null;<br> else {<br> $dateParts = date_parse($date);<br> return $dateParts[&quot;year&quot;]*10000 + $dateParts[&quot;month&quot;]*100 + $dateParts[&quot;day&quot;];<br> }<br>
}</pre>
<p><strong>对于 Oracle 数据库:</strong></p>
<pre class="examplecode">function format_date_for_sql($date){
if ($date == "")
return null;
else {
$dateParts = date_parse($date);
return $dateParts['year']*10000 + '-' + $dateParts['month']*100 + '-' + $dateParts['day'];
}
}</pre>
<p>如果输入字符串为空,则代码返回空值。否则,将 <tt>$date</tt> 作为输入参数以调用内部 <tt>date_parse</tt> 函数。<tt>date_parse</tt> 函数返回一个数组,其中包含三个名为 <tt>$dateParts["year"]</tt><tt>$dateParts["month"]</tt><tt>$dateParts["day"]</tt> 的元素。最终输出字符串由 <tt>$dateParts</tt> 数组元素组成。 </p>
<p class="alert"><strong>重要说明:</strong><tt>date_parse</tt> 函数仅识别英语日期。例如,它解析 "February 2, 2016" 而不解析 "2 Unora, 2016"。</p>
<!--Note to translators: Please replace "2 Unora, 2016" with a date in your own language. -->
<p class="notes"><strong>Oracle 数据库用户注意事项:</strong>唯一的格式要求是,<tt>return $dateParts...</tt> 语句中的日期格式应与 <tt>insert_wish</tt> 查询的 <tt>to_date</tt> SQL 函数中的日期格式相匹配。</p>
<h3 id="validateAndEnterWishToDatabase">在数据库中输入新的愿望记录 </h3>
<p>现在,您已开发了辅助函数,接下来便可添加代码以验证新的愿望数据;如果数据有效,则将数据输入到数据库中。如果数据无效,代码必须重新加载 "Add Wish" 窗体。如果数据无效的原因是没有输入说明,但输入了截止日期,则会保存截止日期并在重新加载窗体时重新显示该日期,您<a href="#inputFormAfterunsuccessfulSave">以前开发的</a>代码实现了该功能。</p>
在顶部 &lt;? php?&gt; 块(属于 <tt>editWish.php</tt>)中会话处理代码下面输入以下代码:
<pre class="examplecode">require_once("Includes/db.php");
$wisherID = WishDB::getInstance()->get_wisher_id_by_name($_SESSION['user']);
$wishDescriptionIsEmpty = false;
if ($_SERVER['REQUEST_METHOD'] == "POST"){
if (array_key_exists("back", $_POST)) {
header('Location: editWishList.php' );
exit;
} else
if ($_POST['wish'] == &quot;&quot;) {
$wishDescriptionIsEmpty = true;
}
else {
WishDB::getInstance()->insert_wish($wisherID, $_POST['wish'], $_POST['dueDate']);
header('Location: editWishList.php' );
exit;
}
}
</pre>
<p>该代码执行以下功能:</p>
<ul>
<li>允许使用 <tt>db.php</tt> 文件</li>
<li>获取或创建 <tt>WishDB</tt> 类实例 </li>
<li>通过调用 <tt>get_wisher_id_by_name</tt> 函数,检索尝试添加愿望的许愿者的 ID</li>
<li>初始化 <tt>$wishDescriptionIsEmpty</tt> 标志,该标志将在以后显示错误消息时使用。</li>
<li>检查请求方法是否为 POST,这意味着数据是从窗体中提交的,目的是在 <tt>editWish.php</tt> 页中输入愿望数据。 </li>
<li>检查 <tt>$_POST</tt> 数组是否包含具有 "back" 键的元素</li>
</ul>
<p>如果 <tt>$_POST</tt> 数组包含具有 "back" 键的元素,则在提交窗体之前按了 "Back to the List" 按钮。在这种情况下,代码会将用户重定向到 <tt>editWishList.php</tt>,而不保存在字段中输入的任何数据并停止 PHP 处理。 </p>
<p>如果 $_POST 数组<em></em>包含具有 "back" 键的元素,则数据是通过按 "Save Changes" 按钮提交的。在这种情况下,代码验证是否填写了愿望说明。该代码的工作方式是,检查 $_POST 数组中具有 "wish" 键的元素是否为空;如果该键为空,则将 $wishDescriptionIsEmpty 标志更改为 true。请注意,不会在 PHP 块中执行其他代码,将重新加载 "Add Wish" 窗体。</p>
<p>如果未按 "Back to the List" 按钮但填写了愿望说明,代码将调用 <tt>insert_wish</tt> 函数并将许愿者 ID、说明和愿望截止日期作为输入参数。然后,代码将用户重定向到 <tt>editWishList.php</tt> 页并停止 PHP 处理。</p>
<h3><a name="displayingErrorMessages"></a>显示错误消息</h3>
如果用户尝试保存愿望,但没有输入愿望说明,则一定会显示错误消息。<br> 在 HTML 输入窗体的 "Describe your wish" 输入字段下输入以下 &lt;? php?&gt; 块:
<pre class="examplecode">&lt;?php<br> if ($wishDescriptionIsEmpty) echo &quot;Please enter description&lt;br/&gt;&quot;;<br>?&gt;</pre>
如果 <tt>$wishDescriptionIsEmpty</tt> 标志为 true,则会显示错误消息。该标志是在输入窗体验证期间处理的。
<h2><a id="backToIndex" name="backToIndex"></a>返回到主页 index.php </h2>
用户应该能够随时按某个按钮返回到应用程序主页。<br> 要实现该功能,请在 <tt>editWishList.php</tt> 文件中的结束 &lt;/body&gt; 标记前面输入以下 HTML 输入窗体:
<pre class="examplecode">&lt;form name=&quot;backToMainPage&quot; action=&quot;index.php&quot;&gt;<br>&lt;input type=&quot;submit&quot; value=&quot;Back To Main Page&quot;/&gt;<br>&lt;/form&gt;</pre>
一按下 "Back to Main Page" 按钮,该窗体便将用户重定向到主页 index.php。
<h2><a name="testingAddWishFunctionality"></a>测试添加愿望功能 </h2>
<ol>
<li>运行应用程序。在 <tt>index.php</tt> 页上,填写以下字段:在 "Username" 字段中输入 "Tom",在 "Password" 字段中输入 "tomcat"。<br> <img alt="用户登录以编辑愿望列表" class="margin-around" src="../../../images_www/articles/72/php/wish-list-lesson6/user-logon-to-edit-wish-list.png"> </li>
<li>按 "Edit My Wish List" 按钮。<tt>editWishList.php</tt> 页打开。 <br> <img alt="添加了 &quot;Add&quot;(添加)按钮的 &quot;Edit Wish List&quot;(编辑愿望列表)" class="margin-around" src="../../../images_www/articles/72/php/wish-list-lesson6/edit-wish-list-add-wish.png"></li>
<li>按 "Back to Main Page" 按钮。<tt>index.php</tt> 页打开。 </li>
<li>以 Tom 身份登录,然后再次按 "Edit My Wish List" 按钮。<tt>editWishList.php</tt> 页打开。 </li>
<li>按 "Add Wish" 按钮。<tt>editWish.php</tt> 页打开。填写窗体。<br> <img alt="新愿望的窗体已填写" class="margin-around" src="../../../images_www/articles/72/php/wish-list-lesson6/new-wish.png"> <br> 按 "Back to the List" 按钮。<tt>editWishList.php</tt> 页打开,但未添加输入的愿望。</li>
<li>再次按 "Add Wish" 按钮。<tt>editWish.php</tt> 页打开。填写截止日期并将说明保留空白。按 "Save Changes" 按钮。<tt>editWish.php</tt> 页将显示输入窗体,其中包含一条错误消息和填写的截止日期。 </li>
<li>再次按 "Add Wish" 按钮。<tt>editWish.php</tt> 页打开。填写窗体,然后按 "Save Changes" 按钮。<tt>editWishList.php</tt> 页将显示更新的愿望列表。 <br> <img alt="新愿望已添加到愿望列表中" class="margin-around" src="../../../images_www/articles/72/php/wish-list-lesson6/edit-wish-list-updated.png"></li>
</ol>
<h2><a name="lessonResultSourceCode"></a>完成当前课程后的应用程序源代码 </h2>
<p>MySQL 用户:单击<a href="https://netbeans.org/files/documents/4/1932/lesson6.zip" target="_blank">此处</a>以下载源代码,该代码反映了在完成课程后的项目状态。</p>
<p>Oracle 数据库用户:单击<a href="https://netbeans.org/projects/www/downloads/download/php%252Foracle-lesson6.zip" target="_blank">此处</a>以下载源代码,该代码反映了在完成课程后的项目状态。</p>
<h2><a name="nextSteps"></a>后续步骤</h2>
<p><a href="wish-list-lesson5.html">&lt;&lt; 上一课</a><br> <br> <a href="wish-list-lesson7.html">下一课 &gt;&gt;</a><br> <br> <a href="wish-list-tutorial-main-page.html">返回到教程主页</a></p>
<br>
<div class="feedback-box" ><a href="/about/contact_form.html?to=3&amp;subject=Feedback:%20PHP%20Wish%20List%20CRUD%206:%20Writing%20New%20DB%20Entry">发送有关此教程的反馈意见</a></div>
<br style="clear:both;" >
<p>要发送意见和建议、获得支持以及随时了解 NetBeans IDE PHP 开发功能的最新开发情况,请<a href="../../../community/lists/top.html">加入 users@php.netbeans.org 邮件列表</a>
<p><a href="wish-list-tutorial-main-page.html"></a>
<p><a href="../../trails/php.html">返回至 PHP 学习资源</a></p>
</body>
</html>