PHP basic seven

<!-- Chapter 13 PHP Operation Database-->
<?php
//     $sql = "insert into money(username,age) values('bao3', 25)";
//     $con = mysqli_connect("localhost","root","root");
//     if (!$con) {
//         die('Could not connect:'.mysql_error());
//     }
// echo 'Connection succeeded! ';
    
    
//     mysqli_select_db($con, 'mytest');
//     $result = mysqli_query($con, $sql);
//     var_dump($result);
//     mysqli_close($con)
?>

<form action="" method="post">
    用户名:<input type="text" name="username" /><br />
    密码:<input type="password" name="password" /><br />
  重复密码:<input type="password" name="repassword" /><br />
  <input type="submit" value="提交" />
</form>

<?php
    if (trim(@$_POST['password']) != trim(@$_POST['repassword'])) {
        exit('The two passwords are inconsistent, please return to the previous page');
    }
    
    @$username = trim($_POST['username']);
    @$password = md5(trim($_POST['password']));
    @$time = date('ymd',time());
    $ip = $_SERVER['REMOTE_ADDR'];
    $conn = mysqli_connect('localhost', 'root', 'root');
    if (mysqli_error($conn)) {
        echo mysqli_error($conn);
        exit;
    }
    
    mysqli_select_db($conn, 'book');
    mysqli_set_charset($conn, 'utf-8');
    mysqli_set_charset($conn, 'gb2312');
    $sql = "insert into user(username,password,createtime,createip)
         values('".$username."','".$password."','".$time."','".$ip."')";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        echo 'success';
    }else {
        echo 'failed';
    }
    
    echo 'The ID inserted by the current user is'.mysqli_insert_id($conn);
    mysqli_close($conn);
?>

<!-- Do a list display -->
<?php
    $conn = mysqli_connect('localhost', 'root', 'root', 'book');
    if (mysqli_errno($conn)) {
        mysqli_error($conn);
        exit;
    }
    mysqli_set_charset($conn, 'utf8');
    mysqli_set_charset($conn, 'gb2312');
    $sql = "select id,username,createtime,createip from user order by id desc";
    $result = mysqli_query($conn, $sql);
    if ($result && mysqli_num_rows($result)) {
        echo '<table width="800" border="1">';
        while ($row = mysqli_fetch_assoc($result)){
            echo '<tr>';
            echo '<td>'.$row['username'].'</td>';
            echo '<td>'.@date('Y-m-d H:i:s', $row['createtime']).'</td>';
            echo '<td>'.long2ip($row['createip']).'</td>';
            echo '<td><a href="edit.php?id='.$row['id'].'">编辑用户</a></td>';
            echo '<td><a href="delete.php?id='.$row['id'].'">删除用户</a><td/>';
            echo '</tr>';
        }
        echo '</table>';
    }else {
        echo 'no data';
    }
    mysqli_close($conn);
?>

<!-- Paginate the user -->
<?php
// Make a configuration file config.php
//database server
define('DB_HOST', 'localhost');
//database username
define('DB_USER', 'root');
//database password
define('DB_PWD', 'secret');
// library name
define('DB_NAME', 'book');
//character set
define('DB_CHARSET', 'utf8');
?>
<?php
// connection.php
include 'config.php';
$conn = mysqli_connect('DB_HOST', 'DB_USER', 'DB_PWD', 'DB_NAME');
if (mysqli_error($conn)) {
    mysqli_error($conn);
    exit;
}
mysqli_set_charset($conn, DB_CHARSET);
?>
<?php
    include 'connection.php';
    $count_sql = 'select count(id) as c from user';
    $result = mysqli_query($conn, $count_sql);
    $data = mysqli_fetch_assoc($result);
    $count = $data['c'];
    
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    
//     if (isset($_GET['page'])) {
//         $page = (int) $_GET['page'];
//     }else {
//         $page = 1;
//     }
    
    $num = 5;
    $total = ceil($count / $num);
    if ($page <= 1) {
        $page = 1;
    }
    if ($page >= $total) {
        $page = $total;
    }
    $offset = ($page - 1) * $num;
    
    $sql = "select id,username,createtime,createip from user order by id desc
       id desc limit $offset,$num";
    $result = mysqli_query($conn, $sql);
    if ($result && mysqli_num_fields($result)) {
       echo '<table width="800" border="1">' ;
       while ($row = mysqli_fetch_assoc($result)){
           echo '<tr>';
           echo '<td>'.$row['username'].'</td>';
           echo '<td>'.date('Y-m-d H:i:s',$row['createtime']).'</td>';
           echo '<td>'.long2ip($row['createip']).'</td>';
           echo '<td><a herf="edit.php?id='.$row['id'].'">编辑用户</a></td>';
           echo '<td><a herf="delete.php?id='.$row['id'].'"></a></td>';
           echo '</tr>';
       }
       echo '<tr>
             <td colspan="5">
             <a herf="page.php?page=1">首页</a>
             <a href="page.php?page='.($page - 1).'">上一页</a>
             <a herf="page.php?page='.($page + 1).'">下一页</a>
             <a herf="page.php?page='.$total.'">页尾</a>
                        The current page is '.$page.' of '.$total.'
             </td>
             </tr>';
       echo '</table>';
    }else {
        echo 'no data';
    }
    mysqli_close($conn);
?>
<!-- Batch and specific deletion of users -->
<?php
    include 'connection.php';
    if (is_array($_POST['id'])) {
        $id = join(',', $_POST['id']);
    }elseif (is_numeric($_GET['id'])){
        $id = (int) $_GET['id'];
    }else {
        echo 'Invalid data';
        exit;
    }
    $sql = "delete from user where id in($id)";
    $result = mysqli_query($conn, $sql);
    if ($result) {
        echo 'Delete successful';
    }else {
        echo 'deletion failed';
    }
?>
<!-- Modify user information-->
<?php
//edit.php
    include 'connection.php';
    if (is_numeric($_GET['id'])) {
        $id = (int) $_GET['id'];
    }
    $sql = "select id,username from user where id=".$id;
    $result = mysqli_query($conn, $sql);
    $data = mysqli_fetch_assoc($result);
?>
<?php
    //update.php
    include 'connection.php';
    $id = (int) $_POST['id'];
    if (trim($_POST['password'])) {
        $password = md5(trim($_POST['password']));
        $sql = "update user set password='".$password."' where id = $id";
    }else {
        echo 'Modified successfully';
    }
    $result = mysqli_query($conn, $sql);
    if ($result) {
        echo 'Modified successfully';
    }
?>
<form action="update.php" method="post">
    用户名:<input type="text" name="username" value="<?php echo $data['username'];?>" readonly/><br />
    密码:<input type="password" name="password" /><br />
  <input type="hidden" value="<?php echo $data['id'];?>" name="id" />
  <input type="submit" value="提交" />    
</form>


<!-- Data display garbled ultimate solution-->
<?php
// The core idea of ​​solving the problem of garbled characters is: there must be unified coding in multiple different file systems.
// The 9 points are:
// 1.html encoding is consistent with MySQL encoding
// 2. The PHP encoding is the same as the MySQL encoding
// 3. If there is a header to send the character set, please be the same as the database
//     4.<meta http-equiv="Content-Type"content="text/html;charset=utf-8">
// Must be consistent with the page text encoding
// 5. The character set of the database database should be unified
// 6. The character set of the table should be unified
// 7. The character set of the column should be unified (the table will be written by default if the column is set)
// 8. Connect, check the character set to be unified
// 9. The character set of the result set should be unified



?>



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649125&siteId=291194637