变量未定义和一个警告,找了很久也找不出来!求大神帮忙,多谢

WBOY
Release: 2016-06-13 11:50:38
Original
1176 people have browsed it

变量未定义和一个警告,找了很久也找不出来!求大神帮忙,谢谢!

本帖最后由 xuzuning 于 2014-03-06 10:16:20 编辑 /******* 第20行的while循环里面的变量$arr说未定义,不要降低警告,那是自欺欺人,我要的是怎样程序中改进*******/
<?php<br />session_start();<br />echo $_SESSION['username'];<br /><br />if($_POST && $_POST['submit']){<br />$username =$_POST["username"];<br />$email = $_POST["email"];<br />$title = $_POST["title"];<br />$content = $_POST["content"];<br />$createtime = date("Y-m-d H:i:s");<br />$link = mysql_connect("127.0.1.1","root","")or die("不能连接服务器!可能是数据库服务器没有开启,或用户名密码错误!".mysql_error());<br />mysql_query("use db_leave");<br />mysql_select_db("db_leave",$link);<br />mysql_query("set names'utf8'");<br />$sql = "insert into l_text(id,username,email,title,content,createtime) values('','$username','$email','$title','$content','$createtime')";<br />$result = mysql_query($sql);<br /><br /><br />$r = mysql_query("select * from l_text");<br />}<br />?><br /><html><br /><body><br /><div><br /><ul><br /><?php<br /><br />while($arr = mysql_fetch_array($r)){<br />?><br /><li><br /><table><br /><tr><br />	<td>姓名:</td><br />	<td><?php echo $arr["username"];?></td><br />	<td>邮箱:</td><br />	<td><?php echo $arr["email"];?></td><br />	<td>标题:</td><br />	<td><?php echo $arr["title"];?></td><br />	<td>留言时间:</td><br />	<td><?php echo $arr["createtime"];?></td><br /></tr><br /><tr><br /><td>留言内容:</td><br /><td></hr><?php echo $arr["content"];?></td><br /><br /></tr><br /></table><br /></li><br /><?php<br />}<br />?><br /></ul><br /></div><br /><form action="" method="post"><br />					<tr><br />						<td>姓名:</td><br />						<td><input type="text" name="username" /></td><br />						<td>邮箱:</td><br />						<td><input type="text" name="email" /></td><br />					</tr><br />					<br><br />					<tr><br />						<td>标题:</td><br />						<td colspan="3"><input type="text" name="title" size="58" /></td><br />					</tr><br />					<br><br />					<tr><br />						<td>留言内容:</td><br />						<td colspan="3"><textarea name="content" rows="10" cols="44"></textarea></td><br />					</tr><br />					<tr><br />						<td colspan="4"><input type="submit" name="submit" value="提交留言" /></td><br />					</tr><br />				</form><br /></body><br /></html>
Copy after login

------解决方案--------------------
C 语言中,变量必须先声明再使用(因为编译器需要分配内存)
php 的变量不直接存储于内存地址,而存放于变量表中。所以可以使用未经声明的变量
Notice 警告只是表示可能会出问题,但并不一定出问题

数据库查询放在分支外面是为啥?
因为你在条件分支内外都使用了数据库操作
如果只放在条件分支内,就可能出问题
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!