为什么这段代码会出现问题啊?
时间:2011-11-01
来源:互联网
<?php
$username=$_POST["username"];
$pwd=$_POST['pwd'];
$repeat_pwd=$_POST['repeat_pwd'];
$name=$_POST['name'];
$email=$_POST['email'];
if(!empty($username)){
$db=@new mysqli("127.0.0.1","root","","test")or die("cuowu");
if(mysqli_connect_errno()){
echo "数据库连接失败!<br>\n";
echo mysqli_connect_error();
exit;
}
$sql="INSERT INTO t_user(f_username,f_password,f_name,f_email) VALUES";
$sql .="('$username','$pwd','$name','$email')";
$rs=$db->query($sql);
if(!$rs){
$db->close();
echo '数据录入失败';
exit;
}
echo "<font color='red' size='5'>gongxi</font><br/>\n";
//
$db->close();
}
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=utf8">
<title>zhuce</title>
</head>
<body>
<?php
if(!empty($username)){
echo "信息";
echo $username;
echo $pwd;
echo $name;
echo $repeat_pwd;
echo $name;
echo $email;
}
?>
<form name="frm" method="post" action="zhuce.php">
<p>
用户名:<input name="username" type="text" id="username"></input>
</p>
<p>密码:<input name="pwd" type="password" id="pwd"></input></p>
<p>重复密码:<input name="repeat_pwd" type="password" id="repeat_pwd"></p>
<p>姓名:<input name="name" type="text" id="name"></input></p>
<p>email;<input name="email" type="text" id="email"></input></p>
<p><input type="submit" name="submit" value="提交"></input>
<input type="reset" name="reset" value="重置"></input></p>
</form>
</body>
</html>
这段代码在执行之后会出现这些提示
Notice: Undefined index: username in D:\wamp\www\zhuce\zhuce.php on line 2
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: pwd in D:\wamp\www\zhuce\zhuce.php on line 3
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: repeat_pwd in D:\wamp\www\zhuce\zhuce.php on line 4
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: name in D:\wamp\www\zhuce\zhuce.php on line 5
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: email in D:\wamp\www\zhuce\zhuce.php on line 6
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
但是当把表单提交之后,这些错误提示都会消失这是为什么啊???
另外数据库显示的是乱码这是什么原因啊???
$username=$_POST["username"];
$pwd=$_POST['pwd'];
$repeat_pwd=$_POST['repeat_pwd'];
$name=$_POST['name'];
$email=$_POST['email'];
if(!empty($username)){
$db=@new mysqli("127.0.0.1","root","","test")or die("cuowu");
if(mysqli_connect_errno()){
echo "数据库连接失败!<br>\n";
echo mysqli_connect_error();
exit;
}
$sql="INSERT INTO t_user(f_username,f_password,f_name,f_email) VALUES";
$sql .="('$username','$pwd','$name','$email')";
$rs=$db->query($sql);
if(!$rs){
$db->close();
echo '数据录入失败';
exit;
}
echo "<font color='red' size='5'>gongxi</font><br/>\n";
//
$db->close();
}
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html;charset=utf8">
<title>zhuce</title>
</head>
<body>
<?php
if(!empty($username)){
echo "信息";
echo $username;
echo $pwd;
echo $name;
echo $repeat_pwd;
echo $name;
echo $email;
}
?>
<form name="frm" method="post" action="zhuce.php">
<p>
用户名:<input name="username" type="text" id="username"></input>
</p>
<p>密码:<input name="pwd" type="password" id="pwd"></input></p>
<p>重复密码:<input name="repeat_pwd" type="password" id="repeat_pwd"></p>
<p>姓名:<input name="name" type="text" id="name"></input></p>
<p>email;<input name="email" type="text" id="email"></input></p>
<p><input type="submit" name="submit" value="提交"></input>
<input type="reset" name="reset" value="重置"></input></p>
</form>
</body>
</html>
这段代码在执行之后会出现这些提示
Notice: Undefined index: username in D:\wamp\www\zhuce\zhuce.php on line 2
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: pwd in D:\wamp\www\zhuce\zhuce.php on line 3
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: repeat_pwd in D:\wamp\www\zhuce\zhuce.php on line 4
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: name in D:\wamp\www\zhuce\zhuce.php on line 5
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
( ! ) Notice: Undefined index: email in D:\wamp\www\zhuce\zhuce.php on line 6
Call Stack
# Time Memory Function Location
1 0.0481 682608 {main}( ) ..\zhuce.php:0
但是当把表单提交之后,这些错误提示都会消失这是为什么啊???
另外数据库显示的是乱码这是什么原因啊???
作者: wlzcool 发布时间: 2011-11-01
加上if(isset($_POST['submit'])){
//前面的php代码放到这里边
}
因为php是顺序执行,没有提交表单的时候没有$_POST['pwd']等变量,所以会报错。提交后有相应的变量,所以不报错了。
插入是乱码是因为编码原因。
插入前执行
mysql_query("set names 'gbk'");
或者
mysql_query("set names 'utf8'");
//前面的php代码放到这里边
}
因为php是顺序执行,没有提交表单的时候没有$_POST['pwd']等变量,所以会报错。提交后有相应的变量,所以不报错了。
插入是乱码是因为编码原因。
插入前执行
mysql_query("set names 'gbk'");
或者
mysql_query("set names 'utf8'");
作者: ohmygirl 发布时间: 2011-11-01
这些提示仅仅是些提示,可以忽略的。并不是错误,仅仅是提醒你变量没有定义。
可以根据上面的改也可以
if(isset($_POST)){
//前面的php代码放到这里边
}
乱码问题如上.
可以根据上面的改也可以
if(isset($_POST)){
//前面的php代码放到这里边
}
乱码问题如上.
作者: PhpNewnew 发布时间: 2011-11-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28