zoukankan      html  css  js  c++  java
  • php与数据库的连接用法 (签到一)

    注册页面

    //插入js验证

    <script type="text/javascript">
        window.onload = function(){
            var hid = document.getElementById("id");
            if(hid.value !=""){                                             //当用户名已存在数据库时,提示用户已注册
                alert("用户名已注册");
            }
        }
    </script>

    //html表单

      //隐藏域用来获取yanzheng.php传过来的id值,若获取到则说明该用户已注册

     <input type="hidden" name="uid" value="<?php echo $_GET["id"] ? $_GET["id"] : ""; ?>" id="id">     

      //创建表单用来提交用户输入的数据
        <form action="yanzheng.php" method="post">

      //用来存放用户输入的信息
            用户名:<input type="text" name="id"><br>    
            密码: <input type="password" name="screct"><br>
            确认密码:<input type="password" name="screct1"><br>

      //用来提交表单
            <input type="submit" value="注册" >
        </form>

    //php表单与数据信息验证

      <?php
    //设置php编码
      header("Content-type:text/html;charset=utf-8");
    //定义变量
      $id=$_POST["id"];
      $screct=$_POST["screct"];
    //连接数据库
      $db = new MySQLi("localhost","root","","z_text");
    //验证数据库是否连接成功
      !mysqli_connect_error() or die("连接失败");
    //设置编码
      $db -> query("set names utf8");
    //向数据库中添加值
      $sql="select * from    user where name='$id'";
    //执行qul代码
      $ret=$db -> query($sql);
    //将结果转为数组
      $att=$ret -> fetch_all();
    //判断注册信息是否存在
      if($att==false){
        //向数据库中添加注册信息
          $sql="insert into user(name,password) values('$id','$screct')";
        //执行qul代码
          $ret =$db-> query($sql);

    //判断是否验证成功
          if($ret){

    //验证成功则跳转值签到首页
              header("location:shouye.php");
          }else{

    //如果不成功则返回注册页面
              header("location:zuce.php");
          }
      }else{

    //如果注册信息已存在则,带id值返回zece.php
          header("location:zuce.php?id=1");
      }
      ?>

     遇到的问题及其分析

    1.php网页编码问题:

    header("Content-type:text/html;charset=utf-8");

    2.数据传输问题:

    header("location:路径?键=值");(以get方式传值);

    3.常见报错

    Notice: Undefined index:没有找到变量,或者变量是空值

    解决思路:根据报错提示找到位置,然后输出报错行逐步排查

    4.利用php操作数据时,除了查询结果其他的结果都是布尔值

    5.php传值接收问题

    php穿过来的值可以建立隐藏域用来接受

    <input type="hidden" name="uid" value="<?php echo $_GET["id"] ? $_GET["id"] : ""; ?>">

    php传过来的值可以直接用于输出接收

    欢迎你:<?php echo $uid ?>

  • 相关阅读:
    20Spring_JdbcTemplatem模板工具类
    19Spring_AOP编程(AspectJ)_使用@Pointcut注解来定义切点
    18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结
    17Spring_AOP编程(AspectJ)_AspectJ的注解编程
    14Spring_AOP编程(AspectJ)_环绕通知
    android ViewPager滑动事件讲解
    为listview的item中的元素设置onclick事件
    Android EditText光标颜色 与inputType
    【Android】Android内存溢出问题---用自行开辟的空间进行对内存管理
    【Android】eclipse打不开的解决办法和“Jar mismatch! Fix your dependencies”的解决
  • 原文地址:https://www.cnblogs.com/diverman/p/8412538.html
Copyright © 2011-2022 走看看