如何使用INNER JOIN显示不同用户编辑的数据

时间:2018-08-26 18:03:32

标签: php mysql

我需要在数据库中创建一条记录,当用户编辑描述字段时,它将保存到数据库中(当前有效)。但是,当用户想要将另一个便笺添加到之前添加的同一个便笺中时,它只会保存旧的便笺。 (我为此使用UPDATE)。

所以我想知道如何实现这一目标?在下面的代码中,您将看到我有两个文本区域,一个用于显示数据库中的记录,另一个用于向数据库添加新注释。理想情况下,我想在新的不可编辑文本区域中显示每个新注释(类似于显示添加到原始注释中的所有注释的历史记录。

<?php
require_once '../config.php';

$description_err = "";

if(isset($_POST["id"]) && !empty($_POST["id"])){
$id = $_POST["id"];

$input_description = trim($_POST["descriptionfield"]);
if(empty($input_description)){
    $description_err = "Please enter a description."; 
} else{
    $description= $input_description;
}

if(empty($description_err)){
    $sql = "UPDATE newtask SET new_description=? WHERE id=?";

    if($stmt = mysqli_prepare($link, $sql)){
        mysqli_stmt_bind_param($stmt, "si", $param_description, $param_id);

        $param_description = $description;
        $param_id = $id;

        if(mysqli_stmt_execute($stmt)){
            header("location: user-dashboard.php");
            exit();
        } else{
            echo "Something went wrong. Please try again later.";
        }
    }

    mysqli_stmt_close($stmt);
}

mysqli_close($link);
} else{
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
    $id =  trim($_GET["id"]);

    $sql = "SELECT * FROM newtask WHERE id = ?";
    if($stmt = mysqli_prepare($link, $sql)){
        mysqli_stmt_bind_param($stmt, "i", $param_id);

        $param_id = $id;

        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);

            if(mysqli_num_rows($result) == 1){
                $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

                $description = $row["new_description"];
            } else{
                header("location: ../error.php");
                exit();
            }

        } else{
            echo "Oops! Something went wrong. Please try again later.";
        }
    }

    mysqli_stmt_close($stmt);

    mysqli_close($link);
}  else{
    header("location: ../error.php");
    exit();
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum- 
scale=1" />
<meta name="description" content="" />
<meta name="author" content="" />
<title>Support Admin</title>
<link href="../assets/css/bootstrap.css" rel="stylesheet" />
<link href="../assets/css/font-awesome.min.css" rel="stylesheet" />
<link href="../assets/css/style.css" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?family=Nova+Flat" 
rel="stylesheet" type="text/css" />
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,700,300" 
rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="head">
    <div class="container">
        <div class="row">

            <div class="col-lg-4 col-md-4 col-sm-4">
                <a href="../index.php">
                <img src="../assets/img/logo1.png"  />
                    </a>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4 text-center" >
                 <img src="../assets/img/top-mouse.png "  class="header-mid" 
/>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4">
                <h4><span>Call:</span> 082 </h4>
                <h4><span>E-mail:</span> sales</h4>
            </div>

        </div>
    </div>
</div>

<section>
    <div class="container">
        <div class="row noti-div">

            <div class="col-lg-3 col-md-3 col-sm-3  text-center">

            </div>

        </div>


    </div>

</section>
<section id="main">
  <div class="container">
   <div class="row">
     <div class="col-lg-9 col-md-9 col-sm-9 alert alert-info">
       <h3 class=" text-center">Update Task</h3>
        <div class="hr-div"> <hr />
     </div>

<form action="<?php echo 
 htmlspecialchars(basename($_SERVER['REQUEST_URI'])); 
?>" method="post">

<div class="form-group col-lg-12 col-md-12 col-sm-12 <? 
php echo (!empty($description_err)) ? 'has-error' : ''; ?>">
 <label>New Note<textarea name="descriptionfield" 
    class="formcontrol</textarea>
   </div>

<div class="form-group col-lg-12 col-md-12 col-sm-12">
 <label>Note Added By </label>
 <textarea class="form-control"><?php echo $description; ?></textarea>
 </div>
    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
       <div class="form-group col-lg-12 col-md-12 col-sm-12">

  <button type="submit" class="btn btn-primary">Submit</button>
        </div>
           </form>
       </div>
            <div class="col-lg-3 col-md-3 col-sm-3">
                <a href="index.php" class=" label label-danger"> 
<strong>LOGOUT</strong> </a>
                <div class="list-group">
                    <a href="#" class="list-group-item active">Quick Links
                    </a>

  <a href="user-dashboard.php" class="list-group-item">My  Dashboard</a>
  <a href="open-tickets.php" class="list-group-item">Open Tasks</a>
  <a href="notifications.php" class="list-group-item">Pending Tasks</a>
  <a href="ticket-history.php" class="list-group-item">Completed Tasks</a>
  <a href="change-password.html" class="list-group-item">Change Password</a>

</div>
   <div class="alert alert-success text-center">
    <h3>The Notice Board</h3>
        No Notice Found ! 
   </div>
     <div class="list-group">
       <a href="#" class="list-group-item active">Support Categories</a>
       <a href="#" class="list-group-item">Notes</a>
       <a href="#" class="list-group-item">Manuals</a>
       <a href="#" class="list-group-item">General Information</a>
   </div>
  </div>
 </div>
    </div>
 </section>
 <div id="footer">
    <div class="container">
        <div class="row">
          <div class="col-lg-4 col-md-4 col-sm-4">
            <h3>This</h3>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4">
                <h3>Contact Details</h3>
            </div>
        </div>
    </div>

</div>
<script src="assets/js/jquery-1.10.2.js"></script>
<script src="assets/js/bootstrap.js"></script>

</body>
</html>

因此,如您在上面的代码中看到的那样,它更新了我的“ newtask”表,我认为它需要更新名为“ updatedata”的新表(这不在上面的代码中,因为我不确定如何加上两个表的ID)

然后显示“ newtask”表(原始表)中的数据。

按照上面的问题,我将如何实现这一目标?

谢谢

编辑

因此,从下图开始,我想将“ updatedata”表中的文本链接到“ newtask”表中的id 1。然后在页面的不同div中显示该文本。

我想将所有新文本从更新数据链接到newtask,而不会覆盖newtask中已经存在的数据。

enter image description here

所以我将显示如下所示

enter image description here

0 个答案:

没有答案