updateing multiple rows at once, with different values..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RayDube
    New Member
    • Dec 2007
    • 15

    updateing multiple rows at once, with different values..

    Hello Gents,

    I want to be able to reward my members with credits, banner advertisements, text advertisements and even cash, but on a partially manual basis.

    I created a page where I could decide how many members would get a reward, and then update them based on the member id and details I enter, here's the code for the page:
    Code:
    <?php
    /**
     * @author Raymond Dube
     * @copyright 2008
     * @version 1.0
     */
    include ('../../system.php');
    function ryg_mailer($message)
    {
      mail("$gReceivingEmail", "Bonus Updater", $message, "From:  Bonus Updater\r\nContent-type: text/html; charset=us-ascii");
      return;
    }
    
    if(!$_POST["save"])
    {
      ?>
      <html>
      <head>
      <title>Bonus Manager</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <link rel="stylesheet" type="text/css" href="../css/config.css">
      </head>
      <body>
      <form action='bonus.php' method='POST' name='number_or_members'>
        <table width="100%" class="configuration"  bgcolor="#F2F2F2" align="center">
          <tr>
            <th>Bonus Giveaway</th>
            <th>
              <table width="100%">
                <tr>
                  <td align="center">
                    <input type="button" value="Close Configuration" onClick="window.location.replace('../frames/home.php');" name="close" class="buttonsubmit">
                  </td>
                  <td>
                    <input type="submit" value="Submit" name="save" class="buttonsubmit" style="float: right;">
                  </td>
                </tr>
              </table>
            </th>
          </tr>
          <tr>
            <td width="50%" class="c_col_left">How Many Members?<br>
              <font size="1">
                Enter the number of members you want to award bonuses to, then click Submit!
              </font>
            </td>
            <td width="50%" align="center" class="c_col_right">
              <input size="8" value="<? echo $mems; ?>" name="mems">
            </td>
          </tr>
          <tr>
            <th width="100%" colspan="2">
              <p style="text-align: center">
              <input type="submit" value="Submit" name="save" class="buttonsubmit">
            </th>
          </tr>
        </table>
      </form>
      </body>
      </html>
      <?
    }
    
    if($_POST["save"] == "Submit")
    {
      $mems = $_POST[mems];
      ?>
      <html>
      <head>
      <title>Bonus Manager</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <link rel="stylesheet" type="text/css" href="../css/config.css">
      </head>
      <body>
      <form action='bonus.php' method='POST' name='bonus_form'>
      <input type="hidden" name="mems" value="<? echo $mems;?>">
      <table width="100%" class="configuration"  bgcolor="#F2F2F2" align="center">
        <tr>
          <th>Bonus Giveaway</th>
          <th>
            <table width="100%">
              <tr>
                <td align="center">
                  <input type="button" value="Close Configuration" onClick="window.location.replace('../frames/home.php');" name="close" class="buttonsubmit">
                </td>
                <td>
                  <input type="submit" value="Award Members" name="save" class="buttonsubmit" style="float: right;">
                </td>
              </tr>
            </table>
          </th>
        </tr>
        <tr>
          <td width="50%" class="c_col_left">Member IDs<br>
            <font size="1">
              Enter the member id, one id per box please.
            </font>
          </td>
          <td class="c_col_right">
            <table>
              <tr>
      <?
      $i=0;
      while($i < $mems)
      {
        //build table with the number of cells equal to the number of members to update.
        echo "<td><input size=\"8\" value=\"\" name=\"memid[$i]\" id=\"memid[$i]\"></td>";
        $i++;
      }
      ?>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td width="50%" class="c_col_left">Cash<br>
            <font size="1">
              Enter the Cash Amount for each member.
            </font>
          </td>
          <td class="c_col_right">
            <table>
              <tr>
      <?
      $i=0;
      while($i < $mems)
      {
        //build table with the number of cells equal to the number of members to update.
        echo "<td><input size=\"8\" value=\"\" name=\"cashb[$i]\" id=\"cashb[$i]\"></td>";
        $i++;
      }
      ?>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td width="50%" class="c_col_left">Credits<br>
            <font size="1">
              Enter the Credit Amount for each member.
            </font>
          </td>
          <td class="c_col_right">
            <table>
              <tr>
      <?
      $i=0;
      while($i < $mems)
      {
        //build table with the number of cells equal to the number of members to update.
        echo "<td><input size=\"8\" value=\"\" name=\"creds[$i]\" id=\"creds[$i]\"></td>";
        $i++;
      }
      ?>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td width="50%" class="c_col_left">Banners<br>
            <font size="1">
              Enter the Banner Amount for each member.
            </font>
          </td>
          <td class="c_col_right">
            <table>
              <tr>
      <?
      $i=0;
      while($i < $mems)
      {
        //build table with the number of cells equal to the number of members to update.
        echo "<td><input size=\"8\" value=\"\" name=\"bnrs[$i]\" id=\"bnrs[$i]\"></td>";
        $i++;
      }
      ?>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td width="50%" class="c_col_left">Texts<br>
            <font size="1">
              Enter the Text Amount for each member.
            </font>
          </td>
          <td class="c_col_right">
            <table>
              <tr>
      <?
      $i=0;
      while($i < $mems)
      {
        //build table with the number of cells equal to the number of members to update.
        echo "<td><input size=\"8\" value=\"\" name=\"txts[$i]\" id=\"txts[$i]\"></td>";
        $i++;
      }
      ?>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <th width="100%" colspan="2">
            <table width="100%">
              <tr>
                <td align="center">
                  <input type="button" value="Reset" onClick="window.location.replace('bonus.php');" name="close" class="buttonsubmit">
                </td>
                <td>
                  <input type="submit" value="Award Members" name="save" class="buttonsubmit" style="float: right;">
                </td>
              </tr>
            </table>
          </th>
        </tr>
      </table>	
      </form>
      <?
    }
    if($_POST["save"] == "Award Members")
    {
      $mems = $_POST['mems'];
      $i=0;
      while ($i < $mems)
      {
    	$memid = $_POST['memid'][$i];
        $cashb = $_POST['cashb'][$i];
        $creds = $_POST['creds'][$i];
        $bnrs = $_POST['bnrs'][$i];
        $txts = $_POST['txts'][$i];
        $GET_SQL = "UPDATE `vtp_members` SET `cash`=`cash`+'$cashb', `hits`=`hits`+'$creds', `banner_total`=`banner_total`+'$bnrs', `text_total`=`text_total`+'$txts' WHERE `id`='$memid'";
        echo "<br>Query: ".$GET_SQL;
        /*$update_result = mysql_query($GET_SQL);
        if (!$update_result)
        {
          $error = "Bonus Update did not function as expected.  Member: $membid; Cash: $cashb; Credits: $creds; Banners: $bnrs; Texts: $txts.";
          ryg_mailer($error);
        }*/
      ++$i;
      }
    }
    ?>
    Now the ony problem is that the update portion is not working properly. When I first visit the page a textbox appears asking me how many members I want to reward, so I chose 5 for example, after clicking on submit, I get 5 text boxes on each for for each type I want to reward.

    Now when I put numbers in these fields, and click submit again, I get incorrect date, in fact, I get the letters that spell Array, so something is wrong, but I just can't seem to see it.

    Help! Please! :)

    Ray
  • RayDube
    New Member
    • Dec 2007
    • 15

    #2
    Code:
    if($_POST["save"] == "Submit")
    {
      $mems = $_POST[mems];
      $i=0;
      while($i < $mems)
      {
        //build table with the number of cells equal to the number of members to update.
        echo "<tr>";
        echo "<td><input type=\"text\" id=\"memid[]\" name=\"memid[]\" value=\"\" size=8></td>";
        echo "<td><input type=\"text\" id=\"cashb[]\" name=\"cashb[]\" value=\"\" size=8></td>";
        echo "<td><input type=\"text\" id=\"creds[]\" name=\"creds[]\" value=\"\" size=8></td>";
        echo "<td><input type=\"text\" id=\"bnrs[]\" name=\"bnrs[]\" value=\"\" size=8></td>";
        echo "<td><input type=\"text\" id=\"txts[]\" name=\"txts[]\" value=\"\" size=8></td>";
        echo "</tr>";
        $i++;
      }
    }
    And

    Code:
    if($_POST["award"])
    {
      $mems = $_POST['mems'];
      $i=0;
      for($i=0;$i<$mems;$i++)
      {
        $memid = $_POST['memid'][$i];
        $cashb = $_POST['cashb'][$i];
        $creds = $_POST['creds'][$i];
        $bnrs = $_POST['bnrs'][$i];
        $txts = $_POST['txts'][$i];
        $GET_SQL = "UPDATE `vtp_members` SET `cash`=`cash`+'$cashb', `hits`=`hits`+'$creds', `banner_total`=`banner_total`+'$bnrs', `text_total`=`text_total`+'$txts' WHERE `id`='$memid'";
        echo "<br>Query: ".$GET_SQL;
      }
    }
    The code looks good, but doesn't work, what's up folks, I need an assist...

    Ray

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      It is difficult to wade through somebody else's code.
      You need to isolate the problem more.

      Echo out some values at different stages then you can see what is there.
      Array() means very little, but it generally appears when reading a variable which is actually an array or it could be an empty array

      As a tip there is no need to echo out every line of HTML.
      Also use single quotes
      Code:
      echo '<tr>
          <td><input type="text" id="memid[]" name="memid[]" value="" size=8></td>
          <td><input type="text" id="cashb[]" name="cashb[]" value="" size=8></td>
          <td><input type="text" id="creds[]" name="creds[]" value="" size=8></td>etc...';
      This is more efficient and avoids the ugly escapes

      Comment

      • RayDube
        New Member
        • Dec 2007
        • 15

        #4
        Hi code green,

        I agree, no need to echo each line, but it was done because of "compressin g" there is a little bit of extra functionality in between that was edited out.

        Meanwhile, this *should* work, but doesn't, which is why I need an assist.

        Ray

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          I have looked ar your code but may have missed the problem because there is a lot.

          At the moment you are working blind and have no idea what is happening.
          That is why I suggested putting some debug help in there.
          Simply place print_r($_POST) and echo $mems etc at strategic locations
          in your code.
          This will help narrow down to where the problem is.

          I guess your HTML array is emptying somewhere

          Comment

          • RayDube
            New Member
            • Dec 2007
            • 15

            #6
            Hi there,

            Not many places to put those codes, but I did try to streamline it a bit and still can't imagine where the Array could possibly be emptying...

            This is the result after the first post (where the number of members to be awarded is determined):
            1 member:
            Code:
            Array ( [mems] => 1 [save] => Submit )
            5 members:
            Code:
            Array ( [mems] => 5 [save] => Submit )
            After second post (where bonus amts are added):
            1 member:
            Code:
            Array ( [mems] => 1 [memid] => Array [cashb] => Array [creds] => Array [bnrs] => Array [txts] => Array [Award] => Award )
            5 members:
            Code:
            Array ( [mems] => 5 [memid] => Array [cashb] => Array [creds] => Array [bnrs] => Array [txts] => Array [Award] => Award )
            Any suggestions?

            Ray

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              OK. From this output
              Code:
              Array ( [mems] => 5 [memid] => Array [cashb] => Array [creds] => Array [bnrs] => Array [txts] => Array [Award] => Award )
              I can now see the $_POST elements.
              The inner arrays can be seen by
              Code:
              print_r($_POST['memid']); 
              print_r($_POST['cashb']);
              print_r($_POST['creds']); etc
              If you want to check their contents.
              I will study this further, but just as a thought do you have register globals turned on?
              If so, as memid is an HTML array AND a php variable there may be a problem here

              Comment

              • RayDube
                New Member
                • Dec 2007
                • 15

                #8
                Hi there,

                I've managed to find the problem, one of the included files is stripping the contents of the array, even though it's not supposed to do so.

                We're working with the original script creator to remove the problem code, unf, it's encoded... :(

                Ray

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  Did you spot this by echoing out the variables?
                  I am happy if you take away this valuable lesson

                  Comment

                  • RayDube
                    New Member
                    • Dec 2007
                    • 15

                    #10
                    Well, honestly?

                    I re-wrote the script, entirely, forgot the include they had for sql setup, and the variables were passing, when I added the include, it stopped...

                    Unfortunately, had I done that in the first place, I would have spotted it directly.

                    Ray

                    Comment

                    Working...