<?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
但是当把表单提交之后,这些错误提示都会消失这是为什么啊???
另外数据库显示的是乱码这是什么原因啊???
//前面的php代码放到这里边
}
因为php是顺序执行,没有提交表单的时候没有$_POST['pwd']等变量,所以会报错。提交后有相应的变量,所以不报错了。插入是乱码是因为编码原因。
插入前执行
mysql_query("set names 'gbk'");
或者
mysql_query("set names 'utf8'");
//前面的php代码放到这里边
}乱码问题如上.
同时还少个set names utf8
$link=mysql_connect("127.0.0.1","root","")or die("cuowu");
mysql_select_db("zhuce",$link);
mysql_query("set names utf-8");
if($_POST["submit"]!=""){
$name=$_POST['name'];
$pwd=$_POST['pwd'];
$bumen=$_POST['bumen'];
$sql="Insert Into t_user(f_name,f_password,f_bumen)Values('$name','$pwd','$bumen')";
$result=mysql_query($sql);
if($result==true){ echo "<script>alert('$name,$bumen');</script>";}
else{echo "注册失败";}
}
?>
我把php改成了这样,可是还是有错,同样报这样的错
Notice: Undefined index: submit in D:\wamp\www\11.php on line 8
Call Stack
# Time Memory Function Location
1 0.0009 676640 {main}( ) ..\11.php:0
这个错误,就显示在网页里啊,让人咋看啊!!!
改为
if(isset($_POST["submit"]))
$link=mysql_connect("127.0.0.1","root","")or die("cuowu");
mysql_select_db("zhuce",$link);
mysql_query("set names utf-8");
if(isset($_POST["submit"])){
$name=$_POST['name'];
$pwd=$_POST['pwd'];
$bumen=$_POST['bumen'];
$sql="Insert Into t_user(f_name,f_password,f_bumen)Values('$name','$pwd','$bumen')";
$result=mysql_query($sql);
if($result==true){ echo "<script>alert('$name,$bumen');</script>";}
else{echo "注册失败";}
}
?>
$link=mysql_connect("127.0.0.1","root","")or die("cuowu");
mysql_select_db("zhuce",$link);
mysql_query("set names 'utf8'");//就是这句话啊
if(isset($_POST["submit"])){
$name=$_POST['name'];
$pwd=$_POST['pwd'];
$bumen=$_POST['bumen'];
$sql="Insert Into t_user(f_name,f_password,f_bumen)Values('$name','$pwd','$bumen')";
$result=mysql_query($sql);
if($result==true){ echo "<script>alert('$name,$bumen');</script>";}
else{echo "注册失败";}
}
?>
就是这么改的注册时我写的是“我部门”“123” “部门”结果出现这个
??? 123 ??
$name=iconv('gbk','utf-8',$name);
$bumen=iconv('gbk','utf-8',$bumen);
echo "<script>alert('$name,$bumen');</script>";