如何更新数据库中的值?

时间:2014-03-28 01:11:37

标签: php html mysql

这里我有一些存储在表中的值:

| points_id || user_id(fk) || point_hist |
|___________||_____________||____________|
|      1    ||     10      ||    100     |  
|___________||_____________||____________|
|      2    ||     11      ||     30     |
|___________||_____________||____________|
|      3    ||     12      ||     70     |
|___________||_____________||____________|
|      4    ||     13      ||    200     |

如果用户获得了100 points,并且他们已经拥有100 points,例如points_id 1。

我需要一个查询,将他们的奖励100添加到他们当前的100分= 200分。

这是我的PHP:

<?php
    // see if the form has been completed
    include_once("php_includes/check_login_status.php");
    $username = "";
    $weight = "";
    $height = "";
    $weighthist = "";
    $id = "";

if(isset($_GET["u"])){
    $username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
} 
    //$user_query=mysqli_query($db_conx,"SELECT * FROM users,points_history"); /* WHAT'S THE PURPOSE OF VARIABLE $username? */
    $user_query=mysqli_query($db_conx,"SELECT * FROM users WHERE username='$username' AND activated='1' LIMIT 1"); 

/* check if the user exists in the database */
while ($row = mysqli_fetch_array($user_query)) {
    $id = $row ["id"];
    $username = $row ["username"];
    $weight = $row["weight"];
    $points = $row['point_hist'];

    } /* END OF WHILE LOOP $user_query */

    $q12 = mysqli_query($db_conx,"SELECT * FROM points_history WHERE id='$id'"); 
     /* check if the user exists in the database */
    while ($row = mysqli_fetch_array($q12)) {
        $id = $row ["id"];
        $points = $row['point_hist'];
        } /* END OF WHILE LOOP $user_query */

if (isset($_POST['submit'])){

    $id = $_POST["id"];
    $username = $_POST['username'];
    $weight = $_POST['weight'];
    $weighthist = $_POST['weighthist'];
    $point_hist = $_POST['point_hist'];

    $exists=mysqli_query($db_conx,"SELECT * FROM users WHERE username='$username'");

    if (mysqli_num_rows($exists)!=0) {
    /* update the description in the database */

    /* calculate score */
        $calweight = $weight - $weighthist; 
        $point_hist = $calweight * 10 + $points;

    mysqli_query($db_conx,"INSERT INTO weighthistory (id, weighthist, date) VALUES ('$id','$weighthist',now())");

   // mysqli_query ($db_conx,"INSERT INTO  points_history (id, point_hist, date) VALUES ('$id','$point_hist',now())");


   /* UPDATE QUERY */

    // Update weight in the users table.
    mysqli_query($db_conx,"UPDATE users SET weight='$weighthist' WHERE id='$id'");

    mysqli_query($db_conx,"UPDATE points_history SET point_hist='$point_hist' WHERE id='$id'");    

   /* END OF UPDATE QUERY */

    } /* END OF ELSE IF $exists NOT 0 */

    else { echo "the name does not exist";  }

} /* END OF ISSET SUBMIT */

?>  

1 个答案:

答案 0 :(得分:0)

您已将 MySQL MySQLi 混合在一起。让我们将它们转换为 MySQLi 。让我尝试清理代码以便更好地理解。

让我们假设您的 check_login_status.php 如下所示:

<?php

$db_conx=mysqli_connect("studentnet","k1003140","iphone09","db_k1003140"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

?>

您的主要代码:

<?php

    include("php_includes/check_login_status.php");

    if(isset($_GET["u"])){
        $username = preg_replace('#[^a-z0-9]#i', '', $_GET['u']);
    } 

    $username = "";

    $user_query=mysqli_query($db_conx,"SELECT * FROM users WHERE username='$username' AND activated='1' LIMIT 1"); /* WHAT'S THE PURPOSE OF VARIABLE $username? */

    /* check if the user exists in the database */
    while ($row = mysqli_fetch_array($user_query)) {
        $id = $row ["id"];
        $username = $row ["username"];
        $weight = $row["weight"];
        $email = $row["email"];
        } /* END OF WHILE LOOP $user_query */

    if (isset($_POST['submit'])){
        $id = $_POST["id"];
        $username = $_POST['username'];
        $weight = $_POST['weight'];
        $weighthist = $_POST['weighthist'];
        $point_hist = $_POST['point_hist'];

        $exists=mysqli_query($db_conx,"SELECT * FROM users WHERE username='$username'");

        if (mysqli_num_rows($exists)!=0) {
        /* update the description in the database */

        /* calculate score */
            $calweight = $weight - $weighthist;     
            $point_hist = $calweight * 10;

        mysqli_query($db_conx,"INSERT INTO weighthistory (id, weighthist, date) VALUES ('$id','$weighthist',now())");

        mysqli_query ($db_conx,"INSERT INTO  points_history (id, point_hist, date) VALUES ('$id','$point_hist',now())");


       /* UPDATE QUERY */

       mysqli_query($db_conx,"UPDATE points_history SET point_hist='$TheUpdatedValue' WHERE id='$id'");

       /* END OF UPDATE QUERY */


        } /* END OF ELSE IF $exists NOT 0 */

        else { echo "the name does not exist";  }

} /* END OF ISSET SUBMIT */

?>  

我认为你没有给出完整的代码。我只是将您的代码从混合 MySQL MySQLi 转换为 MySQLi