2012-02-03 93 views
0

我有一个网站,登录的用户可以在其中累积积分,然后可以通过购物车购买积分。下面的页面是一个管理员php功能,管理员可以在该功能中为单个用户提供点数(现在每次只有一位用户)。使用PHP将数字值添加到两个MySQL数据库行中

有三个表参与此脚本:

用户:包含用户的详细信息

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); 
     } 
    ?> 

回答

0

好吧,我不得不说,我不明白你的麻烦是什么。你说你在添加总点数的新点上遇到了麻烦,但是你能否更具体些?有没有PHP或MySQL返回的错误信息?

+0

对不起,如果没有任何意义,代码的第一位会为tally_point表添加点,我已尝试将第二位代码添加到代码的第一位,该部分下面说'if($ p && $ a){',并且收到一条错误消息:Undefined variable:users – AdamMc 2012-02-03 06:25:09

+0

好的,让我们清楚我们的想法。你必须关心的第一件事是在你的

标签中通过帖子传递**你需要的所有变量。如果$ a代表用户唯一密钥(id),请尝试给它提示性的名称,如“$ user_id”而不是“$ a”。现在你必须做检查,对吧?从验证user_id的查询开始。如果现在有效,只需将新行插入数据库。 – 2012-02-03 06:53:10

+0

我注意到您的代码中有以下行: 'if(!empty($ _ POST ['users_id']))' 但您的表单标记内没有任何名为'users_id的输入字段”。这可能会导致错误。 – 2012-02-03 06:54:27

相关问题