创建一个表,在单击时更新文本文件

时间:2015-12-27 07:24:58

标签: javascript php html mysql database

我正在尝试创建一个网站,允许人们查看当前的"积分"他们有,以及他们收到这些积分的原因。 E.G:拿垃圾拿5分,洗碗拿10分。

虽然我已经设置了实际的积分系统和用户名和密码的数据库,但我不知道如何为管理员添加积分和提供原因的方法制定出前端方式。登录后在用户页面上。 任何帮助,将不胜感激 谢谢

1 个答案:

答案 0 :(得分:1)

您需要一个包含用户下拉列表的表单(在html中),原因的文本框和带有提交按钮的点的文本框。单击时,它返回到服务器上的php文件,该文件使用$_GET$_POST获取信息,然后使用mysqli_ php函数更新mysql数据库。然后,php文件可以显示成功/失败以及您可能希望显示给管理员的任何其他信息。

希望通过这些步骤,您可以使用谷歌并找到实际实现的相应代码,如果您遇到困难,请回到此处,显示您目前所拥有的内容,人们将很乐意为您提供帮助。

编辑:圣诞节我想写一些代码,所以这对你来说是一个艰难的开始。有一点你可以改进。您还需要创建一个报告,以便能够展示它的全部内容,但这可能有点太多了。祝你好运,享受。

<?php
/*
This script expects the following to be in place:
1. A mySQL server on the same server as the web server running this script
2. A database called 'tasks' with a username called 'tasks' and a password of 'YourPassWordHere'
3. The following table within the database (SQL included here so can copy/paste into phpMyAdmin to create it:
USE `tasks`;
DROP TABLE IF EXISTS `transaction`;
CREATE TABLE IF NOT EXISTS `transaction` (
  `id_transaction` int(11) NOT NULL AUTO_INCREMENT,
  `id_child` int(11) NOT NULL,
  `id_task` int(11) NOT NULL,
  `id_points` int(11) NOT NULL,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_transaction`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
*/

function echoHTMLHead($title) {
    echo '
<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="viewport" content="width=device-width, initial-scale=1.25">
        <style type="text/css">
            #main {
                width: 230px;
            }
            #myForm1 {
                font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
                font-size: medium;
                background-color: #FFFFCC;
                border: medium inset #808000;
                border-radius: 8px;
                width: 95%;
            }
            #myForm2 {
                font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
                font-size: medium;
                background-color: #FFFFBB;
                border: medium inset #808000;
                border-radius: 8px;
                width: 95%;
            }
            #myForm3 {
                font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
                font-size: medium;
                background-color: #FFFFAA;
                border: medium inset #808000;
                border-radius: 8px;
                width: 95%;
            }
            #error {
                font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
                font-size: medium;
                color:red;  
                text-align:center;
                font-style:normal;
                font-weight:bold;
            }
            #success {
                font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
                font-size: medium;
                color:green;    
                text-align:center;
                font-style:normal;
                font-weight:bold;
            }
        </style>
        <title>'.$title.'</title>
    </head>
        ';
}

