PHP mysql_connect()连接-已淘汰

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。PHP mysql_connect()连接-已淘汰,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1、首先在mysql命令控制台新建数据库

mysql> create database test;
Query OK, 1 row affected (0.04 sec)

mysql> use test;
Database changed

mysql> create table test_table(
    -> id int(5) auto_increment primary key,
    -> user char(10),
    -> pass char(10),
    -> text char(10),
    -> sex char(1),
    -> connect char(10));
Query OK, 0 rows affected (0.21 sec)

mysql>

2、登录注册代码

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
    <div>
        <b>登录</b>
        <form action="login.php" method="post">
            <p><span>用户名:</span><input type="text" name="user" required="" /></p>
            <p><span>密码:</span><input type="password" name="pass"/ required=""></p>
            <p><input type="reset" name=""/> <input type="submit" name=""/></p>
        </form>
        <a href="register.html">新用户注册</a>
    </div>
</body>
</html>

register.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<body>
    <div>
        <b>注册</b>
        <form action="register.php" method="post">
            <p><span>用户名:</span><input type="text" name="user" required=""/></p>
            <p><span>密码:</span><input type="password" name="pass" required=""/></p>
            <p><span>性别:</span>
            <input type="radio" name="sex" checked="checked" value="男"/>男
            <input type="radio"  name="sex" value="女"/>女
            </p>
            <p><span>联系:</span><input type="text" name="connect" value="" required=""/></p>
            <p><span>文本:</span><input type="text" name="text" required=""/></p>
            <p><input type="reset" name=""/> <input type="submit" name=""/></p>
        </form>
        <a href="index.html">已注册</a>
    </div>
</body>
</html>

login.php

<meta charset="utf-8">
<?php
    header ( "Content-type:text/html;charset=utf-8" );
    // Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
    // error_reporting(0); // 错误警告不显示

    $conn = mysql_connect('localhost','root','') or die("error connecting") ;
    mysql_query("set names 'utf8'");  
    mysql_select_db("test"); 

    $user = $_POST['user'];
    $pass = $_POST['pass'];

    $sql="SELECT * FROM test_table where user='$user' and pass='$pass'"; 
    $rs = mysql_query($sql,$conn);
    $row = mysql_fetch_array($rs); 
    // var_dump($row);

    if($row['user']==$user&& $row['pass']==$pass){
        echo $row['user']."登陆成功!";
    }
    else{
        echo"登录失败,请重新登录!";
    }
?>

register.php

<meta charset="utf-8">
<?php
    header ( "Content-type:text/html;charset=utf-8" );
    // Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
    error_reporting(0); // 错误警告不显示

    $conn = mysql_connect('localhost','root','') or die("error connecting") ;
    mysql_query("set names 'utf8'");  
    mysql_select_db("test"); 

    $user = $_POST['user'];
    $pass = $_POST['pass'];
    $sex = $_POST['sex'];
    $connect = $_POST['connect'];
    $text = $_POST['text'];

    $sql = "INSERT INTO test_table VALUES ('' ,'{$user}' ,'{$pass}','{$text}','{$sex}' ,'{$connect}' )";
    $result = mysql_query($sql,$conn);

    echo("注册成功!!!<br/><a href='index.html'>点击登录</a>");

?>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/151248.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!