使用 PHP 将数值添加到 MySQL 数据库的两个行

发布于 2025-01-02 06:07:50 字数 6539 浏览 0 评论 0原文

我有一个网站,登录用户可以累积积分,以后可以通过购物车购买积分。下面的页面是一个 php 管理功能,其中管理员可以向单个用户(目前一次一个用户)给予积分。

此脚本涉及三个表:

users: 包含用户详细信息

tally_point: 存储所有积分交易,包括传入和订购

reward_points: > 存储用户拥有的总点数该

脚本通过下拉菜单检索用户的详细信息并将点数添加到记分点表中,但是……

        <?php # add-points-ind.php
        // This is the main page for the site.

        // Include the configuration file for error management and such.
        require_once ('./includes/config.inc.php');

        // Set the page title and include the HTML header.
        $page_title = 'Add Points to User';
        include ('includes/header_admin_user.html');

        // If no dealer_code variable exists, redirect the user.
        if (!isset($_SESSION['admin_int_id'])) {

           // Start defining the URL.
           $url = 'http://' . $_SERVER['HTTP_HOST']
            . dirname($_SERVER['PHP_SELF']);
           // Check for a trailing slash.
           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
                $url = substr ($url, 0, -1); // Chop off the slash.
           }
           // Add the page.
           $url .= '/login.php'; 

        ob_end_clean(); // Delete the buffer.
        header("Location: $url"); 
        exit(); // Quit the script.
        }
        ?>

        <h1>Add Points to User</h1>
        <div id="maincontent_inner">
        <div id="maincontent_inner2">  

        <?php //add-points-ind.php
        // This page allows the admin to add points to an individual user

        require_once ('mydatabase.php'); // Connect to the database.

        if (isset($_POST['submitted'])) { // Check if the form has been submitted.

        // Check if points were submitted through the form.
        if (is_numeric($_POST['tally_points_in'])) {
        $p = (float) $_POST['tally_points_in'];
        } else {
        $p = FALSE;
        echo '<p><font color="red">Please enter the pointås!</font></p>';
        }

        // Validate the User has been selected
        if ($_POST['selected_user'] == 'new') {

        // If it's a new categories, add the categories to the database.
        $query = 'INSERT INTO tally_points (users_id) VALUES (';

        // Check for a last_name.
        if (!empty($_POST['users_id'])) {
        $query .= "'" . escape_data($_POST['users_id']) . "')";

        $result = mysql_query ($query); // Run the query.
        $a = mysql_insert_id(); // Get the categories ID.

        } else { // No last name value.
        $a = FALSE;
        echo '<p><font color="red">Please enter the Dealers name!</font></p>';
        }

        } elseif ( ($_POST['selected_user'] == 'existing') && ($_POST['existing'] > 0))
        { // Existing categories.
        $a = (int) $_POST['existing'];
        } else { // No categories selected.
        $a = FALSE;
        echo '<p><font color="red">Please select a registered Dealer!</font></p>';
        }

        if ($p && $a) { // If everything's OK.

        // Add the print to the database.
        $query = "INSERT INTO tally_point (users_id, tally_points_in, order_id, total, tally_points_entry_date) VALUES ('$a', '$p', '0', '0', NOW())"; 
        if ($result = mysql_query ($query)) 
        { 
        // Worked.
        echo '<p>The reward product has been added.</p><br /><a href="add-points-ind.php">Go back</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />';
        } else { 
        // If the query did not run OK.
        echo '<p><font color="red">Your submission could not be 
        processed due to a system error.</font></p>';
        }
        } else { // Failed a test.
        echo '<p><font color="red">Please click "back" and try again.</font></p>';
        }
        } else { // Display the form.

        ?>

        <form enctype="multipart/form-data" action="add-points-ind.php" method="post">

        <input type="hidden" name="MAX_FILE_SIZE" value="524288" />

        <fieldset>
        <legend>Add Points Individually:</legend>

        <p><b>Select User:</b></p>

        <p>
        <select name="existing"><option>Select One</option>
        <?php // Retrieve all the users details and add to the pull-down menu.
        $query = "SELECT users_id, users_sale_id, users_first_name, users_surname FROM users ORDER BY users_surname ASC";
        $result = @mysql_query ($query);
        while ($row = @mysql_fetch_array ($result, MYSQL_ASSOC)) {
        echo "<option value=\"{$row['users_id']}\">{$row['users_sale_id']}: {$row['users_first_name']} {$row['users_surname']} </option>\n";
        }
        @mysql_close($dbc); // Close the database connection.
        ?>

        </select></p>
        <span class="extras"><input type="radio" name="selected_user" value="existing" /> Please confirm this is the correct user</span>
        <p><b>Points:</b> <br />
        <input type="text" name="tally_points_in" size="10" maxlength="10" /></p>
        </fieldset>

        <div align="center"><input type="submit" name="submit" value="Submit" /></div>
        <input type="hidden"name="submitted" value="TRUE" />
        </form>

        <?php
        } // End of main conditional.
        ?>

        <br class="clearboth" />
        End text
        </div>

        <?php // Include the HTML footer file.
        include ('includes/footer_admin_user.html');
        ?> 

