想要写个简单的注册页面,想把注册的用户数据写到数据库中

//注册页面

<!DOCTYPE html>
<html>
<head>
 <title>注册页</title>
 <meta charset="utf-8">
 <style type="text/css">
  *{
   margin:0px;padding:0px;
  }
  .gray{
   width:333px;
   height:444px;
   background-color:lightgray;
   margin:88px auto;
   border-radius:5px;
  }
  table
  {
   margin:0px auto;
  }
  table caption
  {
   margin-top:40px;
  }
 </style>
 <script type="text/javascript">
  function checkname(oInput)
  {
   var xmlhttp;
   var span=document.getElementById('s');
   var name=document.getElementById('name');
   if(oInput.value=="")
   {
    span.innerHTML="请输入账号";
    span.style.color='red';
    name.focus();
    return;
   }
   if(window.ActiveXObject)
   {
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
   }
   else
   {
    xmlhttp=new XMLHttpRequest();
   }
   xmlhttp.open('post','checkname.php',true);
   xmlhttp.onreadystatechange=function()
   {
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
     var result=xmlhttp.responseText;
     if(result=='yes')
     {
      span.innerHTML='';
     }
     else
     {
      span.innerHTML='账号已被注册';
      span.style.color='red';
     }
    }
   }
   xmlhttp.setRequestHeader('content-type','application/x-www-form-urlencoded');
   xmlhttp.send('name='+oInput.value);
  }
 </script>
</head>
<body>
 <div class="gray">
  <form action="check.php" method="post">
   <table>
    <caption align="center">新用户注册</caption>
    <tr>
     <td>用户名</td>
    </tr>
    <tr>
     <td><input type="text" name="name" onblur="checkname(this)" id="name"><span id="s"></span></td>
    </tr>
    <tr>
     <td>密码</td>
    </tr>
    <tr>
     <td><input type="password" name="pwd"></td>
    </tr>
    <tr>
     <td>确认密码</td>
    </tr>
    <tr>
     <td><input type="password" name="repwd"></td>
    </tr>
    <tr>
     <td>电子邮件</td>
    </tr>
    <tr>
     <td><input type="text" name="email"></td>
    </tr>
    <tr>
     <td>个人主页</td>
    </tr>
    <tr>
     <td><input type="text" name="homepage"></td>
    </tr>
    <tr>
     <td colspan="2" align="center"><input type="submit" value="注册" name="btnadd"></td>
    </tr>
   </table>
  </form>
 </div>
</body>
</html>


//想用php把注册的信息添加到数据库中

<?php
 header('content-type:text/html;charset=utf-8;');
 $name=$_POST['name'];
 $pwd=md5($_POST['pwd']);
 $email=$_POST['email'];
 $homepage=$_POST['homepage'];
 $pdo=new PDO('mysql:host=localhost;dbname=reg','root','',array(PDO::MYSQL_ATTR_INIT_COMMAND=>"set names 'utf8'"));
 $sql="select count(userid) from users where name=$name";
 $sql2="insert into users values($name,$pwd,$email,$homepage)";
 $recordset=$pdo->query($sql);
 $recordset->setFetchMode(PDO::FETCH_NUM);
 $result=$recordset->fetch();
 print_r($result);
 if($result[0]>0)
 {
  echo 'no';
 }
 else
 {
  echo 'yes';
 }
 if($pdo->exec($sql2))
 {
  echo '注册成功';
 }
 else
 {
  echo '失败';
 }
 $pdo=null;

//总是说我姿势不对,疯了要,觉得程序员每天不是在写代码,是在找错误排查,啊啊啊啊啊w(゚Д゚)w

//写了20分钟,哈哈,搞定,运行,错误,然后我就找了一天的错误,到底是哪里错了呢?


寂静的蝴蝶
浏览 1781回答 1
1回答

帅得一比

把错误提示写上来会更有用
打开App,查看更多内容
随时随地看视频慕课网APP