/* Main part starts here */
// first we check to see if we have data or is this the first time the page is called
if ((isset($_POST['id_child'])) && (isset($_POST['id_task'])) && (isset($_POST['txtPoints'])) && (isset($_POST['txtAdminID']))) {
  // We have data come from the form so we process it.
  // Get the data recieved and put it into variables
  $id_child = trim($_POST['id_child']);
  $id_task = trim($_POST['id_task']);
  $txtPoints = trim($_POST['txtPoints']);
  $txtAdminID = trim($_POST['txtAdminID']);
  /* Now check if admin ID is what it should be and just in case someone  see's this code, we're going to encrypt the AdminID and check it against a pre- encrypted code
     To create a hash create a seperate php file with
  <?php
     $txtAdminID = "12345";
     echo hash('sha256', $txtAdminID);
  ?> */
  $txtKnownAdminHash = "5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5";  // which for the example code is the hash of 12345   
  $txtHashedAdminID = hash('sha256', $txtAdminID);
  if ($txtKnownAdminHash != $txtHashedAdminID) {
    // didn't match so we display an error but display the form again to allow admin to try again

  echoHTMLHead("Tasks and Rewards - Data entry error");
?>
    <body>
        <div id="main"> 
            <p id="error">You are not authorised!!</p>
            <form id="myForm1" accept-charset="utf8" target="_self" enctype="multipart/form-data" autocomplete="on" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myForm1"> 
                <label id="lblPerson" for="id_child" form="myForm1">Person </label>
                <select required id="id_child" name="id_child">
                    <option value="">Select from the list...</option>
                    <option <?php if ($id_child == "1") echo 'selected '; ?>value="1">Trevor</option>
                    <option <?php if ($id_child == "2") echo 'selected '; ?>value="2">Patty</option>
                    <option <?php if ($id_child == "3") echo 'selected '; ?>value="3">Aaron</option>
                    <option <?php if ($id_child == "4") echo 'selected '; ?>value="4">Elian</option>
                </select><br />
                <label id="lblTask" for="id_task" form="myForm1">Task </label>
                <select required id="id_task" name="id_task">
                    <option value="">Select from the list...</option>
                    <option <?php if ($id_task == "1") echo 'selected '; ?>value="1">Wash dishes</option>
                    <option <?php if ($id_task == "2") echo 'selected '; ?>value="2">Clean room</option>
                    <option <?php if ($id_task == "3") echo 'selected '; ?>value="3">Put out trash</option>
                    <option <?php if ($id_task == "4") echo 'selected '; ?>value="4">Mow lawn</option>
                    <option <?php if ($id_task == "5") echo 'selected '; ?>value="5">Water garden</option>
                    <option <?php if ($id_task == "6") echo 'selected '; ?>value="6">Special task</option>
                </select><br />
                <label id="lblPoints" for="txtPoints" form="myForm1">Points </label><input id="txtPoints" value="<?php echo $txtPoints; ?>" autocomplete="on" maxlength="3" size="3" required="required" form="myForm1" name="txtPoints" type="text"><br>
                <label id="lblAdminID" for="txtAdminID" form="myForm1">Admin ID </label><input id="txtAdminID" value="" autocomplete="off" maxlength="10" size="10" form="myForm1" name="txtAdminID" type="password"><br>
                <input form="myForm1" value="submit" name="Submit" type="submit"><br>
            </form>
        </div>
    </body>
</html>

<?php    
  }
  else {
    // We are good to add the info to the database.
    // first set some variables for the database connection. You can change these for your system
    $servername = "localhost";
    $username = "tasks";
    $password = "YourPassWordHere";
    $dbname = "tasks";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    // Create the SQL query string to add the data
    $sql = "INSERT INTO transaction (id_child, id_task, points)
    VALUES ('".$id_child."', '".$id_task."', '".(int)$txtPoints."' )";
    // Run the SQL query and check result
    if ($conn->query($sql) === TRUE) {
        $msg = "Details added";
    } else {
        $msg = "Error: " . $sql . "<br>" . $conn->error;
    }
    // Close the mySQL connection
    $conn->close();  
    // Get some human readable versions of child and task to display later
    switch ($id_child) {
        case 1 : $child = "Trevor";
        break;
        case 2 : $child = "Patty";
        break;
        case 3 : $child = "Aaron";
        break;
        case 4 : $child = "Elian";
        break;
    }  
    switch ($id_task) {
        case 1 : $task = "Wash dishes";
        break;
        case 2 : $task = "Clean room";
        break;
        case 3 : $task = "Put out trash";
        break;
        case 4 : $task = "Mow lawn";
        break;
        case 5 : $task = "Water garden";
        break;
        case 6 : $task = "Special task";
        break;  
    }
    // we now display the results with a fresh form for next transaction
    echoHTMLHead("Tasks and Rewards - Success");
?>
    <body>
        <div id="main">
            <p id="success"><?php echo $msg; ?></p>
            <p>
                Person: <?php echo $child; ?><br />
                Task: <?php echo $task; ?><br />
                Points awarded: <?php echo $txtPoints; ?><br />
            </p>
            <form id="myForm2" accept-charset="utf8" target="_self" enctype="multipart/form-data" autocomplete="on" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myForm2"> 
                <label id="lblPerson" for="id_child" form="myForm2">Person </label> 
                <select id="id_child" form="myForm2" name="id_child">
                    <option selected="" value="0">Select from the list...</option>
                    <option value="1">Trevor</option>
                    <option value="2">Patty</option>
                    <option value="3">Aaron</option>
                    <option value="4">Elian</option>
                </select><br />
                <label id="lblTask" for="id_task" form="myForm2">Task </label>
                <select id="id_task" form="myForm2" name="id_task">
                    <option selected="" value="0">Select from the list...</option>
                    <option value="1">Wash dishes</option>
                    <option value="2">Clean room</option>
                    <option value="3">Put out trash</option>
                    <option value="4">Mow lawn</option>
                    <option value="5">Water garden</option>
                    <option value="6">Special task</option>
                </select><br />
                <label id="lblPoints" for="txtPoints" form="myForm2">Points </label>
                <input id="txtPoints" autocomplete="on" maxlength="3" size="3" required="required" form="myForm2" name="txtPoints" type="text"><br>
                <label id="lblAdminID" for="txtAdminID" form="myForm2">Admin ID </label>
                <input id="txtAdminID" autocomplete="off" maxlength="10" size="10" form="myForm2" name="txtAdminID" type="password"><br>
                <input form="myForm2" value="submit" name="Submit" type="submit"><br>
            </form>
        </div>
    </body>
</html>

<?php    
  }
}  // if ((isset($_POST['id_task'])) && (isset($_POST['txtPoints'])) && (isset($_POST['txtAdminID'])))
else {
  /* This is the first run of this file so display the initial form for user to complete */
  echoHTMLHead("Tasks and Rewards - Data entry");
?>
    <body>
        <div id="main">
            <form id="myForm3" accept-charset="utf8" target="_self" enctype="multipart/form-data" autocomplete="on" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="myForm3"> 
                <label id="lblPerson" for="id_child" form="myForm3">Person 
                <select required id="id_child" form="myForm3" name="id_child">
                    <option value="">Select from the list...</option>
                    <option value="1">Trevor</option>
                    <option value="2">Patty</option>
                    <option value="3">Aaron</option>
                    <option value="4">Elian</option>
                </select></label><br />
                <label id="lblTask" for="id_task" form="myForm3">Task </label>
                <select required id="id_task" form="myForm3"  name="id_task">
                    <option value="">Select from the list...</option>
                    <option value="1">Wash dishes</option>
                    <option value="2">Clean room</option>
                    <option value="3">Put out trash</option>
                    <option value="4">Mow lawn</option>
                    <option value="5">Water garden</option>
                    <option value="6">Special task</option>
                </select><br />
                <label id="lblPoints" for="txtPoints" form="myForm3">Points </label><input id="txtPoints" autocomplete="on" maxlength="3" size="3" required="required" form="myForm3" name="txtPoints" type="text"><br>
                <label id="lblAdminID" for="txtAdminID" form="myForm3">Admin ID </label><input id="txtAdminID" autocomplete="off" maxlength="10" size="10" required="required" form="myForm3" name="txtAdminID" type="password"><br>
                <input form="myForm3" value="submit" name="Submit" type="submit"><br>
            </form>
        </div>
    </body>
</html>
<?php
}
?>