我在添加新点数时遇到了麻烦到总分字段(reward_user_points) 在 reward_points 表中,我在下面有一些代码,但我不确定应该把它放在哪里,如果有人有任何建议,请告诉我。

    <?php
    $query = "SELECT reward_user_points FROM reward_points WHERE users_id = $a";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $TotalPoints = $row['reward_user_points'];

    if (@mysql_affected_rows($dbc) == 1) { // Whohoo!

        $new_credit = $TotalPoints + $p;
        $query = "UPDATE reward_points SET reward_user_points ='$new_credit' WHERE users_id = $a";
        $result = @mysql_query($query); 
        }
    ?>

I have a site in which logged in users can accumulate points which they can later buy with via a shopping cart. The page below is an admin php feature in which an Admin can give points to an individual user (one user at a time for now).

There are three tables involved with this script:

users: contains the users details

tally_point: stores all of the points transactions, both incoming and ordering

reward_points: stores the total amount of points that the user has

The script retrieves the users’ details via a drop down menu and adds the points to the tally point table ok but....

        <?php # add-points-ind.php
        // This is the main page for the site.

        // Include the configuration file for error management and such.
        require_once ('./includes/config.inc.php');

        // Set the page title and include the HTML header.
        $page_title = 'Add Points to User';
        include ('includes/header_admin_user.html');

        // If no dealer_code variable exists, redirect the user.
        if (!isset($_SESSION['admin_int_id'])) {

           // Start defining the URL.
           $url = 'http://' . $_SERVER['HTTP_HOST']
            . dirname($_SERVER['PHP_SELF']);
           // Check for a trailing slash.
           if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
                $url = substr ($url, 0, -1); // Chop off the slash.
           }
           // Add the page.
           $url .= '/login.php'; 

        ob_end_clean(); // Delete the buffer.
        header("Location: $url"); 
        exit(); // Quit the script.
        }
        ?>

        <h1>Add Points to User</h1>
        <div id="maincontent_inner">
        <div id="maincontent_inner2">  

        <?php //add-points-ind.php
        // This page allows the admin to add points to an individual user

        require_once ('mydatabase.php'); // Connect to the database.

        if (isset($_POST['submitted'])) { // Check if the form has been submitted.

        // Check if points were submitted through the form.
        if (is_numeric($_POST['tally_points_in'])) {
        $p = (float) $_POST['tally_points_in'];
        } else {
        $p = FALSE;
        echo '<p><font color="red">Please enter the pointås!</font></p>';
        }

        // Validate the User has been selected
        if ($_POST['selected_user'] == 'new') {

        // If it's a new categories, add the categories to the database.
        $query = 'INSERT INTO tally_points (users_id) VALUES (';

        // Check for a last_name.
        if (!empty($_POST['users_id'])) {
        $query .= "'" . escape_data($_POST['users_id']) . "')";

        $result = mysql_query ($query); // Run the query.
        $a = mysql_insert_id(); // Get the categories ID.

        } else { // No last name value.
        $a = FALSE;
        echo '<p><font color="red">Please enter the Dealers name!</font></p>';
        }

        } elseif ( ($_POST['selected_user'] == 'existing') && ($_POST['existing'] > 0))
        { // Existing categories.
        $a = (int) $_POST['existing'];
        } else { // No categories selected.
        $a = FALSE;
        echo '<p><font color="red">Please select a registered Dealer!</font></p>';
        }

        if ($p && $a) { // If everything's OK.

        // Add the print to the database.
        $query = "INSERT INTO tally_point (users_id, tally_points_in, order_id, total, tally_points_entry_date) VALUES ('$a', '$p', '0', '0', NOW())"; 
        if ($result = mysql_query ($query)) 
        { 
        // Worked.
        echo '<p>The reward product has been added.</p><br /><a href="add-points-ind.php">Go back</a><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />';
        } else { 
        // If the query did not run OK.
        echo '<p><font color="red">Your submission could not be 
        processed due to a system error.</font></p>';
        }
        } else { // Failed a test.
        echo '<p><font color="red">Please click "back" and try again.</font></p>';
        }
        } else { // Display the form.

        ?>

        <form enctype="multipart/form-data" action="add-points-ind.php" method="post">

        <input type="hidden" name="MAX_FILE_SIZE" value="524288" />

        <fieldset>
        <legend>Add Points Individually:</legend>

        <p><b>Select User:</b></p>

        <p>
        <select name="existing"><option>Select One</option>
        <?php // Retrieve all the users details and add to the pull-down menu.
        $query = "SELECT users_id, users_sale_id, users_first_name, users_surname FROM users ORDER BY users_surname ASC";
        $result = @mysql_query ($query);
        while ($row = @mysql_fetch_array ($result, MYSQL_ASSOC)) {
        echo "<option value=\"{$row['users_id']}\">{$row['users_sale_id']}: {$row['users_first_name']} {$row['users_surname']} </option>\n";
        }
        @mysql_close($dbc); // Close the database connection.
        ?>

        </select></p>
        <span class="extras"><input type="radio" name="selected_user" value="existing" /> Please confirm this is the correct user</span>
        <p><b>Points:</b> <br />
        <input type="text" name="tally_points_in" size="10" maxlength="10" /></p>
        </fieldset>

        <div align="center"><input type="submit" name="submit" value="Submit" /></div>
        <input type="hidden"name="submitted" value="TRUE" />
        </form>

        <?php
        } // End of main conditional.
        ?>

        <br class="clearboth" />
        End text
        </div>

        <?php // Include the HTML footer file.
        include ('includes/footer_admin_user.html');
        ?> 

... Im having trouble with getting the new points added to the points total field (reward_user_points) in the reward_points table, I have some code below but Im not sure where I am supposed to put it, if anyone has any suggestions please let me know.

    <?php
    $query = "SELECT reward_user_points FROM reward_points WHERE users_id = $a";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $TotalPoints = $row['reward_user_points'];

    if (@mysql_affected_rows($dbc) == 1) { // Whohoo!

        $new_credit = $TotalPoints + $p;
        $query = "UPDATE reward_points SET reward_user_points ='$new_credit' WHERE users_id = $a";
        $result = @mysql_query($query); 
        }
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美胚控场 2025-01-09 06:07:50

好吧,我不得不说我不太明白你的问题是什么。您说您在将新积分添加到总积分字段时遇到困难,但您能说得更具体一些吗? php或mysql有返回错误信息吗?

Ok, I have to say that I don't understand very well what your trouble is. You say you're having trouble with getting the new points added to the points total field, but could you be a little more specific? Is there any error message returned by php or mysql?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文