代码之家  ›  专栏  ›  技术社区  ›  Werokk

使用mysql php的Todolist

  •  -2
  • Werokk  · 技术社区  · 8 年前

    $username = $_POST ['username'];
    $password = $_POST ['password'];
    
    
    
    $s = " select * from usertable1 where username = '$username' ";
    $result = mysqli_query($con, $s);
    $num = mysqli_num_rows($result);
    $error = "username/password incorrect";
    
    if($num == 1 ){
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        if(password_verify($password, $row['password'])){
            $_SESSION['username']= $username;
            header('location:home.php');
        }else{
    
            header("location: index.php");
        }
    
    }else{
        header("location: index.php");
    
    }
    
    
    
     ?>
    

    <?php
    require_once 'init.php';
    
    $itemsQuery = $db ->prepare("
    SELECT id, name, done
    FROM items
    WHERE username = :username
    ");
    $itemsQuery->execute([
      'username'=>$_SESSION ['id']
    ]);
    $items = $itemsQuery->rowCount()? $itemsQuery :[];
    foreach ($items as $items ) {
      echo $item['name'], '<br>';
    }
     ?>
    
     <html>
     <head>
       <meeta charset = "utf-8">
       <title> To do</title>
       <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
       <link href="https://fonts.googleapis.com/css?family=Shadows+Into+Light+Two" rel="stylesheet">
       <link rel ="stylesheet" href="cool.css">
       <meta name="viewport" content="width=device-width", initial-scale=1.0:>
    
     </head>
    <body>
      <div class ="list">
        <h1 class ="header"> To do.</h1>
    
        <?php if(!empty($items)):
          ?>
        <ul class="items
        <?php foreach ($items as $item): ?>
            <li>
              <span class="item<?php echo $item['done'] ? ' done' : ''?>"><?php echo $item['name'];?></span>
              <?php if(!$item['done']): ?>
              <a href="mark.php?as=done&item=<?php echo $item['id'];?>" class="done-button"> Mark as done</a>
              <?php endif; ?>
    </li>
    <?php endforeach; ?>
        </ul>
        </<?php else: ?>
          <p> You haven't added any items yet. </php>
            </<?php endif; ?>
    
        <form class="item-add" action="add.php" method="post">
          <input type="text" name="name" placeholder="Type a new item here." class="input" autocomplete="off" required>
          <input type="submit" value="Add" class="submit">
    
        </form>
      </div>
    
    </body>
    </html>
    

    初始化.php

    $username = $_POST ['username'];
    $password = $_POST ['password'];
    $s = " select * from usertable where username = '$username' ";
    $result = mysqli_query($db, $s);
    
    
    if($num == 1){
      $_SESSION['username']= $username;
    }else{
      die('You are not signed in.');
    }
    
     ?>
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Er. Amit Joshi    8 年前

    我脑子里有一些额外的想法。

    项目表:


    用户表:

    第一种方法:

    当用户创建/添加新项目时 id user 表也被存储到 item 桌子的 用户

    因此,您可以保留记录的用户详细信息,如 身份证件 session


    第二种方法: 项目 桌子

    项到用户表: id、item\ id(item表中新添加的项的id)、user\ id(用户窗体会话的id)或 用户 表)

    所以当用户创建/添加新项目时,接下来的过程就开始了

    1. 将新项目插入 项目 桌子
    2. 就在插入新项目后使用 mysqli_insert_id inserted id 项目的

    3. inserted_id user_id 进入第三张桌子 item_to_user_table

    第二种方法的优点: 项目 item table 不影响 user table

    欺骗:

    推荐文章