• PHP连接数据库


    <form action="php_mysql_add.php" method="post">
        用户名: <input type="text" name="user"/> <br/>
        标题: <input type="text" name="title"/> <br/>
        内容: <textarea name="content" id="" cols="30" rows="10"></textarea> <br/>
        <input type="submit" name="submit" value="发布留言"/> <br/>
    </form>

    连接到数据库的代码:

    1
    if(@$_POST['submit']){<br><span style="line-height: 1.5;"><span style="color: #999999;">       //设置表单变量</span><br></span>    $user = $_POST['user'];  <br>    $title = $_POST['title'];<br>    $content = $_POST['content'];<br><span style="color: #999999;">       //连接到数据库</span><br><em style="line-height: 1.5;"><em><em>   $conn = @mysql_connect("localhost","root","root") or die("数据库连接失败,请检查你的网络,稍后再试试");<br></em></em></em>

           //选定数据库

          mysql_select_db("test");

           //执行SQL语句(查询)

           mysql_query("set names 'utf8'");

    1
    2
    3
    4
    5
    6
    <em id="__mceDel"><em id="__mceDel"><em id="__mceDel"><em id="__mceDel">    $sql = "INSERT INTO `test`.`message` (`id`,`user`, `title`, `content`, `lastdate`) VALUES (NULL, '$user', '$title', '$content', now())";
        mysql_query($sql);<br><span style="color: #999999;">    //关闭数库</span>
        mysql_close($conn);
    }
    ?>
  • 相关阅读:
    [LeetCode] 617. Merge Two Binary Trees
    [LeetCode] 738. Monotone Increasing Digits
    289. Game of Life
    305. Number of Islands II
    288. Unique Word Abbreviation
    271. Encode and Decode Strings
    393. UTF-8 Validation
    317. Shortest Distance from All Buildings
    286. Walls and Gates
    296. Best Meeting Point
  • 原文地址:https://www.cnblogs.com/wanger1994/p/3705365.html
Copyright © 2020-2023  润新知