Home > Backend Development > PHP Tutorial > 如何实现“当名字在数据库中不存在时,转到错误页面。”?

如何实现“当名字在数据库中不存在时,转到错误页面。”?

WBOY
Release: 2016-06-23 14:04:24
Original
1202 people have browsed it

各位朋友:这段代码是“查询密码第一步:输入会员名”,想实现的效果:当名字在数据库中不存在时,转到错误页面。
我这个代码老是出现:Warning: Cannot modify header information - headers already sent by (output started at F:\PHPSites\member\getpwd1-1.php:23) in F:\PHPSites\member\getpwd1-1.php on line 36
应该怎么改呢?非常感谢了!

<?php$totalRows_rsMember = 0;?><?php require_once('Connections/member.php'); ?><?php$colname_rsMember = "-1";if (isset($_POST['txtusername'])) {  $colname_rsMember = (get_magic_quotes_gpc()) ? $_POST['txtusername'] : addslashes($_POST['txtusername']);}mysql_select_db($database_member, $member);$query_rsMember = sprintf("SELECT username FROM members WHERE username = '%s'", $colname_rsMember);$rsMember = mysql_query($query_rsMember, $member) or die(mysql_error());$row_rsMember = mysql_fetch_assoc($rsMember);$totalRows_rsMember = mysql_num_rows($rsMember);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><link href="style/style.css" rel="stylesheet" type="text/css" /><script language="javascript">function checkFormData () {  if ( form1.txtUsername.value == "" ) {    alert ( "用户名不能为空,请重新输入!" );	form1.txtUsername.focus();	return false;  }}</script></head><body><?php include('menu.php'); ?><?phpif ($totalRows_rsMember == 0) {  header ( "Location: message-1.php?error=3" );  exit;}?><form action="getpwd2-1.php" method="post" name="form1" id="form1" onsubmit="return checkFormData()">  <table width="256" border="1" align="center" rules="none">    <tr>      <th colspan="2" bgcolor="#0066CC" class="title">第一步:输入会员名</th>    </tr>    <tr>      <td width="72" height="32" align="right">用户名:</td>      <td width="168" height="32"><input name="txtUsername" type="text" id="txtUsername" /></td>    </tr>    <tr>      <td height="32" colspan="2" align="center"><input name="btnNext" type="submit" id="btnNext" value="下一步" /></td>    </tr>  </table></form></body></html><?phpmysql_free_result($rsMember);?>
Copy after login


回复讨论(解决方案)

在 getpwd1-1.php 的 23 行处有输出
导致 header ( "Location: message-1.php?error=3" ); 出错

先检查那个输出是否确实必要?是否能调整到别处
实在不行的话,可在 require_once('Connections/member.php'); 前加上
ob_start();
来回避

在 getpwd1-1.php 的 23 行处有输出
导致 header ( "Location: message-1.php?error=3" ); 出错

先检查那个输出是否确实必要?是否能调整到别处
实在不行的话,可在 require_once('Connections/member.php'); 前加上
ob_start();
来回避
……

版主大人:我贴上来的就是getpwd1-1.php中的所有代码,具体要怎么调整?
如果在 require_once('Connections/member.php'); 前加上ob_start();的话,就直接转到错误页面了,而不是“输入不存在的用户名再转到错误页面”了。请指点,非常感谢!

粗心了没核实文件名
把 16 到 33 行(含33行)的内容移到 39 行的后面

另外 10 到 14 行的内容应当在 7 行的条件分支里

粗心了没核实文件名
把 16 到 33 行(含33行)的内容移到 39 行的后面

另外 10 到 14 行的内容应当在 7 行的条件分支里

版主大人:我照着您指点的改了,如下,但还是直接就转到错误页面啊!

<?php$totalRows_rsMember = 0;?><?php require_once('Connections/member.php'); ?><?php$colname_rsMember = "-1";if (isset($_POST['txtusername'])) {  $colname_rsMember = (get_magic_quotes_gpc()) ? $_POST['txtusername'] : addslashes($_POST['txtusername']);  mysql_select_db($database_member, $member);$query_rsMember = sprintf("SELECT username FROM members WHERE username = '%s'", $colname_rsMember);$rsMember = mysql_query($query_rsMember, $member) or die(mysql_error());$row_rsMember = mysql_fetch_assoc($rsMember);$totalRows_rsMember = mysql_num_rows($rsMember);}?><?phpif ($totalRows_rsMember == 0) {  header ( "Location: message-1.php?error=3" );  exit;}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><link href="style/style.css" rel="stylesheet" type="text/css" /><script language="javascript">function checkFormData () {  if ( form1.txtUsername.value == "" ) {    alert ( "用户名不能为空,请重新输入!" );	form1.txtUsername.focus();	return false;  }}</script></head><body><?php include('menu.php'); ?><form action="getpwd2-1.php" method="post" name="form1" id="form1" onsubmit="return checkFormData()">  <table width="256" border="1" align="center" rules="none">    <tr>      <th colspan="2" bgcolor="#0066CC" class="title">第一步:输入会员名</th>    </tr>    <tr>      <td width="72" height="32" align="right">用户名:</td>      <td width="168" height="32"><input name="txtUsername" type="text" id="txtUsername" /></td>    </tr>    <tr>      <td height="32" colspan="2" align="center"><input name="btnNext" type="submit" id="btnNext" value="下一步" /></td>    </tr>  </table></form></body></html><?phpmysql_free_result($rsMember);?>
Copy after login

5 到 21 行改成

<?php$colname_rsMember = "-1";if (isset($_POST['txtusername'])) {  $colname_rsMember = (get_magic_quotes_gpc()) ? $_POST['txtusername'] : addslashes($_POST['txtusername']);  mysql_select_db($database_member, $member);  $query_rsMember = sprintf("SELECT username FROM members WHERE username = '%s'", $colname_rsMember);  $rsMember = mysql_query($query_rsMember, $member) or die(mysql_error());  $row_rsMember = mysql_fetch_assoc($rsMember);  $totalRows_rsMember = mysql_num_rows($rsMember);  if ($totalRows_rsMember == 0) {    header ( "Location: message-1.php?error=3" );    exit;  }}?>
Copy after login
自己想一下,问什么要这样写?

5 到 21 行改成
PHP code?123456789101112131415
原来可以这样,终于把这个逻辑理清楚了,多谢版主大人!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template