dso0139 2015-04-10 01:40
浏览 19

使用PHP MySQL的文本版本控制系统

I am trying to create a system where a user can enter some text and another user can edit that input and another can edit the input that the second user has entered. This is the code that I have so far; it only works as a reply system to a post at the moment:

<?php
    include 'includes/connection.php';

    $query = "SELECT * FROM branches";  
    $result1 = mysql_query($query) or die(mysql_error());

    while($person = mysql_fetch_array($result1)) {  //As long as there is data, output the data
        $id = $person['ID'];
        $query2 = "SELECT * FROM branchesedit WHERE (parent_id = '$id' )";
        $result2 =  mysql_query($query2) or die(mysql_error());

        echo "<h3>" . $person['Names'] . "</h3>";
        echo "<p>" . $person['Lyrics'] . "</p>";
        echo "<a href=\"modify.php?id=" . $person['ID'] . "\">Modify Song</a>";
        echo "<span> </span>";
        echo "<a href=\"delete.php?id=" . $person['ID'] . "\">Delete Song</a>"; 

        while($row2 = mysql_fetch_array($result2)){
            echo "<h3>" . $row2['Name'] . "</h3>";
            echo "<p>" . $row2['LyricUpdate'] . "</p>";
        }
    }
?>

modify.php

<?php
    if(isset($_POST['submit'])) {
        $query = "SELECT ID FROM branches WHERE ID = $_GET[id]";

        mysql_query("INSERT into branchesedit(`IDs`, `Name`, `LyricUpdate`, `parent_id`)
            VALUES ('','$_POST[inputName]', '$_POST[ta]', '$_POST[id]')") or die(mysql_error());

        echo "Song has been modified";
        header("Location: index.php");  
    }
?>
  • 写回答

1条回答 默认 最新

  • doubaomao9304 2015-04-10 04:05
    关注

    Note:

    • You are using an isset() function on your modify.php where in your first given code (guessing your index.php) does not have a submit button. Only has a link that will redirect users to modify.php.
    • Better include a connection in your modify.php to establish connection so you can run your query.
    • You should consider using mysqli_* prepared statement rather than the deprecated mysql_* functions to prevent SQL injections.

    Your modify.php in prepared statement:

    <?php
        /* INCLUDE HERE YOUR CONNECTION */
        if(!empty($_GET['id'])) {
    
          if($stmt = $con->prepare("SELECT IDs, Name, LyricUpdate FROM branchesedit WHERE parent_id = ? ORDER BY IDs DESC")){
            $stmt->bind_param("i",$_GET["id"]);
            $stmt->execute();
            $stmt->store_result();
            $stmt->bind_result($id,$name,$lyricupdate);
            $stmt->fetch();
            ?>
              <h1>Modified by: <?php echo $name; ?></h1>
              <form action="modify.php" method="POST">
                <input type="hidden" name="id" value="<?php echo $_GET["id"]; ?>">
                <input type="text" name="inputName" value="<?php echo $name; ?>"><br>
                <textarea name="ta"><?php echo $lyricupdate; ?>"></textarea><br>
                <input type="submit" name="submit">
              </form>
            <?php
            $stmt->close();
          } /* END OF PREPARED STATEMENT */
        } /* END OF NOT EMPTY ID */
    
        if(isset($_POST["submit"])){
    
          if($stmt = $con->prepare("INSERT into branchesedit (`Name`, `LyricUpdate`, `parent_id`)
            VALUES (?,?,?)")){
            $stmt->bind_param("ssi",$_POST["inputName"],$_POST["ta"],$_POST["id"]);
            $stmt->execute();
            $stmt->close();
          } /* END OF INSERT PREPARED STATEMENT */
    
          echo "Song has been modified";
          header("LOCATION: index.php");  
        } /* END OF ISSET SUBMIT */
    ?>
    

    Summary:

    • When a user clicks on Modify Song link, user will be redirected to modify.php and then runs a query that will select the latest edit from your table branchesedit based from the ID being passed from the link.
    • User will see a form that is already filled up based from the last edit.
    • When submitted, it will still be in the modify.php and then runs an insert query.
    • After the insert query, it will redirect back to index.php
    • Replace the necessary connection variable I used in the prepared statement:

    Example of your connection to be included in your queries (connection.php):

    $con = new mysqli("Yourhost", "Yourusername", "Yourpassword", "Yourdatabase");
    
    /* CHECK CONNECTION */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s
    ", mysqli_connect_error());
        exit();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)