新人求助 关于登录注册写数据库的问题,代码如下。

这是register页面
<!--注册部分-->
<div class="register_body">
    <div class="col-xs-12 register_title">注册</div>
    <form action="reg.php" class="register" method="post"> 
        <div class="nav">
            <div class="nav register_nav">
                <div class="col-xs-4">
                    用户名:
                </div>
                <div class="col-xs-6">
                    <input type="text" name="" id="name_r" value="" placeholder="&nbsp;&nbsp;用户名/手机号" onBlur="javascript:ok_or_errorByRegister(this)" />
                </div>
                <div class="col-xs-1 ok_gou">
                    √
                </div>
                <div class="col-xs-1 error_cuo">
                    ×
                </div>
            </div>
            <div class="nav register_psdnav">
                <div class="col-xs-4">
                    密&nbsp;&nbsp;&nbsp;码:
                </div>
                <div class="col-xs-6">
                    <input type="password" name="" id="psd_r" value="" placeholder="&nbsp;&nbsp;密码" onBlur="javascript:ok_or_errorByRegister(this)" />
                </div>
                <div class="col-xs-1 ok_gou">
                    √
                </div>
                <div class="col-xs-1 error_cuo">
                    ×
                </div>
            </div>
            <div class="nav register_affirm">
                <div class="col-xs-4">
                    确认密码:
                </div>
                <div class="col-xs-6">
                    <input type="password" name="" id="affirm_psd" value="" placeholder="&nbsp;&nbsp;确认密码" onBlur="javascript:ok_or_errorByRegister(this)" />
                </div>
                <div class="col-xs-1 ok_gou">
                    √
                </div>
                <div class="col-xs-1 error_cuo">
                    ×
                </div>
            </div>
            <div class="nav register_psdnav">
                <div class="col-xs-4">
                    邮&nbsp;&nbsp;&nbsp;箱:
                </div>
                <div class="col-xs-6">
                    <input type="email" name="" id="mail" value="" placeholder="&nbsp;&nbsp;邮箱" onBlur="javascript:ok_or_errorByRegister(this)" />
                </div>
                <div class="col-xs-1 ok_gou">
                    √
                </div>
                <div class="col-xs-1 error_cuo">
                    ×
                </div>
            </div>
            <div class="col-xs-12 register_btn_div">
                <input type="submit" class="sub_btn" value="注册" id="register" />
            </div>
        </div>
    </form>
这是reg.php

<?php
if(!isset($_POST['submit'])){
    exit('非法访问!');
}
$username = $_POST['name_r'];
$password = $_POST['psd_r'];
$email = $_POST['mail'];

//包含数据库连接文件
include('../conn.php');

//检测用户名是否已经存在
$check_query = mysql_query("select id from time_user where username='$username' limit 1");
if(mysql_fetch_array($check_query)){
    echo "错误:用户名 ',$username,' 已存在。<a href='javascript:history.back(-1);'>返回</a>";
    exit;
}
//写入数据
$password = MD5($password);
$sql = "INSERT INTO time_user(username,password,email)VALUES('$username','$password','$email')";
if(mysql_query($sql,$conn)){
    exit("用户注册成功!点击此处 <a href='Register.html'>登录</a>");
} else {
    echo '抱歉!添加数据失败:',mysql_error(),'<br />';
    echo "点击此处 <a href='javascript:history.back(-1);'>返回</a> 重试";
}
?>
这是conn.php

<?php
/*****************************
*数据库连接
*****************************/
$conn = @mysql_connect("localhost","root","111");
if (!$conn){
    die("连接数据库失败:" . mysql_error());
}
mysql_select_db("timecapsule", $conn);
//字符转换,读库
mysql_query("set character set 'gbk'");
//写库
mysql_query("set names 'gbk'");
?>

然后打开网页,输入信息后点击注册,跳转到reg.php 然后显示非法访问,求解。
环境用的 Appserv。

http://img.mukewang.com/57b032a30001042e11850235.jpg

还发现了问题 如果去掉$_POST['submit']那一段,倒是能插入数据库字段 但是插入的好像获取不到表单的值,显示的是这样子的【前两个是测试的时候加的 第三个是表单插入的】

http://img.mukewang.com/57b037bf0001b17306190116.jpg
求助求助


徐蕴琪
浏览 1954回答 1
1回答

柠檬酸钠

给你的提交按钮加一个name="submit"
打开App,查看更多内容
随时随地看视频慕课网APP