将输入字段中的值添加到数据库到现有数字时,添加的数字是

时间:2018-04-18 14:44:08

标签: php html mysql sql

当在输入的字段中输入$ points的输入数字时,它将数字添加到数据库中已经存在的总数中,但由于某种原因,添加的数字是双倍。例如,如果输入3,6将添加到总计中。任何人都可以帮忙解决这个问题吗?

这个想法是有人应该能够在总数中添加点数,然后在一个单独的页面上能够在进度条中查看它(这是正常工作的),但总计不会加起来。 我是php新手,所以提前抱歉代码中的任何错误。

谢谢

<?php 
session_start();
if(!isset($_SESSION["sess_user"])){
    header("location:login.php");
} else {
echo "Userid: ".$_SESSION["sess_id"];
?>
<!doctype html>
<html>
<head>
<h2><a id="button" href = "index.php">Main Menu</a></h2>
<h2><a id="button" href = "selftrack.php">Track your updated progress!</a></h2>
</head>
<body>

<?php
            // Connect to the database
            $username = ""; 
            $password = "";
            $host = "";
            $db = $username;
            $points = $_POST['self_p'];
            // Connect to the MySQL server and select the required database
            $connection = mysqli_connect($host, $username, $password, $db);

            if (mysqli_connect_error()) {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
            } 
            else { // Database connected correctly            

                echo "<h1>Add  daily points</h1>";
                if (isset($_POST["addSubmit"])) { 
                    if ((!empty($_POST["self_p"]))) {// Check all parts of the form have a value


                        $query="UPDATE targets 
                        SET self_points = self_points + ".$points."
                        WHERE user_id='".$_SESSION['sess_id']."'";


                        $result = mysqli_query($connection, $query);

                        if ($result == false) { 
                            // Show error message
                            echo "<p>The target points for  " . $_POST["self_p"] . " was not added.</p>";                        
                        } 
                        else { 
                        echo "<p>The target points for \"" . $_POST["self_p"] . "\" has been added.</p>";
                        }
                    } 
                    else { 
                    echo "<p>Please fill out all the details</p>";
                    }
                }
            }

            ?>

            <form  role="form" id="addForm" name="addForm" action="?" method="post">            

                <div class="form-group">
                <div class="col-xs-7">
                    <label for="addFormLast_Name">Please enter your daily points, up to 5:</label>
                    <input class="form-control" id="addFormLast_Name" name="self_p" type="text">
                </div>
                <div class="form-group">
                <div class="col-xs-7">
                    <input class="form-control" id="addSubmit" name="addSubmit" value="Add Target" type="submit">

                </div>
                </div>
                 <?php
             mysqli_close($connection);
        }
        ?>
                </body>
                <?php

?>

                </html>

0 个答案:

没有答案